下面列出了org.springframework.beans.factory.SmartInitializingSingleton#org.springframework.http.client.AsyncClientHttpRequestInterceptor 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Bean
public SmartInitializingSingleton loadBalancedAsyncRestTemplateInitializer2(
final List<AsyncRestTemplateCustomizer> customizers) {
return new SmartInitializingSingleton() {
@Override
public void afterSingletonsInstantiated() {
logger.info("init AsyncRestTemplateCircuitBreaker start");
for (AsyncRestTemplate restTemplate : AsyncRestTemplateCircuitBreakerAutoConfiguration.this
.restTemplates) {
AsyncRestTemplateCircuitInterceptor interceptor =
new AsyncRestTemplateCircuitInterceptor(circuitBreakerCore);
logger.info("add AsyncRestTemplateCircuitInterceptor first");
List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
interceptors.add(0, interceptor);
restTemplate.setInterceptors(interceptors);
}
logger.info("init AsyncRestTemplateCircuitBreaker end");
}
};
}
@Bean
public SmartInitializingSingleton loadBalancedAsyncRestTemplateInitializer2(
final List<AsyncRestTemplateCustomizer> customizers) {
return new SmartInitializingSingleton() {
@Override
public void afterSingletonsInstantiated() {
logger.info("Init AsyncRestTemplateRateLimiterConfiguration");
for (AsyncRestTemplate restTemplate : AsyncRestTemplateRateLimiterConfiguration.this.restTemplates) {
List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
logger.debug("AsyncRestTemplate init start, interceptor size:" + interceptors.size());
AsyncClientHttpRequestInterceptor interceptor =
new AsyncRestTemplateRateLimiterInterceptor(serviceName);
interceptors.add(0, interceptor);
logger.debug("AsyncRestTemplate init end, Add AsyncRestTemplateRateLimiterInterceptor");
restTemplate.setInterceptors(interceptors);
}
}
};
}
private AsyncRestTemplate createNewRestTemplate(String addr) {
// we use async client because usual client does not allow to interruption in some cases
NettyRequestFactory factory = new NettyRequestFactory();
if(AddressUtils.isHttps(addr)) {
try {
initSsl(addr, factory);
} catch (Exception e) {
log.error("", e);
}
}
final AsyncRestTemplate restTemplate = new AsyncRestTemplate(factory);
List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new HttpAuthInterceptor(registryRepository));
if(!StringUtils.isEmpty(agentPassword)) {
interceptors.add(new BasicAuthAsyncInterceptor("admin", agentPassword));
}
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
public void customize(final AsyncRestTemplate restTemplate) {
if (restTemplate.getInterceptors().contains(this.metricsClientHttpRequestInterceptor)) {
return;
}
UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
templateHandler = this.metricsClientHttpRequestInterceptor.createUriTemplateHandler(templateHandler);
restTemplate.setUriTemplateHandler(templateHandler);
List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(this.metricsClientHttpRequestInterceptor);
interceptors.addAll(restTemplate.getInterceptors());
restTemplate.setInterceptors(interceptors);
}
@Deprecated
public static AsyncRestTemplate buildAsyncRestTemplate() {
AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<AsyncClientHttpRequestInterceptor>();
AsyncRestTemplateRequestInterceptor asyncRestTemplateInterceptor = new AsyncRestTemplateRequestInterceptor(
getRestTemplateTracer());
interceptors.add(asyncRestTemplateInterceptor);
asyncRestTemplate.setInterceptors(interceptors);
return asyncRestTemplate;
}
private void registerTracingInterceptor(InterceptingAsyncHttpAccessor restTemplate) {
List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
for (AsyncClientHttpRequestInterceptor interceptor : interceptors) {
if (interceptor instanceof TracingAsyncRestTemplateInterceptor) {
return;
}
}
log.debug("Adding " + TracingAsyncRestTemplateInterceptor.class.getSimpleName() + " to " + restTemplate);
interceptors = new ArrayList<>(interceptors);
interceptors.add(new TracingAsyncRestTemplateInterceptor(tracer, spanDecorators));
restTemplate.setInterceptors(interceptors);
}
@PostConstruct
public void init() {
if (this.restTemplates != null) {
for (AsyncRestTemplate restTemplate : this.restTemplates) {
List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>(
restTemplate.getInterceptors());
interceptors.add(this.clientInterceptor);
restTemplate.setInterceptors(interceptors);
}
}
}
@Bean
public AsyncRestTemplateCustomizer asyncRestTemplateCustomizer(
final AsyncLoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> {
List<AsyncClientHttpRequestInterceptor> list = new ArrayList<>(
restTemplate.getInterceptors());
list.add(loadBalancerInterceptor);
restTemplate.setInterceptors(list);
};
}
private void assertLoadBalanced(AsyncRestTemplate restTemplate) {
List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate
.getInterceptors();
then(interceptors).hasSize(1);
AsyncClientHttpRequestInterceptor interceptor = interceptors.get(0);
then(interceptor).isInstanceOf(AsyncLoadBalancerInterceptor.class);
}
AsyncClientHttpRequestFactory configureClient(AsyncClientHttpRequestInterceptor interceptor) {
HttpComponentsAsyncClientHttpRequestFactory factory =
new HttpComponentsAsyncClientHttpRequestFactory(asyncClient);
factory.setReadTimeout(1000);
factory.setConnectTimeout(1000);
this.interceptor = interceptor;
return factory;
}
@Test public void autowiredWithBeanConfig() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(HttpTracingConfiguration.class);
ctx.register(TracingAsyncClientHttpRequestInterceptor.class);
ctx.refresh();
ctx.getBean(AsyncClientHttpRequestInterceptor.class);
}
@Test
public void testAsyncInterceptorNotRegistered() {
for (AsyncClientHttpRequestInterceptor interceptor : asyncRestTemplate.getInterceptors()) {
assertThat(interceptor).isNotInstanceOf(TracingRestTemplateInterceptor.class);
}
}
/**
* Return the request interceptor that this accessor uses.
*/
public List<AsyncClientHttpRequestInterceptor> getInterceptors() {
return this.interceptors;
}
public static AsyncClientHttpRequestInterceptor create(Tracing tracing) {
return create(HttpTracing.create(tracing));
}
public static AsyncClientHttpRequestInterceptor create(HttpTracing httpTracing) {
return new TracingAsyncClientHttpRequestInterceptor(httpTracing);
}
/**
* Set the request interceptors that this accessor should use.
* @param interceptors the list of interceptors
*/
public void setInterceptors(List<AsyncClientHttpRequestInterceptor> interceptors) {
this.interceptors = interceptors;
}