网关 Spring Cloud Zuul 静态、动态路由配置的几种形式

435 阅读1分钟

文章目录

静态Url路由

path+url

zuul:
  routes:
    laker:
      path: /laker/**
      url: http://localhost:8080

其中path是一个ant风格的表达式,所以/laker/*仅仅匹配一层目录,而/laker/**可以匹配任意多层级目录。

简单的url路由不会作为HystrixCommand执行,也不会使用Ribbon负载均衡,即无负载均衡,故障切换,重试,断路器功能。

使用的 PRE类型的SimpleHostRoutingFilter.java

静态服务器列表路由

指定一个服务器列表的serviceId

zuul:
  routes:
    echo:
      path: /myusers/**
      serviceId: myusers-service
      stripPrefix: true

hystrix:
  command:
    myusers-service:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: ...

myusers-service:
  ribbon:
    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    listOfServers: https://example1.com,http://example2.com
    ConnectTimeout: 1000
    ReadTimeout: 3000
    MaxTotalHttpConnections: 500
    MaxConnectionsPerHost: 100

使用的 PRE类型的RibbonRoutingFilter.java
另一个方法是指定一个服务路由并且为serviceId配置Ribbon客户端(这么做需要在Ribbon中禁用Eureka)

zuul:
  routes:
    users:
      path: /myusers/**
      serviceId: users

ribbon:
  eureka:
    enabled: false

users:
  ribbon:
    listOfServers: example.com,google.com

动态路由

配合注册中心使用,例如eureka或者nacos

zuul:
  routes:
    myroutes1:
      path: /myusers/**
      serviceId: users
eureka:
  client:
    serviceUrl:
      defaultZne:xxx

加群一起抱团取暖,共同进步

🍎QQ群【837324215】
🍎关注我的公众号【Java大厂面试官】,一起学习呗🍎🍎🍎
🍎个人vxlakernote