网站举报查询进度,嵊州市网站建设,网站做3年3年包括什么,非响应式网站改响应式背景
Sentinel Dashboard 默认将限流、熔断等规则保存在内存中#xff0c;然后同步给连接 Dashboard 的客户端#xff0c;客户端也是保存在内存中。 那么如果当 Sentinel Dashboard 异常重启#xff0c;那么之前配置的规则将全部丢失#xff0c;需要重新进行配置。 其中然后同步给连接 Dashboard 的客户端客户端也是保存在内存中。 那么如果当 Sentinel Dashboard 异常重启那么之前配置的规则将全部丢失需要重新进行配置。 其中Sentinel 本身已经提供第三方组件作为动态数据源如Apollo、Nacos、Zookeeper。 因此我们可以对 Sentinel Dashboard 进行改造将规则存储在上述的中间件中保证 Dashboard 重启不会丢失原有的所有规则配置。
改造
下载源码
因为我们需要对 Sentinel Dashboard 进行改造所以需先下载源码下来这里选择的版本为1.8.6 下载源码后我们可以发现在test下是有 Apollo、Nacos 和 Zookeeper 作为数据源的代码因此我们可以进行借鉴复用。 因为我们的微服务架构是Spring Cloud Alibaba 系列的所以注册中心和配置中心都是使用的 Nacos所以下面将基于 Nacos 去改造 Sentinel Dashboard。
Sentinel Dashboard 接入 Nacos
test 中Nacos 是通过调用 ConfigService 去查询规则和保存规则虽然单元测试中代码仅仅是流控规则的代码但是我们完全可以借鉴毕竟现在仅仅是改造规则的保存和查询逻辑整体方式都是一致的仅仅需要找到其他规则所在代码然后做替换即可。
自定义 DynamicRuleProvider DynamicRulePulisher
DynamicRuleProvider
用于查询规则我们借鉴 test 代码中的com.alibaba.csp.sentinel.dashboard.rule.nacos.FlowRuleNacosProvider即可为所有规则都实现一遍核心逻辑就是使用 Nacos 提供的ConfigService进行查询。
DynamicRulePublisher
用于新增/更新/删除规则我们借鉴 test 代码中的com.alibaba.csp.sentinel.dashboard.rule.nacos.FlowRuleNacosPublisher即可为所有规则都实现一遍核心逻辑就是使用 Nacos 提供的ConfigService进行推送。
实现
由于我们仅仅调整规则的保存/更新/删除和查询逻辑其余业务逻辑不调整所以不管是那种规则调整的逻辑都是一致的可能不一样的仅仅是以下两点
保存在 Nacos 中不同规则的dataId不一样从 Nacos 查询后转化的实体对象不一致
因此我们这里针对 Provider 和 Publisher 都抽象一个类实现核心的保存逻辑和更新逻辑。不同规则只需要继承对应的抽象类然后具体对应的规则dataId和查询实体泛型。 NacosConfigUtil
/*** author Eric Zhao* since 1.4.0*/
public final class NacosConfigUtil {public static final String GROUP_ID SENTINEL_GROUP;// 保存到nacos中的文件后缀public static final String FLOW_DATA_ID_POSTFIX -flow-rules;public static final String DEGRADE_DATA_ID_POSTFIX -degrade-rules;public static final String SYSTEM_DATA_ID_POSTFIX -system-rules;public static final String AUTHORITY_DATA_ID_POSTFIX -authority-rules;public static final String PARAM_FLOW_DATA_ID_POSTFIX -param-flow-rules;public static final String GATEWAY_FLOW_DATA_ID_POSTFIX -gw-flow-rules;public static final String GATEWAY_API_FLOW_DATA_ID_POSTFIX -gw-api-group-rules;//public static final String PARAM_FLOW_DATA_ID_POSTFIX -param-rules;public static final String CLUSTER_MAP_DATA_ID_POSTFIX -cluster-map;/*** 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() {}
}NacosRuleProvider
/*** Nacos RuleProvider 抽象类* author winfun* since 2023/9/29**/
public abstract class NacosRuleProviderT extends RuleEntity implements DynamicRuleProviderListT {Autowiredprotected ConfigService configService;Autowiredprotected ConverterString, ListT converter;Overridepublic ListT getRules(String appName) throws Exception {String rules configService.getConfig(appName getDataIdPostfix(),NacosConfigUtil.GROUP_ID, 3000);if (StringUtil.isEmpty(rules)) {return new ArrayList();}return converter.convert(rules);}/*** dataId 后缀参考 com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil* return dataId 后缀*/public abstract String getDataIdPostfix();
}NacosRulePublisher
/*** Nacos RulePublisher 抽象类* author winfun* since 2023/9/29**/
public abstract class NacosRulePublisherT extends RuleEntity implements DynamicRulePublisherListT {Autowiredprotected ConfigService configService;Autowiredprotected ConverterListT, String converter;Overridepublic void publish(String app, ListT rules) throws Exception {AssertUtil.notEmpty(app, app name cannot be empty);if (rules null) {return;}configService.publishConfig(app getDataIdPostfix(),NacosConfigUtil.GROUP_ID, converter.convert(rules));}/*** dataId 后缀参考 com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil* return dataId 后缀*/public abstract String getDataIdPostfix();
}后续不同的规则分别创建子类继承上面的抽象类即可。 这里拿网关做例子
/*** 网关 RulePublisher* author winfun* since 2023/9/29**/
Component(nacosGatewayFlowRulePublisher)
public class NacosGatewayFlowRulePublisher extends NacosRulePublisherGatewayFlowRuleEntity {Overridepublic String getDataIdPostfix() {return NacosConfigUtil.GATEWAY_FLOW_DATA_ID_POSTFIX;}
}/*** 网关 RuleProvider* author winfun* since 2023/9/29**/
Component(nacosGatewayFlowRuleProvider)
public class NacosGatewayFlowRuleProvider extends NacosRuleProviderGatewayFlowRuleEntity {Overridepublic String getDataIdPostfix() {return NacosConfigUtil.GATEWAY_FLOW_DATA_ID_POSTFIX;}
}Controller 接口调整
上面我们仅仅为各种规则的保存和查询创建了对应的 RulePublisher 和 RuleProvider我们还需要对页面的接口进行调整将接口中保存和查询规则的代码调整。 改动逻辑将代码中使用 SentinelApiClient 进行规则查询和保存的逻辑代码改为 Nacos 对应的 Provider 和 Publisher 其中对应关系
GatewayApiController使用 nacosApiDefinitionProvider 和 nacosApiDefinitionPublisherGatewayFlowRuleController使用 nacosGatewayFlowRuleProvider 和 nacosGatewayFlowRulePublisherFlowControllerV2使用 nacosFlowRuleProvider 和 nacosFlowRulePublisherAuthorityRuleController使用 authorityRuleNacosProvider 和 authorityRuleNacosPublisherDegradeController使用 nacosDegradeRuleProvider 和 nacosDegradeRulePublisherParamFlowRuleController使用 nacosParamFlowRuleProvider 和 nacosParamFlowRulePublisherSystemController使用 nacosSystemRuleProvider 和 nacosSystemRulePublisher
以 GatewayFlowRuleController 为例子 注释掉 SentinelApiClient引入对应 Nacos 的 RulePublisher 和 RuleProvider 查询代码调整 新增/更新/删除代码调整其实就是更新推送规则的逻辑
启动应用
添加 Nacos 配置
nacos.serverAddr127.0.0.1:8848
nacos.namespace1ee594f6-001f-4d68-9852-ca6fce6039c9groupId默认使用 SENTINEL_GROUP配置在 NacosConfigUtil 中 启动 Spring 应用 应用启动后由于还没有 Sentinel 客户端接入因此登录 Web 端后是没有东西可以显示的。
Sentinel 客户端接入
这里用 Zuul 网关服务作为例子。
Zuul 网关接入 Sentinel
引入Maven依赖
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-zuul-adapter/artifactId
/dependency
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-transport-simple-http/artifactId
/dependency
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId
/dependency
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-sentinel-gateway/artifactId
/dependency!-- 动态数据源 --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-sentinel-datasource/artifactId
/dependency
!-- nacos 数据源 --
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactId
/dependency添加配置
spring:cloud:nacos:server-addr: 127.0.0.1:8848password: nacosusername: nacosdiscovery:namespace: 1ee594f6-001f-4d68-9852-ca6fce6039c9enabled: trueconfig:namespace: 1ee594f6-001f-4d68-9852-ca6fce6039c9enabled: falsefile-extension: ymlsentinel:filter:enabled: truetransport:port: 8719dashboard: 127.0.0.1:18181datasource:# 限流flow:nacos:server-addr: ${spring.cloud.nacos.server-addr}namespace: ${spring.cloud.nacos.discovery.namespace}dataId: ${spring.application.name}-flow-rulesgroupId: SENTINEL_GROUPdata-type: jsonrule-type: flow# 熔断degrade:nacos:server-addr: ${spring.cloud.nacos.server-addr}namespace: ${spring.cloud.nacos.discovery.namespace}dataId: ${spring.application.name}-degrade-rulesgroupId: SENTINEL_GROUPdata-type: jsonrule-type: degrade自定义降级Provider
/*** CustomZuulBlockFallbackProvider* author winfun* since 2023/10/3**/
Slf4j
Component
public class CustomZuulBlockFallbackProvider implements ZuulBlockFallbackProvider {Overridepublic String getRoute() {return *;}Overridepublic BlockResponse fallbackResponse(String route, Throwable cause) {RequestContext ctx RequestContext.getCurrentContext();HttpServletRequest request ctx.getRequest();String url request.getRequestURL().toString();if (cause instanceof FlowException) {log.warn(url:{} 触发限流,url);return new BlockResponse(429, Sentinel触发限流, route);} else if (cause instanceof DegradeException) {log.warn(url:{} 触发熔断,url);return new BlockResponse(429, Sentinel触发熔断, route);} else {return new BlockResponse(500, 系统异常, route);}}
}启动应用并随便调用一个接口调用后 Sentinel 客户端就会将相关信息上报给 Sentinel Dashboard
设置 Route 类型的限流规则和熔断规则
在 Sentinel Dashboard 新增限流规则和熔断规则 Nacos 在对应的 namespace 中也会出现对应的配置 调用报错的接口当调用第一次失败后第二次就会触发熔断规则