sentinel of grpc circuit breaker

Keywords: Programming Spring github

background

In order to prevent the downstream service avalanche, the use of circuit breakers is considered here

Technology selection

Due to the spring boot service and the integration of istio, three solutions are considered here

  • istio
  • hystrix
  • sentinel

Here are the comparison of these schemes

Implementation of microservice circuit breaker mode: Istio vs Hystrix

Sentinel vs. Hystrix

The first consideration is istio, but when istio is used for fusing and shunting, the flow is unstable, and the return status and data cannot reach the pre fetching effect. Later, considering that sentinel automatically integrates grpc, sentinel is used here.

step

1. Increase dependency

 <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. Add configuration class

Scheme 1: add global interceptor


/**
 * @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);
    }
}

Scheme 2: add interceptor when calling Of course, there is another way to add interceptors. As follows, add interceptors in the startup class

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

Then add dependency in the calling class

    @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. Add sentinel dashboard configuration

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

4. Deploy sentinel dashboard

Refer to the following link for deployment:

Sentinel console

After startup, visit the following links, grpc is automatically integrated, and the effect is as follows:

For detailed configuration, please refer to the official wiki: https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8

Posted by EagleAmerican on Tue, 24 Mar 2020 08:02:37 -0700