application.properties:
1 #代理设置 2 proxy.enabled=false 3 proxy.host=192.168.18.233 4 proxy.port=8888 5 6 #REST超时配置 7 rest.ReadTimeout=35000 8 rest.ConnectTimeout=5000
代理配置类:
1 import org.springframework.boot.context.properties.ConfigurationProperties; 2 import org.springframework.stereotype.Component; 3 4 import lombok.Data; 5 6 /** 7 * 网络代理设置 8 * 9 * @author yangzhilong 10 * 11 */ 12 @Component 13 @ConfigurationProperties(prefix="proxy") 14 @Data 15 public class ProxyConfig { 16 /** 17 * 是否启用代理 18 */ 19 private Boolean enabled; 20 /** 21 * 代理主机地址 22 */ 23 private String host; 24 /** 25 * 代理端口 26 */ 27 private Integer port; 28 }
SpringBoot的Configuration:
1 import java.net.InetSocketAddress; 2 import java.net.Proxy; 3 import java.net.SocketAddress; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Value; 7 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.context.annotation.Configuration; 10 import org.springframework.http.client.SimpleClientHttpRequestFactory; 11 import org.springframework.web.client.RestTemplate; 12 13 import com.yzl.vo.ProxyConfig; 14 15 @Configuration 16 @ConditionalOnClass(ProxyConfig.class) 17 public class RestConfiguration { 18 @Value("${rest.ReadTimeout}") 19 private int readTimeout; 20 @Value("${rest.ConnectTimeout}") 21 private int connectionTimeout; 22 @Autowired 23 private ProxyConfig proxyConfig; 24 25 @Bean 26 public SimpleClientHttpRequestFactory httpClientFactory() { 27 SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory(); 28 httpRequestFactory.setReadTimeout(readTimeout); 29 httpRequestFactory.setConnectTimeout(connectionTimeout); 30 31 if(proxyConfig.getEnabled()){ 32 SocketAddress address = new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort()); 33 Proxy proxy = new Proxy(Proxy.Type.HTTP, address); 34 httpRequestFactory.setProxy(proxy); 35 } 36 37 return httpRequestFactory; 38 } 39 40 @Bean 41 public RestTemplate restTemplate(SimpleClientHttpRequestFactory httpClientFactory) { 42 RestTemplate restTemplate = new RestTemplate(httpClientFactory); 43 return restTemplate; 44 } 45 }
如果不希望这种全局的超时时间污染正常的SpringCloud中restTemplate的时间设置,可以使用如下方法:
1 package com.yzl.autoconfig; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 7 import org.springframework.web.client.RestTemplate; 8 9 import com.yzl.util.RestClient; 10 11 /** 12 * 工具类引导装配类 13 * @author yangzhilong 14 * 15 */ 16 @Configuration 17 public class RestClientAutoConfiguration { 18 @Value("${rest.config.connectTimeout:10000}") 19 private int connectTimeout; 20 @Value("${rest.config.readTimeout:30000}") 21 private int readTimeout; 22 23 /** 24 * 使用Bootstrap来装配RestClient中的RestTemplate属性, 25 * 避免直接装配RestTemplate来污染了正常的spring Cloud的调用 26 * @return 27 */ 28 @Bean 29 public RestClientBootstrap bootstrap(){ 30 HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); 31 httpRequestFactory.setConnectTimeout(connectTimeout); 32 httpRequestFactory.setReadTimeout(readTimeout); 33 RestTemplate restTemplate = new RestTemplate(httpRequestFactory); 34 RestClient.setRestTemplate(restTemplate); 35 return new RestClientBootstrap(); 36 } 37 38 /** 39 * 空的引导类 40 * @author yangzhilong 41 * 42 */ 43 static class RestClientBootstrap { 44 45 } 46 }
RestClient工具类:
1 package com.yzl.util; 2 3 import java.util.Map; 4 5 import org.springframework.http.HttpEntity; 6 import org.springframework.http.HttpHeaders; 7 import org.springframework.http.HttpMethod; 8 import org.springframework.http.MediaType; 9 import org.springframework.util.LinkedMultiValueMap; 10 import org.springframework.util.MultiValueMap; 11 import org.springframework.web.client.RestTemplate; 12 13 import com.alibaba.fastjson.JSON; 14 15 /** 16 * HTTP请求帮助类 17 * @author yangzhilong 18 * 19 */ 20 public class RestClient { 21 22 private static RestTemplate restTemplate; 23 24 /** 25 * 注入实现类 26 * @param client 27 */ 28 public static void setRestTemplate(RestTemplate client) { 29 restTemplate = client; 30 } 31 /** 32 * 无参数或者参数附带在url中 33 * @param url 34 * @return 35 */ 36 public static String get(String url) { 37 return restTemplate.getForObject(url , String.class); 38 } 39 40 /** 41 * json格式的post提交 42 * @param obj 43 * @param url 44 * @return 45 */ 46 public static String postJson(String url, Object obj) { 47 HttpHeaders headers = new HttpHeaders(); 48 MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); 49 headers.setContentType(type); 50 headers.add("Accept", MediaType.APPLICATION_JSON.toString()); 51 String result = null; 52 if(obj == null){ 53 result = "{}"; 54 }else{ 55 result = JSON.toJSONString(obj); 56 } 57 HttpEntity<String> formEntity = new HttpEntity<String>(result,headers); 58 return restTemplate.postForObject(url , formEntity, String.class); 59 } 60 61 /** 62 * form格式的post提交 63 * @param map 64 * @param url 65 * @return 66 */ 67 public static String postForm(String url, Map<String , String> map){ 68 HttpHeaders headers = new HttpHeaders(); 69 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); 70 MultiValueMap<String, String> params= new LinkedMultiValueMap<>(); 71 for(Map.Entry<String ,String> me : map.entrySet()){ 72 params.add(me.getKey() , me.getValue()); 73 } 74 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers); 75 return restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class).getBody(); 76 } 77 78 }
然后实际发起HTTP请求的时候使用上面的工具类