Commit 31283c83 authored by 杨锐's avatar 杨锐

auto config ElasticJobAspect

parent 94279625
...@@ -40,6 +40,11 @@ ...@@ -40,6 +40,11 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
......
package cn.quantgroup.tech.brave.configuration; package cn.quantgroup.tech.brave.configuration;
import brave.Tracer;
import brave.Tracing; import brave.Tracing;
import brave.http.HttpTracing; import brave.http.HttpTracing;
import brave.httpclient.TracingHttpClientBuilder; import brave.httpclient.TracingHttpClientBuilder;
...@@ -29,6 +30,9 @@ import okhttp3.Dispatcher; ...@@ -29,6 +30,9 @@ import okhttp3.Dispatcher;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
...@@ -40,11 +44,9 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean; ...@@ -40,11 +44,9 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import zipkin2.Span; import zipkin2.Span;
...@@ -53,6 +55,7 @@ import zipkin2.reporter.AsyncReporter; ...@@ -53,6 +55,7 @@ import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.Sender; import zipkin2.reporter.Sender;
import zipkin2.reporter.kafka11.KafkaSender; import zipkin2.reporter.kafka11.KafkaSender;
import javax.annotation.Resource;
import javax.servlet.Filter; import javax.servlet.Filter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -93,8 +96,8 @@ public class BraveAutoConfiguration { ...@@ -93,8 +96,8 @@ public class BraveAutoConfiguration {
/** /**
* 提供HttpClientBuilderBean * 提供HttpClientBuilderBean
* *
* @see ITechHttpClient
* @return * @return
* @see ITechHttpClient
*/ */
@Deprecated @Deprecated
@Bean(name = "techHttpClientBuilder") @Bean(name = "techHttpClientBuilder")
...@@ -113,8 +116,8 @@ public class BraveAutoConfiguration { ...@@ -113,8 +116,8 @@ public class BraveAutoConfiguration {
/** /**
* 提供OkHttpClientBean * 提供OkHttpClientBean
* *
* @see ITechOkHttpClient
* @return * @return
* @see ITechOkHttpClient
*/ */
@Deprecated @Deprecated
@Bean(name = "techOkHttpClientBuilder") @Bean(name = "techOkHttpClientBuilder")
...@@ -132,8 +135,8 @@ public class BraveAutoConfiguration { ...@@ -132,8 +135,8 @@ public class BraveAutoConfiguration {
/** /**
* 提供RestTemplate * 提供RestTemplate
* *
* @see ITechRestTemplateBuilder
* @return * @return
* @see ITechRestTemplateBuilder
*/ */
@Deprecated @Deprecated
@Bean(name = "techRestTemplate") @Bean(name = "techRestTemplate")
...@@ -296,9 +299,9 @@ public class BraveAutoConfiguration { ...@@ -296,9 +299,9 @@ public class BraveAutoConfiguration {
/** /**
* 提供HttpClientBuilderBean * 提供HttpClientBuilderBean
* *
* @see ITechHttpClient
* @param tracing * @param tracing
* @return * @return
* @see ITechHttpClient
*/ */
@Deprecated @Deprecated
@Bean(name = "techHttpClientBuilder") @Bean(name = "techHttpClientBuilder")
...@@ -316,9 +319,9 @@ public class BraveAutoConfiguration { ...@@ -316,9 +319,9 @@ public class BraveAutoConfiguration {
/** /**
* 提供OkHttpClientBean * 提供OkHttpClientBean
* *
* @see ITechOkHttpClient
* @param httpTracing * @param httpTracing
* @return * @return
* @see ITechOkHttpClient
*/ */
@Deprecated @Deprecated
@Bean(name = "techOkHttpClientBuilder") @Bean(name = "techOkHttpClientBuilder")
...@@ -343,8 +346,8 @@ public class BraveAutoConfiguration { ...@@ -343,8 +346,8 @@ public class BraveAutoConfiguration {
/** /**
* 提供RestTemplate * 提供RestTemplate
* *
* @see ITechRestTemplateBuilder
* @return * @return
* @see ITechRestTemplateBuilder
*/ */
@Deprecated @Deprecated
@Bean(name = "techRestTemplate") @Bean(name = "techRestTemplate")
...@@ -412,6 +415,38 @@ public class BraveAutoConfiguration { ...@@ -412,6 +415,38 @@ public class BraveAutoConfiguration {
} }
@Configuration
@ConditionalOnProperty(name = "elastic.job.enabled", havingValue = "true")
public static class ElasticJobEnabled {
@Aspect
@Component
public class ElasticJobAspect {
@Resource
private Tracing tracing;
@Around("target(com.dangdang.ddframe.job.api.ElasticJob)")
public Object dynamicTrace(ProceedingJoinPoint pjp) throws Throwable {
Tracer tracer = tracing.tracer();
/* 防止应用层非法使用(上下文中存在span),不生成root span。 */
if (tracer.currentSpan() == null) {
brave.Span span = tracer.newTrace();
try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
return pjp.proceed();
// note: try-with-resources closes the scope *before* the catch block
} catch (RuntimeException | Error e) {
span.error(e);
throw e;
} catch (Throwable e1) {
throw e1;
} finally {
span.finish();
}
}
return pjp.proceed();
}
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment