SmartRestTemplateConfig.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package net.lab1024.smartadmin.config;
  2. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  3. import org.apache.http.client.HttpClient;
  4. import org.apache.http.client.config.RequestConfig;
  5. import org.apache.http.config.Registry;
  6. import org.apache.http.config.RegistryBuilder;
  7. import org.apache.http.conn.socket.ConnectionSocketFactory;
  8. import org.apache.http.conn.socket.PlainConnectionSocketFactory;
  9. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  10. import org.apache.http.impl.client.HttpClientBuilder;
  11. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.annotation.Configuration;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.http.client.ClientHttpRequestFactory;
  17. import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  18. import org.springframework.http.converter.FormHttpMessageConverter;
  19. import org.springframework.http.converter.HttpMessageConverter;
  20. import org.springframework.http.converter.StringHttpMessageConverter;
  21. import org.springframework.web.client.RestTemplate;
  22. import java.nio.charset.Charset;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * [ ]
  27. *
  28. * @author yandanyang
  29. * @version 1.0
  30. * @company 1024lab.net
  31. * @copyright (c) 2019 1024lab.netInc. All rights reserved.
  32. * @date
  33. * @since JDK1.8
  34. */
  35. @Configuration
  36. public class SmartRestTemplateConfig {
  37. @Value("${http.pool.max-total}")
  38. private Integer maxTotal;
  39. @Value("${http.pool.default-max-per-route}")
  40. private Integer defaultMaxPerRoute;
  41. @Value("${http.pool.socket-timeout}")
  42. private Integer socketTimeout;
  43. @Value("${http.pool.connect-timeout}")
  44. private Integer connectTimeout;
  45. @Value("${http.pool.connection-request-timeout}")
  46. private Integer connectionRequestTimeout;
  47. @Bean
  48. public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
  49. return new RestTemplate(factory);
  50. }
  51. @Bean
  52. public ClientHttpRequestFactory httpRequestFactory() {
  53. HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient());
  54. return factory;
  55. }
  56. /**
  57. * fastJsonRestTemplate
  58. *
  59. * @return
  60. */
  61. @Bean(name = "fastJsonRestTemplate")
  62. public RestTemplate fastJsonRestTemplate() {
  63. RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
  64. HttpMessageConverter<?> converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
  65. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  66. List<MediaType> fastMediaTypes = new ArrayList<>();
  67. fastMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
  68. fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  69. fastConverter.setSupportedMediaTypes(fastMediaTypes);
  70. List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
  71. converters.add(1,converter);
  72. converters.add(fastConverter);
  73. return restTemplate;
  74. }
  75. @Bean
  76. public HttpClient httpClient() {
  77. Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
  78. .register("http", PlainConnectionSocketFactory.getSocketFactory())
  79. .register("https", SSLConnectionSocketFactory.getSocketFactory())
  80. .build();
  81. PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
  82. connectionManager.setMaxTotal(maxTotal);
  83. connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
  84. RequestConfig requestConfig = RequestConfig.custom()
  85. .setSocketTimeout(socketTimeout)
  86. .setConnectTimeout(connectTimeout)
  87. .setConnectionRequestTimeout(connectionRequestTimeout)
  88. .build();
  89. return HttpClientBuilder.create()
  90. .setDefaultRequestConfig(requestConfig)
  91. .setConnectionManager(connectionManager)
  92. .build();
  93. }
  94. }