Springcloud config + zuul 搭建动态网关
1,实现的效果,就是zuul 网关的配置路由实现负载均衡,zuul 的配置文件放在springcloud config 上
2,需要的服务如下:
3,其实就是配置下springcloud-zuul 的配置,连接到springcloud-config
maven:在原来的zuul 基础上,加上spring-config-client 和监控中心(手动刷新用的)
<!-- actuator监控中心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- springcloud config 2.0 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
bootstrap.yml
spring: application: ####注册中心应用名称 name: service-zuul cloud: config: ####读取后缀 profile: dev ####读取config-server注册地址 discovery: service-id: config-server enabled: true #####eureka服务注册地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka server: port: 80 #配置手动实时刷新 #managementendpoints.web.exposure.include=* management: endpoints: web: exposure: include: "*"
git 上的配置文件如下:service-zuul-dev-yml
### 配置网关反向代理 zuul: routes: api-member: ### 以 /api-member/访问转发到会员服务 path: /api-member/** serviceId: app-aiyuesheng-member api-order: ### 以 /api-order/访问转发到订单服务 path: /api-order/** serviceId: app-aiyuesheng-order
如果配置文件修改了,参照springcloud-config 手动刷新,只不过,他需要在启动类加上这个
@RefreshScope @ConfigurationProperties("zuul") public ZuulProperties zuulProperties() { return new ZuulProperties(); }
再通过post man 发送post 请求:
http://127.0.0.1/actuator/refresh
Aimer,c'est partager