您现在的位置是:首页 > 文章详情

grpc断路器之sentinel

日期:2020-03-23点击:589

背景

为了防止下游服务雪崩,这里考虑使用断路器

技术选型

由于是springboot服务且集成了istio,这里考虑三种方案

  • istio
  • hystrix
  • sentinel

这里分别有这几种方案的对比

微服务断路器模式实现:Istio vs Hystrix

Sentinel 与 Hystrix 的对比

首先考虑的是istio,但是在使用istio进行熔断、分流时,流量不稳定,并且返回状态以及数据达不到预取效果,后面考虑到sentinel自动集成了grpc,所以这里使用sentinel。

步骤

1、增加相关依赖

 <dependencies> <dependency> <groupid>com.alibaba.cloud</groupid> <artifactid>spring-cloud-starter-alibaba-sentinel</artifactid> </dependency> <dependency> <groupid>com.alibaba.csp</groupid> <artifactid>sentinel-grpc-adapter</artifactid> <version>1.7.1</version> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>com.alibaba.cloud</groupid> <artifactid>spring-cloud-alibaba-dependencies</artifactid> <version>2.0.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> 

2、增加配置类

方案一:增加全局拦截器

 /** * @Auther: lipeng * @Date: 2020/3/23 17:24 * @Description: */ @Order(Ordered.LOWEST_PRECEDENCE) @Configuration @Slf4j public class SentinelConfig { @Bean public ClientInterceptor sentinelGrpcClientInterceptor(){ return new SentinelGrpcClientInterceptor(); } @Bean public GlobalClientInterceptorConfigurer globalInterceptorConfigurerAdapter(ClientInterceptor sentinelGrpcClientInterceptor) { return registry -&gt; registry.addClientInterceptors(sentinelGrpcClientInterceptor); } } 

方案二:调用时增加拦截器 当然这里也可以换一种方式增加拦截器,如下,在启动类中增加

@Bean public ClientInterceptor sentinelGrpcClientInterceptor(){ return new SentinelGrpcClientInterceptor(); } 

然后在调用类中增加依赖

 @Autowired private ClientInterceptor sentinelGrpcClientInterceptor; public void doXXX(){ XXXGrpc.XXXBlockingStub stub = XXXGrpc.newBlockingStub(serverChannel).withInterceptors(sentinelGrpcClientInterceptor); XXXApi.XXXResponse response = stub.withDeadlineAfter(grpcTimeout, TimeUnit.MILLISECONDS).doXXX(request); } 

3、增加sentinel dashboard配置

spring: cloud: sentinel: transport: port: 8719 dashboard: localhost:7080 

4、部署sentinel dashboard

部署参考下面链接:

Sentinel 控制台

启动后访问下相关链接,grpc自动集成上去了,效果如下:

在这里插入图片描述 在这里插入图片描述

详细配置可以参考官方wiki:https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8

原文链接:https://my.oschina.net/u/560547/blog/3209530
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章