封裝RestTemplate,支持okhttp,httppool,同步,異步

Springboot Rest Template配置

  1. 支持Ok Http和Http連接池模式,內嵌包裝成HttClientService服務,並提供完成的請求日誌處理

  2. 引入pom:

    <dependency>
        <groupId>com.xiao.skywalking.demo</groupId>
        <artifactId>SpringBoot-Custom-Rest-Autconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    
  3. Ok Http使用:

        rest:
          # okhttp 配置
          okhttp:
            enable: true
            connection-timeout: 12000
            read-timeout: 30000
            write-timeout: 12000
    
  4. 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
    
  5. 同步異步使用:

       rest:
         http:
           service:
             sync: true
             async: false
    
  6. 使用方式,推薦使用HttpClientService,因爲提供了完整的日誌記錄:

            // 使用包裝http client
           @Autowired
           private HttpClientService httpClientService;
           
           // 使用 resttemplate
           @Autowired
           private RestTemplate restTemplate;
    
  7. 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));
            }
        }
    

GitHub地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章