SpringCloud Gateway動態(tài)轉(zhuǎn)發(fā)后端服務(wù)實現(xiàn)過程講解
前言
API網(wǎng)關(guān)的核心功能是統(tǒng)一流量入口,實現(xiàn)路由轉(zhuǎn)發(fā),SpringCloudGateway是API網(wǎng)關(guān)開發(fā)的技術(shù)之一,此外比較流行的還有Kong和ApiSix,這2個都是基于OpenResty技術(shù)棧。
簡單的路由轉(zhuǎn)發(fā)可以通過SpringCloudGateway的配置文件實現(xiàn),在一些業(yè)務(wù)場景種,會需要動態(tài)替換路由配置中的后端服務(wù)地址,單純靠配置文件無法滿足這種需求。
本文介紹一種將路由配置保存到數(shù)據(jù)庫中,可以根據(jù)接口請求的特定條件,從數(shù)據(jù)庫中動態(tài)讀取后端服務(wù)地址,實現(xiàn)靈活轉(zhuǎn)發(fā)。
具體的代碼參照 示例項目 https://github.com/qihaiyan/springcamp/tree/master/spring-cloud-gateway
一、概述
通過把SpringCloudGateway的相關(guān)路由配置規(guī)則保存到數(shù)據(jù)庫中,可以動態(tài)的靈活調(diào)整路由。在本文的實現(xiàn)中,我們通過請求header中的特定值,動態(tài)選擇對應(yīng)的后端服務(wù)地址。
二、項目中加入依賴
在項目的gradle中增加依賴關(guān)系。
build.gradle:
plugins {
id 'org.springframework.boot' version '3.0.2'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}group = 'cn.springcamp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'configurations {
compileOnly {
extendsFrom annotationProcessor
}
testCompileOnly {
extendsFrom testAnnotationProcessor
}
}repositories {
mavenCentral()
}dependencies {
implementation "org.springframework.boot:spring-boot-starter-json"
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'io.r2dbc:r2dbc-h2'
annotationProcessor 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation 'org.junit.vintage:junit-vintage-engine'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'com.h2database:h2'
testImplementation 'io.r2dbc:r2dbc-h2'
testImplementation 'org.junit.vintage:junit-vintage-engine'
}dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2022.0.1"
}
}test {
useJUnitPlatform()
}
由于SpringCloudGateway基于SpringWebFlux技術(shù)構(gòu)建,所以依賴中的數(shù)據(jù)庫配置需要使用r2dbc 。
三、配置文件
示例程序首選通過配置文件對路由進行基本配置,配置文件代碼:
spring:
r2dbc:
url: r2dbc:h2:mem:///testdb?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
cloud:
gateway:
routes:
- id: routeOne
predicates:
- Path=/route1/**
uri: no://op
filters:
- UriHostPlaceholderFilter=10001
- id: routeTwo
predicates:
- Path=/route2/**
uri: no://op
filters:
- UriHostPlaceholderFilter=10001
配置文件中配置了2個路由,對應(yīng)的接口地址路徑分別是 /route1/**
和 Path=/route2/**
,路徑中的 ***
表示模糊匹配,只要是以 /route1/
為前綴的路徑都可以被訪問到。
后端服務(wù)地址配置了一個無意的地址: uri: no://op
,因為我們的處理邏輯會通過從數(shù)據(jù)庫中讀取配置來動態(tài)替換后端服務(wù)地址。
四、動態(tài)路由數(shù)據(jù)存儲格式
我們通過 ROUTE_FILTER_ENTITY
這個數(shù)據(jù)庫表來存儲接口后端服務(wù)配置數(shù)據(jù)。表結(jié)構(gòu)為:
CREATE TABLE "ROUTE_FILTER_ENTITY" ( id VARCHAR(255) PRIMARY KEY, route_id VARCHAR(255), -- 路由ID,對應(yīng)配置文件中的 ```id```配置項 code VARCHAR(255), -- 接口請求header中的code參數(shù)的值 url VARCHAR(255) -- 后端服務(wù)地址 );
當客戶端訪問 /route1/test
接口時,根據(jù)配置文件的路由配置,SpringCloudGateway 會命中 id: routeOne
這個路由規(guī)則,這個規(guī)則對應(yīng)的后端服務(wù)地址是 uri: no://op
,并不是我們期望的真實后端服務(wù)地址。
因此,我們需要讀取到真實的后端服務(wù)地址,并將請求轉(zhuǎn)發(fā)到這個地址。跟據(jù) routeId 和 接口請求header中的code參數(shù)的值,就可以從 ROUTE_FILTER_ENTITY 表中查到對應(yīng)的后端服務(wù)地址 url
這個字段的值。
我們已經(jīng)讀取到了后端服務(wù)地址,還需要將請求轉(zhuǎn)發(fā)到這個地址,下面介紹轉(zhuǎn)發(fā)的方法。
五、后端服務(wù)動態(tài)轉(zhuǎn)發(fā)
動態(tài)轉(zhuǎn)發(fā)通過自定義 filter 的方式實現(xiàn),自定義 filter 代碼如下:
@Component public class UriHostPlaceholderFilter extends AbstractGatewayFilterFactory<UriHostPlaceholderFilter.Config> { @Autowired private RouteFilterRepository routeFilterRepository; public UriHostPlaceholderFilter() { super(Config.class); } @Override public List<String> shortcutFieldOrder() { return Collections.singletonList("order"); } @Override public GatewayFilter apply(Config config) { return new OrderedGatewayFilter((exchange, chain) -> { String code = exchange.getRequest().getHeaders().getOrDefault("code", new ArrayList<>()).stream().findFirst().orElse(""); String routeId = exchange.getAttribute(GATEWAY_PREDICATE_MATCHED_PATH_ROUTE_ID_ATTR); if (StringUtils.hasText(code)) { String newurl; try { newurl = routeFilterRepository.findByRouteIdAndCode(routeId, code).toFuture().get().getUrl(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } if (StringUtils.hasText(exchange.getRequest().getURI().getQuery())) { newurl = newurl + "?" + exchange.getRequest().getURI().getQuery(); } URI newUri = null; try { newUri = new URI(newurl); } catch (URISyntaxException e) { log.error("uri error", e); } exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newUri); } return chain.filter(exchange); }, config.getOrder()); } @Data @NoArgsConstructor public static class Config { private int order; public Config(int order) { this.order = order; } } }
通過擴展 AbstractGatewayFilterFactory 類,我們自定義了 UriHostPlaceholderFilter 這個 filter 。
代碼的核心邏輯在 apply 方法中。
首先通過 String code = exchange.getRequest().getHeaders().getOrDefault("code", new ArrayList<>()).stream().findFirst().orElse("")
可以獲取到接口請求 header 中 code 這個參數(shù)的值。
再通過 String routeId = exchange.getAttribute(GATEWAY_PREDICATE_MATCHED_PATH_ROUTE_ID_ATTR)
可以獲取到 routeId 。
最后通過 newurl = routeFilterRepository.findByRouteIdAndCode(routeId, code).toFuture().get().getUrl()
就可以從數(shù)據(jù)庫中讀取到配置好的后端服務(wù)地址。
拿到后端服務(wù)地址后, 通過調(diào)用 exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newUri);
將請求轉(zhuǎn)發(fā)到對應(yīng)的地址。
六、單元測試
在單元測試代碼中,我們預(yù)置了一條后端服務(wù)動態(tài)配置數(shù)據(jù):
insert into ROUTE_FILTER_ENTITY values('1','routeOne','alpha','http://httpbin.org/anything')
然后模擬請求 /route1/test?a=test
這個接口,根據(jù)我們的配置,請求會被轉(zhuǎn)發(fā)到 http://httpbin.org/anything
。
執(zhí)行單元測試后,可以從日志中發(fā)現(xiàn),接口返回的數(shù)據(jù)是 http://httpbin.org/anything 這個后端服務(wù)返回的數(shù)據(jù)。
當我們希望調(diào)整后端服務(wù)地址時,只需要把 ROUTE_FILTER_ENTITY 表中的這條配置數(shù)據(jù)中的 url 字段改成其它的任何服務(wù)地址即可,大大增加了程序的靈活度。
到此這篇關(guān)于SpringCloud Gateway動態(tài)轉(zhuǎn)發(fā)后端服務(wù)實現(xiàn)過程講解的文章就介紹到這了,更多相關(guān)SpringCloud Gateway動態(tài)轉(zhuǎn)發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Java list.remove( )方法需要注意的兩個坑
這篇文章主要介紹了淺談Java list.remove( )方法需要注意的兩個坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12Java集合之Comparable和Comparator接口詳解
Java提供了Comparable接口與Comparator接口,它們?yōu)閿?shù)組或集合中的元素提供了排序邏輯,實現(xiàn)此接口的對象數(shù)組或集合可以通過Arrays.sort或Collections.sort進行自動排序。本文將通過示例講講它們的使用,需要的可以參考一下2022-12-12java向數(shù)據(jù)庫插入數(shù)據(jù)顯示亂碼的幾種問題解決
這篇文章主要給大家介紹了關(guān)于java向數(shù)據(jù)庫插入數(shù)據(jù)顯示亂碼問題的解決方案,文章分別羅列了前臺亂碼的問題、前臺先后臺插入數(shù)據(jù)后臺接收到的數(shù)據(jù)是亂碼以及后臺向數(shù)據(jù)庫插入數(shù)據(jù)是亂碼等幾種情況,需要的朋友可以參考下2021-11-11Javaweb實現(xiàn)完整個人博客系統(tǒng)流程
這篇文章主要介紹了怎樣用Java來實現(xiàn)一個完整的個人博客系統(tǒng),我們通過實操上手的方式可以高效的鞏固所學(xué)的基礎(chǔ)知識,感興趣的朋友一起來看看吧2022-03-03Spring @Profile注解實現(xiàn)多環(huán)境配置
這篇文章主要介紹了Spring @Profile注解實現(xiàn)多環(huán)境配置,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Java?Chassis3熔斷機制的改進路程技術(shù)解密
這篇文章主要介紹了Java?Chassis?3技術(shù)解密之熔斷機制的改進路程實例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01