SpringBoot配置RestTemplate的代理和超时时间application.properties:
1 #代理设置
abled=false
3 proxy.host=192.168.18.233
4 proxy.port=8888
5
6 #REST超时配置
7 rest.ReadTimeout=35000
8 rest.ConnectTimeout=5000
代理配置类:
1import org.t.properties.ConfigurationProperties;
2import org.springframework.stereotype.Component;
3
4import lombok.Data;
5
6/**
7 * ⽹络代理设置
8 *
9 * @author yangzhilong
10 *
11*/
12 @Component
13 @ConfigurationProperties(prefix="proxy")
14 @Data
15public class ProxyConfig {
16/**
17    * 是否启⽤代理
18*/
19private Boolean enabled;
20/**
21    * 代理主机地址
22*/
23private String host;
24/**
25    * 代理端⼝
26*/
27private Integer port;
28 }
SpringBoot的Configuration:
1import java.InetSocketAddress;
2import java.Proxy;
3import java.SocketAddress;
4
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.beans.factory.annotation.Value;
7import org.springframework.dition.ConditionalOnClass;
8import t.annotation.Bean;
9import t.annotation.Configuration;
10import org.springframework.http.client.SimpleClientHttpRequestFactory;
11import org.springframework.web.client.RestTemplate;
12
l.vo.ProxyConfig;
14
15 @Configuration
16 @ConditionalOnClass(ProxyConfig.class)
17public class RestConfiguration {
18    @Value("${rest.ReadTimeout}")
19private int readTimeout;
20    @Value("${rest.ConnectTimeout}")
21private int connectionTimeout;
22    @Autowired
23private ProxyConfig proxyConfig;
24
25    @Bean
26public SimpleClientHttpRequestFactory httpClientFactory() {
27        SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
28        httpRequestFactory.setReadTimeout(readTimeout);
29        httpRequestFactory.setConnectTimeout(connectionTimeout);
30
Enabled()){
32            SocketAddress address = new Host(), Port());
33            Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
34            httpRequestFactory.setProxy(proxy);
35        }
36
37return httpRequestFactory;
38    }
39
40    @Bean
41public RestTemplate restTemplate(SimpleClientHttpRequestFactory httpClientFactory) {
42        RestTemplate restTemplate = new RestTemplate(httpClientFactory);
43return restTemplate;
44    }
45 }
如果不希望这种全局的超时时间污染正常的SpringCloud中restTemplate的时间设置,可以使⽤如下⽅法: l.autoconfig;
2
3import org.springframework.beans.factory.annotation.Value;
4import t.annotation.Bean;
5import t.annotation.Configuration;
6import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
7import org.springframework.web.client.RestTemplate;
8
l.util.RestClient;
10
11/**
12 * ⼯具类引导装配类springcloud和springboot
13 * @author yangzhilong
14 *
15*/
16 @Configuration
17public class RestClientAutoConfiguration {
18    @Value("${tTimeout:10000}")
19private int connectTimeout;
20    @Value("${adTimeout:30000}")
21private int readTimeout;
22
23/**
24    * 使⽤Bootstrap来装配RestClient中的RestTemplate属性,
25    * 避免直接装配RestTemplate来污染了正常的spring Cloud的调⽤
26    * @return
27*/
28    @Bean
29public 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);
35return new RestClientBootstrap();
36    }
37
38/**
39    * 空的引导类
40    * @author yangzhilong
41    *
42*/
43static class RestClientBootstrap {
44
45    }
46 }
RestClient⼯具类:
package sc.auth.utils;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
/**
* HTTP Rest Util
* @author yangzhilong
*
*/
public class RestClient {
private static RestTemplate restTemplate;
/**
* @param client
*/
public static void setRestTemplate(RestTemplate client) {
restTemplate = client;
}
/
**
*
* @param <T>
* @param url
* @param clasz
* @return
*/
public static <T> T get(String url, Class<T> clasz) {
ForObject(url , clasz);
}
/**
*
* @param <T>
* @param url
* @param headMap
* @param bodyObj
* @param clasz
* @return
*/
public static <T> T postJson(String url, Map<String, String> headMap, Object bodyObj, Class<T> clasz) {        HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.String());
if(null != headMap) {
headers.Key(), Value());
});
}
String result = null;
if(bodyObj == null){
result = "{}";
}else{
result = JSONString(bodyObj);
}
HttpEntity<String> formEntity = new HttpEntity<String>(result,headers);
return restTemplate.postForObject(url , formEntity, clasz);
}
/**
*
* @param <T>
* @param url
* @param attrMap
* @param clasz
* @return
*/
public static <T> T postForm(String url, Map<String , String> attrMap, Class<T> clasz){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
params.Key() , Value());
});
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
hange(url, HttpMethod.POST, requestEntity, clasz).getBody();
}
}
然后实际发起HTTP请求的时候使⽤上⾯的⼯具类

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。