背景
【grpc|grpc断路器之sentinel】为了防止下游服务雪崩,这里考虑使用断路器
技术选型
由于是springboot服务且集成了istio,这里考虑三种方案
- istio
- hystrix
- sentinel
微服务断路器模式实现:Istio vs Hystrix
Sentinel 与 Hystrix 的对比
首先考虑的是istio,但是在使用istio进行熔断、分流时,流量不稳定,并且返回状态以及数据达不到预取效果,后面考虑到sentinel自动集成了grpc,所以这里使用sentinel。
步骤
1、增加相关依赖
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
com.alibaba.csp
sentinel-grpc-adapter
1.7.1
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.0.1.RELEASE
pom
import
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 -> 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自动集成上去了,效果如下:
![grpc|grpc断路器之sentinel](https://img.it610.com/image/info8/af8409ed310143e7b1fb5c256ef47ee5.jpg)
文章图片
![grpc|grpc断路器之sentinel](https://img.it610.com/image/info8/3d37102c74964c5bba9c5ab2eb2aadab.jpg)
文章图片
详细配置可以参考官方wiki:https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8
推荐阅读
- grpc- python调用c++的rpc服务
- cmake|cmake - 如何生成grpc的*.pb.cc和*.pb.h文件
- GRPC 负载均衡实现
- AspNetCore|gRPC-Web 踩坑记
- #|gRPC 在 Java 中的入门实例
- grpc练习
- gRPC Java Quickstart
- GRPC 初体验
- grpc|在python中使用grpc和protobuf
- 微服务|GO-Grpc微服务开发六 网关和http调用