springcloud config、 bus配置,gateway路由动态调整

下面涉及4个服务,测试网关路由动态调整,服务:

  • iflydocs-discovery 服务注册中心 端口:8080

  • iflydocs-config 配置中心 端口:8087

  • iflydocs-gateway 网关 端口:8081

  • iflydocs-auth 认证服务 端口:8082

一、git创建配置文件仓库

config-repo
B1CEBF46-0845-4D2C-806E-2B0CDDA0E92C.png
创建所需配置文件,添加测试配置
iflydocs-gateway-local.properties

gateway.private.test=six

生成git private key;
终端执行:
ssh-keygen -m PEM -t rsa -b 4096 -C "test@126.com"
输入passphrase
生成文件:id_rsa id_rsa.pub known_hosts
在gitlab生成输入id_rsa.pub
BC5A17B7-409C-4ADC-B336-9EECE519F36E.png
id_rsa文件将在下面配置中用到

准备kafka消息队列
创建topic命名:config

二、iflydocs-config配置

pom.xml添加依赖



<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-kafka</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-kafka</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-bus</artifactId>
    </dependency>

application.properties

spring.cloud.bus.enabled=true
spring.cloud.bus.destination=springCloudBus

spring.cloud.config.label=master
spring.cloud.config.server.git.uri=git@git.xx.com:yuwb/config-repo.git
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.server.git.force-pull=true

#使用用户名密码配置
#spring.cloud.config.server.git.uri=https://git.xx.com/yuwb/config-repo.git
#spring.cloud.config.server.git.username=xxxx
#spring.cloud.config.server.git.password=xxxx

#gitlab ssh key配置
spring.cloud.config.server.git.host-key-algorithm=ssh-rsa
spring.cloud.config.server.git.passphrase=iflydocs
spring.cloud.config.server.git.private-key=-----BEGIN RSA PRIVATE KEY-----\n\
xxxxx(id_rsa中的文本内容)\n\
-----END RSA PRIVATE KEY-----

iflydocs-gateway服务配置

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-bus-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>

bootstrap.properties(优先于application.properties加载)

spring.cloud.bus.enabled=true
spring.cloud.bus.refresh.enabled=true
spring.cloud.bus.env.enabled=true
# 加载对应的端点/默认只加载了 info / health
#management.endpoints.web.exposure.include=bus-refresh
management.endpoints.web.exposure.include=*
spring.kafka.bootstrap-servers=kafka-xx:9092,kafka-xx:9092
spring.cloud.config.label=master
spring.cloud.config.uri=http://127.0.0.1:8087
spring.cloud.config.profile=local
spring.kafka.consumer.group-id=config
spring.cloud.config.name=iflydocs-gateway
spring.cloud.config.discovery.service-id=iflydocs-config
spring.cloud.bus.trace.enabled=true

添加api读取properties配置

@RestController
@RequestMapping("/api")
@RefreshScope
public class ApiController {
    @Autowired
    private IflynoteApiService iflynoteApiService;

    @Value("${gateway.private.test}")
    private String pr;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @GetMapping("/configTest")
    public String configTest(){
        return pr;
    }

}

依次启动iflydocs-discovery、iflydocs-config、iflydocs-gateway、iflydocs-auth服务

测试配置读取:
CC43C172-FFDE-43F8-B1C6-E3E8C8CC2BE6.png
从上图查看已读取到配置
验证auth服务接口:
72F89F02-A139-46CC-B141-ED071F4AD940.png
验证网关没有添加auth服务路由:
FD2EA364-9F27-4A99-A18D-3FEC55A3E8CB.png

修改配置,验证路由调整效果

修改config-repo的iflydocs-gateway-local.properties配置文件:
变更gateway.private.test的数值
添加服务路由:

gateway.private.test=777
#路由ID
spring.cloud.gateway.routes[0].id=iflydocs-auth
#路由URI  lb代表从注册中心获取服务
spring.cloud.gateway.routes[0].uri=lb://iflydocs-auth
#设置路由断言,代理servicerId为auth-service的路径
spring.cloud.gateway.routes[0].predicates[0]=Path=/iflydocs-auth/**
#忽略前缀  http://localhost:8081/iflydocs-auth/test/get --> http://localhost:8082/test/get
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1

调用iflydocs-config刷新配置接口:
curl -X 'POST' 'http://localhost:8087/actuator/bus-refresh'

测试接口:
8D99E990-C446-4125-8284-C22E3774D448.png
使用路由转发接口请求:
65144A59-CCBE-4866-8BAC-C6DA93AA9556.png

配置数值更新成功,路由更新成功.

路由变更测试:

修改路由配置:

spring.cloud.gateway.routes[0].id=iflydocs-auth2
#路由URI  lb代表从注册中心获取服务
spring.cloud.gateway.routes[0].uri=lb://iflydocs-auth
#设置路由断言,代理servicerId为auth-service的路径
spring.cloud.gateway.routes[0].predicates[0]=Path=/iflydocs-auth2/**
#忽略前缀  http://localhost:8081/iflydocs-auth/test/get --> http://localhost:8082/test/get
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1

调用刷新接口:
curl -X 'POST' 'http://localhost:8087/actuator/bus-refresh'
使用新路由调用接口:
745143E7-EA84-4155-96A4-C35AA7A9EDFB.png
路由配置已刷新成功

删除路由配置,调用刷新接口:
去除路由配置,发现仍然可以访问,已经加载的配置无法通过删除配置刷新,只能重启服务。

发表新评论