1. 为什么要使用动态路由
一个由springCloud搭建的微服务系统,如果功能模块比较多,就需要在zuul中配置多个路由,称之为静态路由。但是,每次新增或修改路由的时候,都需要重启zuul,所以,如果实现了动态更新路由配置,就不需要重启zuul了。
2. 动态路由实现
1. 新建config-server
ConfigServerApplication.java 添加@EnableConfigServer注解。
@EnableConfigServer
@EnableEurekaServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sitech</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1</version>
<name>config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在github上创建一个zuul-dev.yml文件
zuul:
routes:
app-tcv:
path: /tcv/**
serviceId: app-tcv
app-incomeDetail:
path: /incomeDetail/**
serviceId: app-incomeDetail
app-generateorder:
path: /generateOrder/**
serviceId: app-generateOrder
app-billing:
path: /billing/**
serviceId: app-billing
app-historyCheck:
path: /historyCheck/**
serviceId: app-historyCheck
新建一个application.yml配置文件,配置如下:
server:
#服务端口号
port: 8013
spring:
application:
#服务名称 - 服务之间使用名称进行通讯
name: config-server
#对应zuul-dev.yml文件的dev
profiles:
active: dev
cloud:
config:
server:
git:
#git 仓库的地址
uri: xxxxx
#git 仓库的目录路径
search-paths: /
#git 仓库的账号密码
username: xxxxx
password: xxxxx
eureka:
client:
service-url:
# 填写注册中心服务器地址
defaultZone: http://root:root@localhost:8010/eureka
# 是否需要将自己注册到注册中心
register-with-eureka: true
# 是否需要搜索服务信息
fetch-registry: true
instance:
# 使用ip地址注册到注册中心
prefer-ip-address: true
# 注册中心列表中显示的状态参数
instance-id: ${spring.cloud.client.ip-address}:${server.port}
启动config-server,访问http://localhost:8013/zuul-dev.yml,可以看到zuul-dev.yml的配置。
修改zuul-dev.yml,注释掉一个路由
再次访问http://localhost:8013/zuul-dev.yml,可以看到zuul-dev.yml的配置已更新。
2. 修改zuul配置
pom加上实时刷新actuator和config-client的jar依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
zuul的配置文件要用bootstrap-dev.yml ,而不是用application.yml。原因:https://www.cnblogs.com/BlogNetSpace/p/8469033.html
server:
#服务端口号
port: 8012
servlet:
encoding:
charset: utf-8
force: true
enabled: true
spring:
application:
#git上配置文件名称
name: zuul
cloud:
config:
####读取后缀
profile: dev
name: zuul
####读取config-server注册地址
discovery:
#configServer在注册中心的别名
service-id: config-server
enabled: true
management:
server:
port: ${server.port}
endpoints:
web:
exposure:
include: "*"
eureka:
client:
service-url:
# 填写注册中心服务器地址
defaultZone: http://root:root@localhost:8010/eureka
# 是否需要将自己注册到注册中心
register-with-eureka: true
# 是否需要搜索服务信息
fetch-registry: true
instance:
# 使用ip地址注册到注册中心
prefer-ip-address: true
# 注册中心列表中显示的状态参数
instance-id: ${spring.cloud.client.ip-address}:${server.port}
修改ZuulApplication.java
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
// 实时刷新zuul
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties(){
return new ZuulProperties();
}
}
这时候修改github上的zuul-dev.yml文件,只要执行
curl -v -X POST “http://localhost:8012/actuator/refresh”
就可以实现动态更新zuul路由了。