Zuul 整合Config实现动态路由
Zuul配置
- pom.xml
-
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <!--健康监控,用于配置文件refresh--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
- application.yml
-
spring: application: name: hyq-zuul-server profiles: active: test #端口 server.port: 4002 # setting up spring eureka service discovery eureka: instance: prefer-ip-address: true instance-id: ${spring.application.name}:${server.port} client: serviceUrl: # defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/,http://localhost:8083/eureka/ defaultZone: http://admin:admin@localhost:8081/eureka/
-
- bootstrap.yml
-
# config 配置中心配置-->只能放在bootstrap.yml文件中才能生效 spring: cloud: config: uri: http://localhost:5001 profile: test label: master username: admin password: admin
-
- 码云配置文件:https://gitee.com/hyman.hu/springcloud-config-server/blob/master/config/application-test.yml
- application-test.yml
-
# 路由配置:实际开发过程中放到config配置中心 zuul: routes: # 别名 eureka-client: # 通配符说明: # ?: 匹配任意单字符 eg:/client/? 匹配 /client/a /client/b # *: 匹配任意数量字符 eg: /client/* 匹配 /client/user # **: 匹配任意数量字符,支持多久目录 eg: /client/** 匹配 /client/user/info path: /client/** serviceId: HYQ-EUREKA-CLIENT #如果是/ribbon/**路径下的请求,则跳转到service-ribbon eureka-server: path: /server/** serviceId: HYQ-HSYTRIX-CLIENT #如果是/feign/**路径下的请求,则跳转到service-feign cinfig-client: path: /config-client/** serviceId: HYQ-CONFIG-CLIENT #如果是/feign/**路径下的请求,则跳转到service-feign # 使用zuul.ignored-services配置需要忽略的服务,多个用逗号分隔 ignored-services: '*' #只代理配置的微服务 #ignored-patterns: /client/** # 忽略所有包含client/** 的路径 # 暴露 routes 接口:默认暴露health,info # 查看以及管理Zuul的路由:http://localhost:4001/actuator/routes management: endpoints: web: exposure: include: routes,refresh # 配默认熔断超时策略 # zuul配置了熔断fallback的话,熔断超时也要配置, # 不然如果你配置的ribbon超时时间大于熔断的超时,那么会先走熔断,相当于你配的ribbon超时就不生效了。 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 60000 # ribbon超时时间设置 ribbon: ReadTimeout: 60000 ConnectTimeout: 60000
-
- application-test.yml
- 启动类:ZuulConfigApplication.java
-
@SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class ZuulConfigApplication { public static void main(String[] args) { SpringApplication.run(ZuulConfigApplication.class, args); } @Bean @RefreshScope @ConfigurationProperties("zuul") @Primary //必须 public ZuulProperties zuulProperties(){ return new ZuulProperties(); } }
-
ConfigServer 配置
-
参考博客:https://blog.csdn.net/m0_37204491/article/details/86014545
-
路由查看地址: GET http://128.160.180.1:4002/actuator/routes
-
其他 Eureka client 可以参考我的其他博客