SpringCloud Gateway 利用 Mysql 實(shí)現(xiàn)動(dòng)態(tài)路由的方法
需求描述
標(biāo)準(zhǔn)網(wǎng)關(guān)動(dòng)態(tài)路由功能是重要的一環(huán),將路由、斷言以及過(guò)濾器信息,持久化到 Mysql 中,通過(guò)配置后臺(tái)頁(yè)面實(shí)現(xiàn)路由、斷言、以及過(guò)濾器等配置的增刪改查。
Spring Cloud Gateway 路由及黑白名單實(shí)現(xiàn)背景 Spring Cloud 路由API
Spring Cloud Gateway 通過(guò)定義 RouteDefinitionRepository 來(lái)實(shí)現(xiàn)動(dòng)態(tài)路由.
//保存路由緩存 public interface RouteDefinitionWriter { Mono<Void> save(Mono<RouteDefinition> route); Mono<Void> delete(Mono<String> routeId); }
//獲取路由緩存 public interface RouteDefinitionLocator { Flux<RouteDefinition> getRouteDefinitions(); }
Spring Cloud 配置文件路由加載方式
public class PropertiesRouteDefinitionLocator implements RouteDefinitionLocator { private final GatewayProperties properties; public PropertiesRouteDefinitionLocator(GatewayProperties properties) { this.properties = properties; } @Override public Flux<RouteDefinition> getRouteDefinitions() { return Flux.fromIterable(this.properties.getRoutes()); } }
Spring Cloud 黑白名 FilterFactory
利用 Spring Cloud Gateway 聲明的一個(gè)工廠接口 GatewayFilterFactory, 定義 黑白名單過(guò)濾器
BlacklistGatewayFilterFactory 類圖
WhitelistGatewayFilterFactory 類圖
動(dòng)態(tài)路由設(shè)計(jì) Spring Cloud Gateway 路由實(shí)體類
Spring Cloud Gateway 通過(guò)定義 RouteDefinition 類裝載路由信息。
package org.springframework.cloud.gateway.route; public class RouteDefinition { //路由 ID @NotEmpty private String id = UUID.randomUUID().toString(); //斷言數(shù)組 @NotEmpty @Valid private List<PredicateDefinition> predicates = new ArrayList<>(); //過(guò)濾器數(shù)組 @Valid private List<FilterDefinition> filters = new ArrayList<>(); // 路由地址 @NotNull private URI uri; // 路由順序 private int order = 0; }
數(shù)據(jù)庫(kù)設(shè)計(jì)
路由表
drop table if exists gateway_route_t; create table if not exists gateway_route_t ( ID int auto_increment primary key, ROUTE_ID varchar(255) not null comment '路由ID', ROUTE_ORDER int default 0 null comment '路由順序', URI varchar(255) not null comment '路由路徑', VALID int default 1 not null comment '是否有效:0-無(wú)效,1-有效', CREATE_USER varchar(200) null comment '創(chuàng)建人', CREATE_TIME datetime null comment '創(chuàng)建時(shí)間', UPDATE_USER varchar(200) null comment '修改人', UPDATE_TIME datetime null comment '修改時(shí)間', constraint idx_ROUTE_ID_index unique (ROUTE_ID) ) comment '網(wǎng)關(guān)路由信息表' charset = utf8;
路由參數(shù)表
drop table if exists gateway_route_param_t; create table if not exists gateway_route_param_t ( ID int auto_increment primary key, ROUTE_ID varchar(255) not null comment '路由ID', PARAM_NAME varchar(255) not null comment '參數(shù)name', PARAM_KEY varchar(255) not null comment '參數(shù) key', PARAM_VALUE varchar(255) not null comment '參數(shù) value', TYPE int not null comment '參數(shù)類型,1為 predicate,2為過(guò) filter', VALID int default 1 not null comment '是否有效:0-無(wú)效,1-有效', CREATE_USER varchar(200) null comment '創(chuàng)建人', CREATE_TIME datetime null comment '創(chuàng)建時(shí)間', UPDATE_USER varchar(200) null comment '修改人', UPDATE_TIME datetime null comment '修改時(shí)間' ) comment '網(wǎng)關(guān)路由參數(shù)表' charset = utf8; create index idx_route_id on gateway_route_param_t (ROUTE_ID);
接口設(shè)計(jì) 接口定義
路由表配置接口
封裝 gateway_route_t dao 層接口.
/** * <功能描述> 路由表接口 * * @author 20024322 * @date 2020/12/24 13:20 */ public interface IRouteConfigService extends IService<RouteDomain>
路由參數(shù)表配置接口
封裝 gateway_route_param_t dao 層接口.
/** * <功能描述> 路由參數(shù)表接口 * * @author 20024322 * @date 2020/12/24 13:20 */ public interface IRouteParamConfigService extends IService<RouteParamDomain>
數(shù)據(jù)庫(kù)路由服務(wù)接口
封裝 路由表配置服務(wù)接口以及路由參數(shù)表配置接口, 對(duì)外層提供對(duì)數(shù)據(jù)庫(kù)路由信息的操作.
/** * <功能描述> 數(shù)據(jù)庫(kù)路由服務(wù) * * @author 20024322 * @date 2020/12/24 13:20 */ public interface IRoutePropertiesService
網(wǎng)關(guān)路由緩存接口
封裝 RouteDefinitionRepository 接口,對(duì)外提供對(duì)網(wǎng)關(guān)路由緩存的刷新.
/** * <功能描述> 網(wǎng)關(guān)緩存路由服務(wù) * * @author 20024322 * @date 2020/12/24 13:20 */ public interface IGatewayRouteService extends ApplicationEventPublisherAware
路由事件監(jiān)聽(tīng)接口
配置需要監(jiān)聽(tīng)路由變化的 service 實(shí)現(xiàn)
/** * <功能描述> 路由事件監(jiān)聽(tīng)接口 * * @author 20024322 * @date 2020/12/24 13:20 */ public interface RouteEventListener extends ApplicationListener<RefreshCacheEvent>
數(shù)據(jù)庫(kù)黑白名單配置接口
/** * <功能描述> API Filter 接口定義 * * @author 20024322 * @date 2020/12/24 13:20 */ public interface IApiFilterService
網(wǎng)關(guān)白名單緩存接口
提供指定路由 API 白名單check 監(jiān)聽(tīng)路由事件
/** * <功能描述> API 白名單緩存接口 * * @author 20024322 * @date 2020/12/24 13:20 */ public interface IApiCacheService extends RouteEventListener
路由參數(shù)執(zhí)行校驗(yàn)接口
封裝 提供路由參數(shù)的校驗(yàn)的接口.
/** * <功能描述> 路由參數(shù)校驗(yàn) * * @author 20024322 * @date 2020/12/24 13:20 */ public interface IRouteValidateExecutorService
接口類圖 路由及黑白名單類圖
斷言及過(guò)濾器封裝類圖
集群緩存刷新事件處理策略類圖
路由初始化設(shè)計(jì) 重載 PropertiesRouteDefinitionLocator
/** * <功能描述> 重寫(xiě) PropertiesRouteDefinitionLocator bean * 將配置文件中的路由信息通過(guò) MysqlRouteConfig 載入。 * * @author 20024322 * @date 2020/12/24 13:20 */ @Configuration @AutoConfigureBefore({MysqlRouteConfig.class}) public class PropertiesRouteConfig { @Bean public PropertiesRouteDefinitionLocator propertiesRouteDefinitionLocator( GatewayProperties properties) { return new PropertiesRouteDefinitionLocator(new GatewayProperties()); } }
定義 initMysqlRouteDefinition Bean 加載數(shù)據(jù)庫(kù)及配置文件的路由配置
/** * <功能描述> 從Mysql中初始化路由信息 * 覆蓋配置文件中的路由信息 * * @author 20024322 * @date 2020/12/24 13:20 */ @Configuration public class MysqlRouteConfig { private final IRoutePropertiesService routePropertiesService; private final IGatewayRouteService gatewayRouteService; public MysqlRouteConfig(IRoutePropertiesService routePropertiesService, IGatewayRouteService gatewayRouteService) { this.routePropertiesService = routePropertiesService; this.gatewayRouteService = gatewayRouteService; } /** * 初始化 gatewayProperties 中的 route * * @param gatewayProperties * @return */ @Bean public List<RouteDefinition> initMysqlRouteDefinition(GatewayProperties gatewayProperties) { List<RouteDefinition> gatewayPropertiesRoutes = gatewayProperties.getRoutes(); //初始化數(shù)據(jù)庫(kù)路由信息 List<RouteDefinition> routeDefinitionList = routePropertiesService.getRouteDefinitionList(); if (CollectionUtils.isEmpty(gatewayProperties.getRoutes()) && CollectionUtils.isEmpty(routeDefinitionList)) { throw new BizBaseException(HprmcExceptionCode.ROUTE_NOT_FOUND); } Set<String> routeIds = routeDefinitionList.stream() .map(RouteDefinition::getId).collect(Collectors.toSet()); if (gatewayPropertiesRoutes.stream().anyMatch(r -> routeIds.contains(r.getId()))) { throw new BizBaseException(HprmcExceptionCode.ROUTE_INIT_CONFLICT); } //將配置文件中的路由信息添加到 InMemoryRouteDefinitionRepository 成員變量中 if (!CollectionUtils.isEmpty(gatewayPropertiesRoutes)) { gatewayPropertiesRoutes.forEach(gatewayRouteService::addInMemoryRouteRefresh); } //寫(xiě)到 InMemoryRouteDefinitionRepository 成員初始化緩存 if (!CollectionUtils.isEmpty(routeDefinitionList)) { routeDefinitionList.forEach(gatewayRouteService::addInMemoryRouteRefresh); } return routeDefinitionList; } }
到此這篇關(guān)于SpringCloud Gateway 利用 Mysql 實(shí)現(xiàn)動(dòng)態(tài)路由的方法的文章就介紹到這了,更多相關(guān)SpringCloud Gateway 實(shí)現(xiàn)動(dòng)態(tài)路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成I18n國(guó)際化文件在jar包外生效問(wèn)題
這篇文章主要介紹了SpringBoot集成I18n國(guó)際化文件在jar包外生效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04Spring?Boot項(xiàng)目中使用?TrueLicense?生成和驗(yàn)證License的詳細(xì)步驟
這篇文章主要介紹了Spring?Boot項(xiàng)目中使用?TrueLicense?生成和驗(yàn)證License,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10fastjson全局日期序列化設(shè)置導(dǎo)致JSONField失效問(wèn)題解決方案
這篇文章主要介紹了fastjson通過(guò)代碼指定全局序列化返回時(shí)間格式,導(dǎo)致使用JSONField注解標(biāo)注屬性的特殊日期返回格式失效問(wèn)題的解決方案2023-01-01SpringCloud Ribbon負(fù)載均衡工具使用
Ribbon是Netflix的組件之一,負(fù)責(zé)注冊(cè)中心的負(fù)載均衡,有助于控制HTTP和TCP客戶端行為。Spring?Cloud?Netflix?Ribbon一般配合Ribbon進(jìn)行使用,利用在Eureka中讀取的服務(wù)信息,在調(diào)用服務(wù)節(jié)點(diǎn)時(shí)合理進(jìn)行負(fù)載2023-02-02解讀controller層,service層,mapper層,entity層的作用與聯(lián)系
這篇文章主要介紹了關(guān)于controller層,service層,mapper層,entity層的作用與聯(lián)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11arthas?jprofiler做復(fù)雜鏈路的調(diào)用分析
這篇文章主要為大家介紹了arthas?jprofiler做復(fù)雜鏈路的調(diào)用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Springboot自動(dòng)裝配實(shí)現(xiàn)過(guò)程代碼實(shí)例
這篇文章主要介紹了Springboot自動(dòng)裝配實(shí)現(xiàn)過(guò)程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06詳解mybatis批量插入10萬(wàn)條數(shù)據(jù)的優(yōu)化過(guò)程
這篇文章主要介紹了詳解mybatis批量插入10萬(wàn)條數(shù)據(jù)的優(yōu)化過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Java開(kāi)發(fā)或調(diào)用WebService的幾種方式總結(jié)
java開(kāi)發(fā)過(guò)程中,很多地方都會(huì)遇到數(shù)據(jù)傳遞,遠(yuǎn)程獲取數(shù)據(jù)問(wèn)題,這篇文章主要介紹了Java開(kāi)發(fā)或調(diào)用WebService的幾種方式的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06