国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法

SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法

2021-08-10 10:44稀土掘金 Java教程

這篇文章主要介紹了SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

需求描述

標準網(wǎng)關(guān)動態(tài)路由功能是重要的一環(huán),將路由、斷言以及過濾器信息,持久化到 Mysql 中,通過配置后臺頁面實現(xiàn)路由、斷言、以及過濾器等配置的增刪改查。

Spring Cloud Gateway 路由及黑白名單實現(xiàn)背景 Spring Cloud 路由API

SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法

Spring Cloud Gateway 通過定義 RouteDefinitionRepository 來實現(xiàn)動態(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 配置文件路由加載方式

SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法

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 聲明的一個工廠接口 GatewayFilterFactory, 定義 黑白名單過濾器

BlacklistGatewayFilterFactory 類圖

SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法

WhitelistGatewayFilterFactory 類圖

SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法

動態(tài)路由設(shè)計 Spring Cloud Gateway 路由實體類

Spring Cloud Gateway 通過定義 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<>();

  //過濾器數(shù)組
  @Valid
  private List<FilterDefinition> filters = new ArrayList<>();

  // 路由地址
  @NotNull
  private URI uri;

  // 路由順序
  private int order = 0;
}

數(shù)據(jù)庫設(shè)計

路由表

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-無效,1-有效",
  CREATE_USER varchar(200) null comment "創(chuàng)建人",
  CREATE_TIME datetime   null comment "創(chuàng)建時間",
  UPDATE_USER varchar(200) null comment "修改人",
  UPDATE_TIME datetime   null comment "修改時間",
  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為過 filter",
  VALID    int default 1 not null comment "是否有效:0-無效,1-有效",
  CREATE_USER varchar(200) null comment "創(chuàng)建人",
  CREATE_TIME datetime   null comment "創(chuàng)建時間",
  UPDATE_USER varchar(200) null comment "修改人",
  UPDATE_TIME datetime   null comment "修改時間"
) comment "網(wǎng)關(guān)路由參數(shù)表" charset = utf8;

create index idx_route_id on gateway_route_param_t (ROUTE_ID);

接口設(shè)計 接口定義

路由表配置接口

封裝 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ù)庫路由服務(wù)接口

封裝 路由表配置服務(wù)接口以及路由參數(shù)表配置接口, 對外層提供對數(shù)據(jù)庫路由信息的操作.

/**
 * <功能描述> 數(shù)據(jù)庫路由服務(wù)
 *
 * @author 20024322
 * @date 2020/12/24 13:20
 */
public interface IRoutePropertiesService

網(wǎng)關(guān)路由緩存接口

封裝 RouteDefinitionRepository 接口,對外提供對網(wǎng)關(guān)路由緩存的刷新.

/**
 * <功能描述> 網(wǎng)關(guān)緩存路由服務(wù)
 *
 * @author 20024322
 * @date 2020/12/24 13:20
 */
public interface IGatewayRouteService extends ApplicationEventPublisherAware

路由事件監(jiān)聽接口

配置需要監(jiān)聽路由變化的 service 實現(xiàn)

/**
 * <功能描述> 路由事件監(jiān)聽接口
 *
 * @author 20024322
 * @date 2020/12/24 13:20
 */
public interface RouteEventListener extends ApplicationListener<RefreshCacheEvent>

數(shù)據(jù)庫黑白名單配置接口

/**
 * <功能描述> API Filter 接口定義
 *
 * @author 20024322
 * @date 2020/12/24 13:20
 */
public interface IApiFilterService

網(wǎng)關(guān)白名單緩存接口

提供指定路由 API 白名單check 監(jiān)聽路由事件

/**
 * <功能描述> API 白名單緩存接口
 *
 * @author 20024322
 * @date 2020/12/24 13:20
 */
public interface IApiCacheService extends RouteEventListener

路由參數(shù)執(zhí)行校驗接口

封裝 提供路由參數(shù)的校驗的接口.

/**
 * <功能描述> 路由參數(shù)校驗
 *
 * @author 20024322
 * @date 2020/12/24 13:20
 */
public interface IRouteValidateExecutorService

接口類圖 路由及黑白名單類圖

SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法

斷言及過濾器封裝類圖

SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法

集群緩存刷新事件處理策略類圖

SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法

路由初始化設(shè)計 重載 PropertiesRouteDefinitionLocator

/**
 * <功能描述> 重寫 PropertiesRouteDefinitionLocator bean
 * 將配置文件中的路由信息通過 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ù)庫及配置文件的路由配置

/**
 * <功能描述> 從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ù)庫路由信息
    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);
    }

    //寫到 InMemoryRouteDefinitionRepository 成員初始化緩存
    if (!CollectionUtils.isEmpty(routeDefinitionList)) {
      routeDefinitionList.forEach(gatewayRouteService::addInMemoryRouteRefresh);
    }
    return routeDefinitionList;
  }
}

到此這篇關(guān)于SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法的文章就介紹到這了,更多相關(guān)SpringCloud Gateway 實現(xiàn)動態(tài)路由內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://juejin.cn/post/6928933330029117448

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 特黄特色大片免费视频观看 | 成人午夜网 | 在线国产一区 | 综合网视频 | 久草中文在线 | 精品日韩一区二区三区 | 久操色 | 91久久精品国产亚洲a∨麻豆 | 精品国产三级 | 黑人xxx视频 | 成年人免费网站 | 欧美大片一区 | 日韩成人在线一区 | 在线观看成人av | 国产一区中文字幕 | 日本久久久久久 | 亚洲成人在线观看视频 | 精品无码久久久久国产 | 亚洲精品日韩在线 | 久久久久久亚洲一区二区三区蜜臀 | 久久久久久久久久久国产 | 日韩在线一区二区三区免费视频 | 一区二区三区 在线 | 色中色综合| 99精品网站| 超碰中文字幕 | 最近的中文字幕在线看视频 | 一级片网址 | 久久久久久久国产精品 | 成人精品动漫一区二区三区 | 精品一区二区三区免费毛片爱 | 欧美一级全黄 | 亚洲午夜电影在线 | 日韩成人免费在线 | 国产精品毛片久久久久久久 | 欧州一区二区三区 | 自拍偷拍专区 | 国产精品久久久久久久久费观看 | 狠狠操狠狠干 | 四虎永久免费影院 | 久久久久999 |