歡迎訪問我的GitHub
https://github.com/zq2599/blog_demos
內(nèi)容:所有原創(chuàng)文章分類匯總及配套源碼冤狡,涉及Java嗅绰、Docker秋度、Kubernetes娱挨、DevOPS等;
本篇概覽
- 本文是《Spring Cloud Gateway實(shí)戰(zhàn)》系列的第三篇,前文介紹了多種路由配置方式,它們存在一個(gè)共同問題:路由配置變更后必須重啟Gateway應(yīng)用才能生效祷愉,聰明的您一下就看出了問題關(guān)鍵:這樣不適合生產(chǎn)環(huán)境窗宦!
- 如何讓變動(dòng)后的路由立即生效,而無需重啟應(yīng)用呢二鳄?這就是今天的主題:<font color="blue">動(dòng)態(tài)路由</font>
設(shè)計(jì)思路
- 這里提前將設(shè)計(jì)思路捋清楚赴涵,總的來說就是將配置放在nacos上,寫個(gè)監(jiān)聽器監(jiān)聽nacos上配置的變化订讼,將變化后的配置更新到Gateway應(yīng)用的進(jìn)程內(nèi):
- 上述思路體現(xiàn)在代碼中就是下面三個(gè)類:
- 將操作路由的代碼封裝到名為RouteOperator的類中髓窜,用此類來刪除和增加進(jìn)程內(nèi)的路由
- 做一個(gè)配置類RouteOperatorConfig,可以將RouteOperator作為bean注冊(cè)在spring環(huán)境中
- 監(jiān)聽nacos上的路由配置文件欺殿,一旦有變化就取得最新配置寄纵,然后調(diào)用RouteOperator的方法更新進(jìn)程內(nèi)的路由,這些監(jiān)聽nacos配置和調(diào)用RouteOperator的代碼都放RouteConfigListener類中
在本次實(shí)戰(zhàn)中脖苏,一共涉及三個(gè)配置文件程拭,其中bootstrap.yml + gateway-dynamic-by-nacos是大家熟悉的經(jīng)典配置,bootstrap.yml 在本地棍潘,里面是nacos的配置恃鞋,gateway-dynamic-by-nacos在naocs上,里面是整個(gè)應(yīng)用所需的配置(例如服務(wù)端口號(hào)亦歉、數(shù)據(jù)庫(kù)等)恤浪,還有一個(gè)配置文件在nacos上,名為<font color="blue">gateway-json-routes</font>肴楷,是JSON格式的水由,里面是路由配置,之所以選擇JSON格式赛蔫,是因?yàn)镴SON比yml格式更易于解析和處理砂客;
最終,整個(gè)微服務(wù)架構(gòu)如下圖所示:
- 思路已清晰呵恢,開始編碼
源碼下載
- 本篇實(shí)戰(zhàn)中的完整源碼可在GitHub下載到鞭盟,地址和鏈接信息如下表所示(https://github.com/zq2599/blog_demos):
名稱 | 鏈接 | 備注 |
---|---|---|
項(xiàng)目主頁 | https://github.com/zq2599/blog_demos | 該項(xiàng)目在GitHub上的主頁 |
git倉(cāng)庫(kù)地址(https) | https://github.com/zq2599/blog_demos.git | 該項(xiàng)目源碼的倉(cāng)庫(kù)地址,https協(xié)議 |
git倉(cāng)庫(kù)地址(ssh) | git@github.com:zq2599/blog_demos.git | 該項(xiàng)目源碼的倉(cāng)庫(kù)地址瑰剃,ssh協(xié)議 |
- 這個(gè)git項(xiàng)目中有多個(gè)文件夾,本篇的源碼在<font color="blue">spring-cloud-tutorials</font>文件夾下筝野,如下圖紅框所示:
- spring-cloud-tutorials是父工程晌姚,下屬多個(gè)子工程,今天的實(shí)戰(zhàn)的代碼是<font color="blue">gateway-dynamic-by-nacos</font>歇竟,如下圖所示:
編碼
- 新增名為<font color="blue">gateway-dynamic-by-nacos</font>的工程挥唠,其pom.xml內(nèi)容如下,注意中文注釋的說明:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tutorials</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway-dynamic-by-nacos</artifactId>
<dependencies>
<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 把springboot內(nèi)容斷點(diǎn)暴露出去 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 使用bootstrap.yml的時(shí)候焕议,這個(gè)依賴一定要有 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- 路由策略使用lb的方式是宝磨,這個(gè)依賴一定要有 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--nacos:配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos:注冊(cè)中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 如果父工程不是springboot,就要用以下方式使用插件,才能生成正常的jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.bolingcavalry.gateway.GatewayApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 配置文件bootstrap.yml唤锉,上面只有nacos世囊,可見其他配置信息還是來自naocs:
spring:
application:
name: gateway-dynamic-by-nacos
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yml
group: DEFAULT_GROUP
- 負(fù)責(zé)處理進(jìn)程內(nèi)路由配置的類是RouteOperator,如下所示窿祥,可見整個(gè)配置是字符串類型的株憾,用了Jackson的ObjectMapper進(jìn)行反序列化(注意,前面的實(shí)戰(zhàn)中配置文件都是yml格式晒衩,但本例中是JSON嗤瞎,稍后在nacos上配置要用JSON格式),然后路由配置的處理主要是RouteDefinitionWriter類型的bean完成的听系,為了讓配置立即生效贝奇,還要用applicationEventPublisher發(fā)布進(jìn)程內(nèi)消息:
package com.bolingcavalry.gateway.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class RouteOperator {
private ObjectMapper objectMapper;
private RouteDefinitionWriter routeDefinitionWriter;
private ApplicationEventPublisher applicationEventPublisher;
private static final List<String> routeList = new ArrayList<>();
public RouteOperator(ObjectMapper objectMapper, RouteDefinitionWriter routeDefinitionWriter, ApplicationEventPublisher applicationEventPublisher) {
this.objectMapper = objectMapper;
this.routeDefinitionWriter = routeDefinitionWriter;
this.applicationEventPublisher = applicationEventPublisher;
}
/**
* 清理集合中的所有路由,并清空集合
*/
private void clear() {
// 全部調(diào)用API清理掉
routeList.stream().forEach(id -> routeDefinitionWriter.delete(Mono.just(id)).subscribe());
// 清空集合
routeList.clear();
}
/**
* 新增路由
* @param routeDefinitions
*/
private void add(List<RouteDefinition> routeDefinitions) {
try {
routeDefinitions.stream().forEach(routeDefinition -> {
routeDefinitionWriter.save(Mono.just(routeDefinition)).subscribe();
routeList.add(routeDefinition.getId());
});
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* 發(fā)布進(jìn)程內(nèi)通知靠胜,更新路由
*/
private void publish() {
applicationEventPublisher.publishEvent(new RefreshRoutesEvent(routeDefinitionWriter));
}
/**
* 更新所有路由信息
* @param configStr
*/
public void refreshAll(String configStr) {
log.info("start refreshAll : {}", configStr);
// 無效字符串不處理
if (!StringUtils.hasText(configStr)) {
log.error("invalid string for route config");
return;
}
// 用Jackson反序列化
List<RouteDefinition> routeDefinitions = null;
try {
routeDefinitions = objectMapper.readValue(configStr, new TypeReference<List<RouteDefinition>>(){});
} catch (JsonProcessingException e) {
log.error("get route definition from nacos string error", e);
}
// 如果等于null掉瞳,表示反序列化失敗,立即返回
if (null==routeDefinitions) {
return;
}
// 清理掉當(dāng)前所有路由
clear();
// 添加最新路由
add(routeDefinitions);
// 通過應(yīng)用內(nèi)消息的方式發(fā)布
publish();
log.info("finish refreshAll");
}
}
- 做一個(gè)配置類RouteOperatorConfig.java髓帽,將實(shí)例化后的RouteOperator注冊(cè)到spring環(huán)境中:
package com.bolingcavalry.gateway.config;
import com.bolingcavalry.gateway.service.RouteOperator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RouteOperatorConfig {
@Bean
public RouteOperator routeOperator(ObjectMapper objectMapper,
RouteDefinitionWriter routeDefinitionWriter,
ApplicationEventPublisher applicationEventPublisher) {
return new RouteOperator(objectMapper,
routeDefinitionWriter,
applicationEventPublisher);
}
}
- 最后是nacos的監(jiān)聽類RouteConfigListener菠赚,可見關(guān)鍵技術(shù)點(diǎn)是ConfigService.addListener,用于添加監(jiān)聽郑藏,里面就是配置發(fā)生變化后更新路由的邏輯衡查,另外還有很重要的一步:立即調(diào)用getConfig方法取得當(dāng)前配置,刷新當(dāng)前進(jìn)程的路由配置:
package com.bolingcavalry.gateway.service;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.concurrent.Executor;
@Component
@Slf4j
public class RouteConfigListener {
private String dataId = "gateway-json-routes";
private String group = "DEFAULT_GROUP";
@Value("${spring.cloud.nacos.config.server-addr}")
private String serverAddr;
@Autowired
RouteOperator routeOperator;
@PostConstruct
public void dynamicRouteByNacosListener() throws NacosException {
ConfigService configService = NacosFactory.createConfigService(serverAddr);
// 添加監(jiān)聽必盖,nacos上的配置變更后會(huì)執(zhí)行
configService.addListener(dataId, group, new Listener() {
public void receiveConfigInfo(String configInfo) {
// 解析和處理都交給RouteOperator完成
routeOperator.refreshAll(configInfo);
}
public Executor getExecutor() {
return null;
}
});
// 獲取當(dāng)前的配置
String initConfig = configService.getConfig(dataId, group, 5000);
// 立即更新
routeOperator.refreshAll(initConfig);
}
}
RouteConfigListener.java中還有一處要記下來拌牲,那就是dataId變量的值<font color="red">gateway-json-routes</font>,這是nacos上配置文件的名字歌粥,稍后咱們?cè)趎acos上配置的時(shí)候會(huì)用到
最后是平淡無奇的啟動(dòng)類:
package com.bolingcavalry.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}
編碼完成了塌忽,接下來在nacos上增加兩個(gè)配置;
第一個(gè)配置名為<font color="blue">gateway-dynamic-by-nacos</font>失驶,內(nèi)容如下:
server:
port: 8086
# 暴露端點(diǎn)
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
- 第二個(gè)配置名為<font color="blue">gateway-json-routes</font>土居,格式要選擇<font color="red">JSON</font>,可見只有一個(gè)路由(IP+端口那個(gè))嬉探,另一個(gè)用服務(wù)名作為URL的路由先不配上去擦耀,稍后用來驗(yàn)證動(dòng)態(tài)增加能不能立即生效:
[
{
"id": "path_route_addr",
"uri": "http://127.0.0.1:8082",
"predicates":[
{
"name": "Path",
"args": {
"pattern": "/hello/**"
}
}
]
}
]
- 至此,咱們已經(jīng)完成了開發(fā)工作涩堤,接下來驗(yàn)證動(dòng)態(tài)路由是否能達(dá)到預(yù)期效果眷蜓,我這里用的客戶端工具是postman
驗(yàn)證
- 確保nacos、provider-hello胎围、gateway-dynamic-by-nacos等服務(wù)全部啟動(dòng):
- 用postman訪問<font color="blue">http://127.0.0.1:8086/hello/str</font>吁系,可以正常訪問到德召,證明Gateway應(yīng)用已經(jīng)從nacos順利下載了路由:
- 此時(shí)如果用訪問<font color="blue">http://127.0.0.1:8086/lbtest/str</font>應(yīng)該會(huì)失敗,因?yàn)閚acos上還沒有配置這個(gè)path的路由汽纤,如下圖上岗,果然失敗了:
- 在nacos上修改配置項(xiàng)<font color="blue">gateway-json-routes</font>的內(nèi)容,增加名為<font color="red">path_route_lb</font>的路由配置冒版,修改后完整的配置如下:
[
{
"id": "path_route_addr",
"uri": "http://127.0.0.1:8082",
"predicates":[
{
"name": "Path",
"args": {
"pattern": "/hello/**"
}
}
]
}
,
{
"id": "path_route_lb",
"uri": "lb://provider-hello",
"predicates":[
{
"name": "Path",
"args": {
"pattern": "/lbtest/**"
}
}
]
}
]
- 點(diǎn)擊右下角的<font color="blue">發(fā)布</font>按鈕后液茎,gateway-dynamic-by-nacos應(yīng)用的控制臺(tái)立即輸出了以下內(nèi)容,可見監(jiān)聽已經(jīng)生效:
2021-08-15 19:39:45.883 INFO 18736 --- [-127.0.0.1_8848] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848] [polling-resp] config changed. dataId=gateway-json-routes, group=DEFAULT_GROUP
2021-08-15 19:39:45.883 INFO 18736 --- [-127.0.0.1_8848] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[gateway-json-routes+DEFAULT_GROUP]
2021-08-15 19:39:45.890 INFO 18736 --- [-127.0.0.1_8848] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848] [data-received] dataId=gateway-json-routes, group=DEFAULT_GROUP, tenant=null, md5=54fb76dcad838917818d0160ce2bd72f, content=[
{
"id": "path_route_addr",
"uri": "http://127.0.0.1:8082",
"predicates..., type=json
2021-08-15 19:39:45.891 INFO 18736 --- [-127.0.0.1_8848] c.b.gateway.service.RouteOperator : start refreshAll : [
{
"id": "path_route_addr",
"uri": "http://127.0.0.1:8082",
"predicates":[
{
"name": "Path",
"args": {
"pattern": "/hello/**"
}
}
]
}
,
{
"id": "path_route_lb",
"uri": "lb://provider-hello",
"predicates":[
{
"name": "Path",
"args": {
"pattern": "/lbtest/**"
}
}
]
}
]
2021-08-15 19:39:45.894 INFO 18736 --- [-127.0.0.1_8848] c.b.gateway.service.RouteOperator : finish refreshAll
2021-08-15 19:39:45.894 INFO 18736 --- [-127.0.0.1_8848] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_8848] [notify-ok] dataId=gateway-json-routes, group=DEFAULT_GROUP, md5=54fb76dcad838917818d0160ce2bd72f, listener=com.bolingcavalry.gateway.service.RouteConfigListener$1@123ae1f6
2021-08-15 19:39:45.894 INFO 18736 --- [-127.0.0.1_8848] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_8848] [notify-listener] time cost=3ms in ClientWorker, dataId=gateway-json-routes, group=DEFAULT_GROUP, md5=54fb76dcad838917818d0160ce2bd72f, listener=com.bolingcavalry.gateway.service.RouteConfigListener$1@123ae1f6
- 再用postman發(fā)同樣請(qǐng)求辞嗡,這次終于成功了捆等,可見動(dòng)態(tài)路由已經(jīng)成功:
- 由于依賴了<font color="red">spring-boot-starter-actuator</font>庫(kù),并且配置文件中也添加了相關(guān)配置续室,我們還可以查看SpringBoot應(yīng)用內(nèi)部的配置情況栋烤,用瀏覽器訪問<font color="blue">http://localhost:8086/actuator/gateway/routes</font>,可見最新的配置情況挺狰,如下圖:
- 至此明郭,動(dòng)態(tài)路由的開發(fā)和驗(yàn)證已完成,希望這個(gè)實(shí)用的功能可以給您一些參考丰泊,開發(fā)出更加靈活實(shí)用的網(wǎng)關(guān)服務(wù)薯定;
你不孤單,欣宸原創(chuàng)一路相伴
歡迎關(guān)注公眾號(hào):程序員欣宸
微信搜索「程序員欣宸」瞳购,我是欣宸话侄,期待與您一同暢游Java世界...
https://github.com/zq2599/blog_demos