每天多学一点点~
话不多说,这就开始吧…
上篇写过 Sentinel规则持久化改造(pull+push+Ahas),但一般springcloud项目都是与zuul,gateway等网关结合使用,今儿总结下 sentinel 与 gateway的持久化改造。
以sentinel 1.7.0 版本 为例 sentinel git地址
com.alibaba.csp
sentinel-datasource-nacos
修改 Converter 在上篇基础上,加入网关的API和FlowRule GetWayApiNacosPublisher 推送API GateWayFlowRulesNacosProvider 拉取规则 GateWayFlowRulesNacosPunlisher 推送规则 其他 Mapping 同理 此时,GatewayFlowRuleController 已经改造完毕。喜大普奔23333 启动 订单微服务工程(啥也没有,就是一个接口查db) 和网关微服务工程,启动sentinel源码 地址 http://localhost:9999/#/login 先访问一次网关接口 http://localhost:8888/product/selectProductInfoById/1 http://localhost:9999/#/dashboard (sentinel 控制台) 配置流控 多次访问 网关接口被限流了 最后,重启 gateway or sentinel 规则还在~ 在nacos或者sentinel控制台 改规则,两边都能同步 大功告成~ 世上无难事,只怕有心人,每天积累一点点,fighting!!!
@Configuration
public class NacosConfig {
/**
* 流控规则
*
* @return
*/
@Bean
public Converter
, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter
, String> authorRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter
, String> degradeRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter
, String> paramRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter
, String> systemRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter
, String> apiDefinitionEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter
, String> gatewayFlowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter
2.3 新增 pulisher 推送规则 和 provider 拉取规则
**GetWayApiNacosProvider 拉取API **@Component("getWayApiNacosProvider")
public class GetWayApiNacosProvider implements DynamicRuleProvider
> {
@Autowired
private ConfigService configService;
@Autowired
private Converter
@Component("getWayApiNacosPublisher")
public class GetWayApiNacosPublisher implements DynamicRulePublisher
> {
@Autowired
private ConfigService configService;
@Autowired
private Converter
, String> converter;
@Override
public void publish(String app, List
@Component("gateWayFlowRulesNacosProvider")
public class GateWayFlowRulesNacosProvider implements DynamicRuleProvider
> {
@Autowired
private ConfigService configService;
@Autowired
private Converter
@Component("gateWayFlowRulesNacosPunlisher")
public class GateWayFlowRulesNacosPunlisher implements DynamicRulePublisher
> {
@Autowired
private ConfigService configService;
@Autowired
private Converter
, String> converter;
@Override
public void publish(String app, List
2.4 修改GatewayFlowRuleController 和 GatewayApiController
//添加我们自己写的ruleProvider
@Autowired
@Qualifier("gateWayFlowRuleNacosProvider")
private DynamicRuleProvider
> ruleProvider;
//添加我们自己写的 publisher
@Autowired
@Qualifier("gateWayFlowRuleNacosPubisher")
private DynamicRulePublisher
> rulePublisher;
// 原生方法 拉取网关微服务
// List
GatewayApiController 类
/**
* api 推送 到 nacos
* @param app
* @throws Exception
*/
private void publishApis(String app) throws Exception {
List
entity = repository.save(entity); // 保存到内存中
// 推送给微服务
// publishRules(entity.getApp(), entity.getIp(), entity.getPort());
// 现通过 自己的publisher推送到配置中心
publishRules(entity.getApp());
public final class NacosConfigUtil {
public static final String GROUP_ID = "SENTINEL_GROUP";
public static final String FLOW_DATA_ID_POSTFIX = "-flow-rules";
public static final String DEGRAD_DATA_ID_POSTFIX = "-degrade-rules";
public static final String AUTH_DATA_ID_POSTFIX = "-authority-rules";
public static final String SYS_DATA_ID_POSTFIX = "-system-rules";
public static final String PARAM_FLOW_DATA_ID_POSTFIX = "-param-rules";
public static final String CLUSTER_MAP_DATA_ID_POSTFIX = "-cluster-map";
public static final String GETWAY_API_DATA_ID_POSTFIX = "-getway-api";
public static final String GETWAY_FLOW_DATA_ID_POSTFIX = "-getway-flow";
/**
* cc for `cluster-client`
*/
public static final String CLIENT_CONFIG_DATA_ID_POSTFIX = "-cc-config";
/**
* cs for `cluster-server`
*/
public static final String SERVER_TRANSPORT_CONFIG_DATA_ID_POSTFIX = "-cs-transport-config";
public static final String SERVER_FLOW_CONFIG_DATA_ID_POSTFIX = "-cs-flow-config";
public static final String SERVER_NAMESPACE_SET_DATA_ID_POSTFIX = "-cs-namespace-set";
private NacosConfigUtil() {}
}
2.5 修改端口
3. 微服务网关改造
# 新增持久化配置
server:
port: 8888
spring:
application:
name: gateway-sentinel
cloud:
gateway:
discovery:
locator:
lower-case-service-id: true
enabled: true
routes:
- id: product_center
uri: lb://product-center
predicates:
- Path=/product/**
- id: order_center
uri: lb://order-center
predicates:
- Path=/order/**
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:9999
datasource:
# 名称随意
gw-flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-gateway-flow # 在修改的sentinel 源码中定义的规则名
groupId: SENTINEL_GROUP
rule-type: gw-flow
gw-api-group:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-gateway-api # 在修改的sentinel 源码中定义的规则名
groupId: SENTINEL_GROUP
rule-type: gw-api-group
main:
allow-bean-definition-overriding: true
4. 持久化测试
继续多次刷网关接口,显示被限流5.结语