springcloud mysql集群_SpringCloud Gateway 利用 Mysql 实现动态路由的方法

需求描述

标准网关动态路由功能是重要的一环,将路由、断言以及过滤器信息,持久化到 Mysql 中,通过配置后台页面实现路由、断言、以及过滤器等配置的增删改查。

Spring Cloud Gateway 路由及黑白名单实现背景 Spring Cloud 路由API

0bab4b279ac0a3ccdc599533d16681c0.png

Spring Cloud Gateway 通过定义 RouteDefinitionRepository 来实现动态路由.

//保存路由缓存

public interface RouteDefinitionWriter {

Mono save(Mono route);

Mono delete(Mono routeId);

}

//获取路由缓存

public interface RouteDefinitionLocator {

Flux getRouteDefinitions();

}

Spring Cloud 配置文件路由加载方式

6629735a77d6c957129f9c287729fb51.png

public class PropertiesRouteDefinitionLocator implements RouteDefinitionLocator {

private final GatewayProperties properties;

public PropertiesRouteDefinitionLocator(GatewayProperties properties) {

this.properties = properties;

}

@Override

public Flux getRouteDefinitions() {

return Flux.fromIterable(this.properties.getRoutes());

}

}

Spring Cloud 黑白名 FilterFactory

利用 Spring Cloud Gateway 声明的一个工厂接口 GatewayFilterFactory, 定义 黑白名单过滤器

BlacklistGatewayFilterFactory 类图

194baf9aabf8e15c32a8e163b462bbbb.png

WhitelistGatewayFilterFactory 类图

d65a6ca79abc07c5aae145ab37096a4c.png

动态路由设计 Spring Cloud Gateway 路由实体类

Spring Cloud Gateway 通过定义 RouteDefiniti

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值