详解微服务之Spring Cloud Alibaba Sentinel,mysql索引面试题索引失效

@FeignClient(name = “service-provider”, fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)

public interface EchoService {

@GetMapping(value = “/echo/{str}”)

String echo(@PathVariable(“str”) String str);

}

class FeignConfiguration {

@Bean

public EchoServiceFallback echoServiceFallback() {

return new EchoServiceFallback();

}

}

class EchoServiceFallback implements EchoService {

@Override

public String echo(@PathVariable(“str”) String str) {

return “echo fallback”;

}

}

Feign 对应的接口中的资源名策略定义:httpmethod:protocol://requesturl。@FeignClient 注解中的所有属性,Sentinel 都做了兼容。

EchoService 接口中方法 echo 对应的资源名为 GET:http://service-provider/echo/{str}

RestTemplate 支持

Spring Cloud Alibaba Sentinel 支持对 RestTemplate 的服务调用使用 Sentinel 进行保护,在构造 RestTemplate bean的时候需要加上 @SentinelRestTemplate 注解。

@Bean

@SentinelRestTemplate(blockHandler = “handleException”, blockHandlerClass = ExceptionUtil.class)

public RestTemplate restTemplate() {

return new RestTemplate();

}

@SentinelRestTemplate 注解的属性支持限流(blockHandlerblockHandlerClass)和降级(fallbackfallbackClass)的处理。

其中 blockHandler 或 fallback 属性对应的方法必须是对应 blockHandlerClass 或 fallbackClass 属性中的静态方法。

该方法的参数跟返回值跟 org.springframework.http.client.ClientHttpRequestInterceptor#interceptor 方法一致,其中参数多出了一个 BlockException 参数用于获取 Sentinel 捕获的异常。

比如上述 @SentinelRestTemplate 注解中 ExceptionUtil 的 handleException 属性对应的方法声明如下:

public class ExceptionUtil {

public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) {

}

}

应用启动的时候会检查 @SentinelRestTemplate 注解对应的限流或降级方法是否存在,如不存在会抛出异常

@SentinelRestTemplate 注解的限流(blockHandlerblockHandlerClass)和降级(fallbackfallbackClass)属性不强制填写。

当使用 RestTemplate 调用被 Sentinel 熔断后,会返回 RestTemplate request block by sentinel 信息,或者也可以编写对应的方法自行处理返回信息。这里提供了 SentinelClientHttpResponse 用于构造返回信息。

Sentinel RestTemplate 限流的资源规则提供两种粒度:

  • httpmethod:schema://host:port/path:协议、主机、端口和路径

  • httpmethod:schema://host:port:协议、主机和端口

以 https://www.taobao.com/test 这个 url 并使用 GET 方法为例。对应的资源名有两种粒度,分别是 GET:https://www.taobao.com 以及 GET:https://www.taobao.com/test

动态数据源支持

SentinelProperties 内部提供了 TreeMap 类型的 datasource 属性用于配置数据源信息。

比如配置 4 个数据源:

spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json

spring.cloud.sentinel.datasource.ds1.file.rule-type=flow

#spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json

#spring.cloud.sentinel.datasource.ds1.file.data-type=custom

#spring.cloud.sentinel.datasource.ds1.file.converter-class=org.springframework.cloud.alibaba.cloud.examples.JsonFlowRuleListConverter

#spring.cloud.sentinel.datasource.ds1.file.rule-type=flow

spring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848

spring.cloud.sentinel.datasource.ds2.nacos.data-id=sentinel

spring.cloud.sentinel.datasource.ds2.nacos.group-id=DEFAULT_GROUP

spring.cloud.sentinel.datasource.ds2.nacos.data-type=json

spring.cloud.sentinel.datasource.ds2.nacos.rule-type=degrade

spring.cloud.sentinel.datasource.ds3.zk.path = /Sentinel-Demo/SYSTEM-CODE-DEMO-FLOW

spring.cloud.sentinel.datasource.ds3.zk.server-addr = localhost:2181

spring.cloud.sentinel.datasource.ds3.zk.rule-type=authority

spring.cloud.sentinel.datasource.ds4.apollo.namespace-name = application

spring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = sentinel

spring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = test

spring.cloud.sentinel.datasource.ds4.apollo.rule-type=param-flow

这种配置方式参考了 Spring Cloud Stream Binder 的配置,内部使用了 TreeMap 进行存储,comparator 为 String.CASE_INSENSITIVE_ORDER 。

