微服务全链路跟踪:grpc集成zipkin

微服务全链路跟踪:grpc集成zipkin

微服务全链路跟踪:grpc集成jaeger

微服务全链路跟踪:springcloud集成jaeger

微服务全链路跟踪:jaeger集成istio,并兼容uber-trace-id与b3

微服务全链路跟踪:jaeger集成hystrix

微服务全链路跟踪:jaeger增加tag参数

码云地址:https://gitee.com/lpxs/lp-springcloud.git
有问题可以多沟通:[email protected]

本章节内容是基于springboot2集成net.devh.grpc的拓展

本章介绍grpc集成zipkin

zipkin部署

这里就不列举zipkin代码或者容器部署了,网上很多

grpc-client集成

pom.xml依赖
    
        
            
                io.zipkin.brave
                brave-bom
                4.19.2
                pom
                import
            
            
            
                net.devh
                grpc-client-spring-boot-starter
                ${net-devh-grpc.version}
            
            
                net.devh
                grpc-server-spring-boot-starter
                ${net-devh-grpc.version}
            
        
    
    
        
            io.protostuff
            protostuff-core
            1.6.0
        

        
            io.protostuff
            protostuff-runtime
            1.6.0
        
        
            io.grpc
            grpc-all
            ${grpc.version}
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
     
        
            org.projectlombok
            lombok
            true
        
        
            net.devh
            grpc-client-spring-boot-starter
        
        
            io.zipkin.brave
            brave-instrumentation-grpc
        

    

自动装配
import brave.Tracing;
import brave.grpc.GrpcTracing;
import io.grpc.ClientInterceptor;
import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import zipkin2.Span;
import zipkin2.reporter.Reporter;

/**
 * @Auther: lipeng
 * @Date: 2019/1/3 19:34
 * @Description:
 */
@Order(Ordered.LOWEST_PRECEDENCE)
@Configuration
public class GrpcSleuthConfig {

    private static final Logger logger = LoggerFactory.getLogger(GrpcSleuthConfig.class);
    @Bean
    public GrpcTracing grpcTracing(Tracing tracing) {
        return GrpcTracing.create(tracing);
    }

    //We also create a client-side interceptor and put that in the context, this interceptor can then be injected into gRPC clients and
    //then applied to the managed channel.
    @Bean
    ClientInterceptor grpcClientSleuthInterceptor(GrpcTracing grpcTracing) {
        return grpcTracing.newClientInterceptor();
    }

    // Use this for debugging (or if there is no Zipkin server running on port 9411)
    @Bean
    @ConditionalOnProperty(value = "sample.zipkin.enabled", havingValue = "false")
    public Reporter<Span> spanReporter() {
        return new Reporter<Span>() {
            @Override
            public void report(Span span) {
                logger.info("{}",span);
            }
        };
    }

    @Bean
    public GlobalClientInterceptorConfigurer globalInterceptorConfigurerAdapter(ClientInterceptor grpcClientSleuthInterceptor) {
        return registry -> registry.addClientInterceptors(grpcClientSleuthInterceptor);
    }

}

application.xml配置
spring:
  zipkin:
    enable: true
    base-url: http://zipkin.com
  sleuth: 
    web:
      client:
        enabled: true
    sampler: 
      probability: 1.0  #zipkin采集率  0.1表示 10%采集率

grpc-server

pom.xml依赖
   
        
            io.protostuff
            protostuff-core
            1.6.0
        

        
            io.protostuff
            protostuff-runtime
            1.6.0
        
        
            io.grpc
            grpc-all
            ${grpc.version}
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
     
        
            org.projectlombok
            lombok
            true
        
        
            net.devh
            grpc-server-spring-boot-starter
        
        
            io.zipkin.brave
            brave-instrumentation-grpc
        

    
自动装配
import brave.Tracing;
import brave.grpc.GrpcTracing;
import io.grpc.ClientInterceptor;
import io.grpc.ServerInterceptor;
import net.devh.boot.grpc.server.interceptor.GlobalServerInterceptorConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import zipkin2.Span;
import zipkin2.reporter.Reporter;

/**
 * @Auther: lipeng
 * @Date: 2019/1/3 19:34
 * @Description:
 */
@Configuration
public class GrpcSleuthConfig {

    private static final Logger logger = LoggerFactory.getLogger(GrpcSleuthConfig.class);
    @Bean
    public GrpcTracing grpcTracing(Tracing tracing) {
        return GrpcTracing.create(tracing);
    }

    //grpc-spring-boot-starter provides @GrpcGlobalInterceptor to allow server-side interceptors to be registered with all
    //server stubs, we are just taking advantage of that to install the server-side gRPC tracer.
    @Bean
    ServerInterceptor grpcServerSleuthInterceptor(GrpcTracing grpcTracing) {
        return grpcTracing.newServerInterceptor();
    }

    //We also create a client-side interceptor and put that in the context, this interceptor can then be injected into gRPC clients and
    //then applied to the managed channel.
    @Bean
    ClientInterceptor grpcClientSleuthInterceptor(GrpcTracing grpcTracing) {
        return grpcTracing.newClientInterceptor();
    }

    // Use this for debugging (or if there is no Zipkin server running on port 9411)
    @Bean
    @ConditionalOnProperty(value = "sample.zipkin.enabled", havingValue = "false")
    public Reporter<Span> spanReporter() {
        return new Reporter<Span>() {
            @Override
            public void report(Span span) {
                logger.info("{}",span);
            }
        };
    }

    @Bean
    public GlobalServerInterceptorConfigurer globalInterceptorConfigurerAdapter(ServerInterceptor grpcServerSleuthInterceptor) {
        return registry -> registry.addServerInterceptors(grpcServerSleuthInterceptor);
    }
}

application.xml配置
spring:
  zipkin:
    enable: true
    base-url: http://zipkin.com
  sleuth: 
    web:
      client:
        enabled: true
    sampler: 
      probability: 1.0  #zipkin采集率  0.1表示 10%采集率

你可能感兴趣的:(springcloud,springcloud技术分享,grpc)