|
| 1 | +package cn.iocoder.springboot.lab40.zipkindemo.config; |
| 2 | + |
| 3 | +import brave.CurrentSpanCustomizer; |
| 4 | +import brave.SpanCustomizer; |
| 5 | +import brave.Tracing; |
| 6 | +import brave.http.HttpTracing; |
| 7 | +import brave.jms.JmsTracing; |
| 8 | +import brave.servlet.TracingFilter; |
| 9 | +import org.springframework.beans.BeansException; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 12 | +import org.springframework.context.annotation.Bean; |
| 13 | +import org.springframework.context.annotation.Configuration; |
| 14 | +import zipkin2.Span; |
| 15 | +import zipkin2.reporter.AsyncReporter; |
| 16 | +import zipkin2.reporter.Sender; |
| 17 | +import zipkin2.reporter.okhttp3.OkHttpSender; |
| 18 | + |
| 19 | +import javax.jms.ConnectionFactory; |
| 20 | +import javax.servlet.Filter; |
| 21 | + |
| 22 | +@Configuration |
| 23 | +public class ZipkinConfiguration { |
| 24 | + |
| 25 | + // ==================== 通用配置 ==================== |
| 26 | + |
| 27 | + /** |
| 28 | + * Configuration for how to send spans to Zipkin |
| 29 | + */ |
| 30 | + @Bean |
| 31 | + public Sender sender() { // Sender 采用 HTTP 通信方式 |
| 32 | + return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans"); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Configuration for how to buffer spans into messages for Zipkin |
| 37 | + */ |
| 38 | + @Bean |
| 39 | + public AsyncReporter<Span> spanReporter() { // 异步 Reporter |
| 40 | + return AsyncReporter.create(sender()); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Controls aspects of tracing such as the service name that shows up in the UI |
| 45 | + */ |
| 46 | + @Bean |
| 47 | + public Tracing tracing(@Value("${spring.application.name}") String serviceName) { |
| 48 | + return Tracing.newBuilder() |
| 49 | + .localServiceName(serviceName) // 应用名 |
| 50 | + .spanReporter(this.spanReporter()).build(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Allows someone to add tags to a span if a trace is in progress |
| 55 | + */ |
| 56 | + @Bean |
| 57 | + public SpanCustomizer spanCustomizer(Tracing tracing) { |
| 58 | + return CurrentSpanCustomizer.create(tracing); |
| 59 | + } |
| 60 | + |
| 61 | + // ==================== HTTP 相关 ==================== |
| 62 | + |
| 63 | + /** |
| 64 | + * Decides how to name and tag spans. By default they are named the same as the http method |
| 65 | + */ |
| 66 | + @Bean |
| 67 | + public HttpTracing httpTracing(Tracing tracing) { |
| 68 | + return HttpTracing.create(tracing); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates server spans for http requests |
| 73 | + */ |
| 74 | + @Bean |
| 75 | + public Filter tracingFilter(HttpTracing httpTracing) { // 拦截请求,记录 HTTP 请求的链路信息 |
| 76 | + return TracingFilter.create(httpTracing); |
| 77 | + } |
| 78 | + |
| 79 | + // ==================== SpringMVC 相关 ==================== |
| 80 | + // @see SpringMvcConfiguration 类上的,@Import(SpanCustomizingAsyncHandlerInterceptor.class) 。因为 SpanCustomizingAsyncHandlerInterceptor 未提供 public 构造方法 |
| 81 | + |
| 82 | + // ==================== RabbitMQ 相关 ==================== |
| 83 | + |
| 84 | + @Bean |
| 85 | + public JmsTracing jmsTracing(Tracing tracing) { |
| 86 | + return JmsTracing.newBuilder(tracing) |
| 87 | + .remoteServiceName("demo-mq-activemq") // 远程 ActiveMQ 服务名,可自定义 |
| 88 | + .build(); |
| 89 | + } |
| 90 | + |
| 91 | + @Bean |
| 92 | + public BeanPostProcessor activeMQBeanPostProcessor(JmsTracing jmsTracing) { |
| 93 | + return new BeanPostProcessor() { |
| 94 | + |
| 95 | + @Override |
| 96 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 97 | + return bean; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 102 | + // 如果是 ConnectionFactory ,针对 ActiveMQ Producer 和 Consumer |
| 103 | + if (bean instanceof ConnectionFactory) { |
| 104 | + return jmsTracing.connectionFactory((ConnectionFactory) bean); |
| 105 | + } |
| 106 | + return bean; |
| 107 | + } |
| 108 | + |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments