Springboot Rest Template配置
-
支持Ok Http和Http连接池模式,内嵌包装成HttClientService服务,并提供完成的请求日志处理
-
引入pom:
<dependency> <groupId>com.xiao.skywalking.demo</groupId> <artifactId>SpringBoot-Custom-Rest-Autconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
-
Ok Http使用:
rest: # okhttp 配置 okhttp: enable: true connection-timeout: 12000 read-timeout: 30000 write-timeout: 12000
-
Http pool使用:
rest # http pool pool: enable: true max-total: 20 default-max-per-route: 2 validate-after-inactivity: 2000 connect-timeout: 10000 connection-request-timeout: 10000 socket-timeout: 10000
-
同步异步使用:
rest: http: service: sync: true async: false
-
使用方式,推荐使用HttpClientService,因为提供了完整的日志记录:
// 使用包装http client @Autowired private HttpClientService httpClientService; // 使用 resttemplate @Autowired private RestTemplate restTemplate;
-
HttpClientService日志处理,实现HttpRequestLogService接口
public class HttpLogServiceImpl implements HttpRequestLogService { /** * [简要描述]:保存日志信息<br/> * [详细描述]:<br/> * * @param requestLog : * llxiao 2019/4/24 - 14:42 **/ @Override public void saveRequestLog(HttpRequestLog requestLog) { // 日志输出 log.info("Example log : {}", JSONObject.toJSONString(requestLog)); } }