d1, ds2, ds3, ds4 是 ReadableDataSource 的名字,可随意编写。后面的 file ,zk ,nacos , apollo 就是对应具体的数据源。它们后面的配置就是这些具体数据源各自的配置。

每种数据源都有两个共同的配置项: data-type、 converter-class 以及 rule-type

data-type 配置项表示 Converter 类型,Spring Cloud Alibaba Sentinel 默认提供两种内置的值,分别是 json 和 xml (不填默认是json)。如果不想使用内置的 json 或 xml 这两种 Converter,可以填写 custom 表示自定义 Converter,然后再配置 converter-class 配置项,该配置项需要写类的全路径名(比如 spring.cloud.sentinel.datasource.ds1.file.converter-class=org.springframework.cloud.alibaba.cloud.examples.JsonFlowRuleListConverter)。

rule-type 配置表示该数据源中的规则属于哪种类型的规则(flowdegradeauthoritysystemparam-flowgw-flowgw-api-group)。

当某个数据源规则信息加载失败的情况下,不会影响应用的启动,会在日志中打印出错误信息。

默认情况下,xml 格式是不支持的。需要添加 jackson-dataformat-xml 依赖后才会自动生效。

关于 Sentinel 动态数据源的实现原理,参考: 动态规则扩展

Zuul 支持

参考 Sentinel 网关限流

若想跟 Sentinel Starter 配合使用,需要加上 spring-cloud-alibaba-sentinel-gateway 依赖,同时需要添加 spring-cloud-starter-netflix-zuul 依赖来让 spring-cloud-alibaba-sentinel-gateway 模块里的 Zuul 自动化配置类生效:

com.alibaba.cloud

spring-cloud-starter-alibaba-sentinel

com.alibaba.cloud

spring-cloud-alibaba-sentinel-gateway

org.springframework.cloud

spring-cloud-starter-gateway

Spring Cloud Gateway 支持

参考 Sentinel 网关限流

若想跟 Sentinel Starter 配合使用,需要加上 spring-cloud-alibaba-sentinel-gateway 依赖,同时需要添加 spring-cloud-starter-gateway 依赖来让 spring-cloud-alibaba-sentinel-gateway 模块里的 Spring Cloud Gateway 自动化配置类生效:

com.alibaba.cloud

spring-cloud-starter-alibaba-sentinel

com.alibaba.cloud

spring-cloud-alibaba-sentinel-gateway

org.springframework.cloud

spring-cloud-starter-gateway

Sentinel 对外暴露的 Endpoint

Sentinel 内部提供了一个 Endpoint, 对应的 endpoint id 为 sentinel

Endpoint 暴露的 json 中包含了多种属性:

  1. appName: 应用名

  2. logDir: 日志所在目录

  3. logUsePid: 日志文件名是否带上进程id

  4. blockPage: 限流 block 之后跳转的页面

  5. metricsFileSize: metrics 文件的大小

  6. metricsFileCharset: metrics 文件对应的字符集

  7. totalMetricsFileCount: metrics 最多保留的文件数

  8. consoleServer: sentinel dashboard 地址

  9. clientIp: 客户端 ip

  10. heartbeatIntervalMs: 客户端跟 dashboard 的心跳间隔时间

  11. clientPort: 客户端需要暴露的端口跟 dashboard 进行交互

  12. coldFactor: 冷启动因子

  13. filter: CommonFilter 相关的属性, 比如 order, urlPatterns 以及 enable

  14. datasource: 客户端配置的数据源信息

  15. rules: 客户端生效的规则,内部含有 flowRules, degradeRules, systemRules, authorityRule, paramFlowRule

这是 Endpoint 暴露的 json 示例:

{

“blockPage”: null,

“appName”: “sentinel-example”,

“consoleServer”: “localhost:8080”,

“coldFactor”: “3”,

“rules”: {

“flowRules”: [{

“resource”: “GET:http://www.taobao.com”,

“limitApp”: “default”,

“grade”: 1,

“count”: 0.0,

“strategy”: 0,

“refResource”: null,

“controlBehavior”: 0,

“warmUpPeriodSec”: 10,

“maxQueueingTimeMs”: 500,

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

PPO等大厂,18年进入阿里一直到现在。**

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-aQvEZRkN-1710937882756)]
[外链图片转存中…(img-A9gwKiG2-1710937882757)]
[外链图片转存中…(img-UpRN2mR4-1710937882757)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-6CbD9KLC-1710937882757)]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值