httpclient及http日志输出

目录

httpclient及http日志输出

借助logbook输出HTTP日志

pom添加logbook依赖

application.properties

工程添加logbook filter以输出日志


 

httpclient及http日志输出

 

 

借助logbook输出HTTP日志

    在服务提供工程添加logbook filter以输出日志和服务消费者工程httpclient添加logbook拦截器

 

httpclient及http日志输出_第1张图片

 


pom添加logbook依赖


        
	       org.zalando
	      logbook-core
	      ${logbook.version}
        
	    
			org.zalando
			logbook-servlet
			${logbook.version}
	    
		
			org.zalando
			logbook-httpclient
			${logbook.version}
		
		
			org.zalando
			logbook-spring-boot-starter
			${logbook.version}
		

说明:

    1)logbook的依赖版本1.3.0

 

application.properties

logging.config=classpath:log4j2.xml
logbook.write.level=TRACE
logbook.format.style=http

 

工程添加logbook filter以输出日志

package com.mooc.house.user.config;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zalando.logbook.httpclient.LogbookHttpRequestInterceptor;
import org.zalando.logbook.httpclient.LogbookHttpResponseInterceptor;

@Configuration
@ConditionalOnClass({HttpClient.class})
@EnableConfigurationProperties(HttpClientProperties.class)
public class HttpClientAutoConfiguration {

	private final HttpClientProperties properties;
	
	public HttpClientAutoConfiguration(HttpClientProperties properties){
		this.properties = properties;
	}
	
	@Autowired
	private LogbookHttpRequestInterceptor logbookHttpRequestInterceptor;
	
	@Autowired
	private LogbookHttpResponseInterceptor logbookHttpResponseInterceptor;
	
	/**
	 * httpclient bean 的定义
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean(HttpClient.class)
	public HttpClient httpClient() {
		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectTimeout(properties.getConnectTimeOut())
				.setSocketTimeout(properties.getSocketTimeOut()).build();// 构建requestConfig
		HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
				.setUserAgent(properties.getAgent())
				.setMaxConnPerRoute(properties.getMaxConnPerRoute())
				.setMaxConnTotal(properties.getMaxConnTotaol())
				.addInterceptorFirst(logbookHttpRequestInterceptor)
				.addInterceptorFirst(logbookHttpResponseInterceptor)
				.build();
		return client;
	}
}

说明:

1)添加拦截器LogbookHttpRequestInterceptor 和 LogbookHttpResponseInterceptor;

 

 

 

 

 

 

 

==============================

QQ群:143522604

群里有相关资源

欢迎和大家一起学习、交流、提升!

==============================

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(【项目】,【微服务项目】)