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)); } }