欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年02月19日 09:54:39   作者:稀土掘金  
這篇文章主要介紹了SpringCloud Gateway 利用 Mysql 實(shí)現(xiàn)動(dòng)態(tài)路由的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

需求描述

標(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)文章

最新評(píng)論