SpringBoot配置RestTemplate的代理和超时时间

对于某些特殊需求需要使用代理的时候,使用此方法进行配置

application.properties:

1
2
3
4
5
6
7
8
#代理设置
proxy.enabled=false
proxy.host=192.168.18.233
proxy.port=8888

#REST超时配置
rest.ReadTimeout=35000
rest.ConnectTimeout=5000

代理配置类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import lombok.Data;

/**
* 网络代理设置
*
* @author yangzhilong
*
*/
@Component
@ConfigurationProperties(prefix="proxy")
@Data
public class ProxyConfig {
/**
* 是否启用代理
*/
private Boolean enabled;
/**
* 代理主机地址
*/
private String host;
/**
* 代理端口
*/
private Integer port;
}

SpringBoot的Configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.yzl.vo.ProxyConfig;

@Configuration
@ConditionalOnClass(ProxyConfig.class)
public class RestConfiguration {
@Value("${rest.ReadTimeout}")
private int readTimeout;
@Value("${rest.ConnectTimeout}")
private int connectionTimeout;
@Autowired
private ProxyConfig proxyConfig;

@Bean
public SimpleClientHttpRequestFactory httpClientFactory() {
SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
httpRequestFactory.setReadTimeout(readTimeout);
httpRequestFactory.setConnectTimeout(connectionTimeout);

if(proxyConfig.getEnabled()){
SocketAddress address = new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort());
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
httpRequestFactory.setProxy(proxy);
}

return httpRequestFactory;
}

@Bean
public RestTemplate restTemplate(SimpleClientHttpRequestFactory httpClientFactory) {
RestTemplate restTemplate = new RestTemplate(httpClientFactory);
return restTemplate;
}
}

如果不希望这种全局的超时时间污染正常的SpringCloud中restTemplate的时间设置,可以使用如下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.yzl.autoconfig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.yzl.util.RestClient;

/**
* 工具类引导装配类
* @author yangzhilong
*
*/
@Configuration
public class RestClientAutoConfiguration {
@Value("${rest.config.connectTimeout:10000}")
private int connectTimeout;
@Value("${rest.config.readTimeout:30000}")
private int readTimeout;

/**
* 使用Bootstrap来装配RestClient中的RestTemplate属性,
* 避免直接装配RestTemplate来污染了正常的spring Cloud的调用
* @return
*/
@Bean
public RestClientBootstrap bootstrap(){
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectTimeout(connectTimeout);
httpRequestFactory.setReadTimeout(readTimeout);
RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
RestClient.setRestTemplate(restTemplate);
return new RestClientBootstrap();
}

/**
* 空的引导类
* @author yangzhilong
*
*/
static class RestClientBootstrap {

}
}

RestClient工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.yzl.util;

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请求帮助类
* @author yangzhilong
*
*/
public class RestClient {

private static RestTemplate restTemplate;

/**
* 注入实现类
* @param client
*/
public static void setRestTemplate(RestTemplate client) {
restTemplate = client;
}
/**
* 无参数或者参数附带在url中
* @param url
* @return
*/
public static String get(String url) {
return restTemplate.getForObject(url , String.class);
}

/**
* json格式的post提交
* @param obj
* @param url
* @return
*/
public static String postJson(String url, Object obj) {
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
String result = null;
if(obj == null){
result = "{}";
}else{
result = JSON.toJSONString(obj);
}
HttpEntity<String> formEntity = new HttpEntity<String>(result,headers);
return restTemplate.postForObject(url , formEntity, String.class);
}

/**
* form格式的post提交
* @param map
* @param url
* @return
*/
public static String postForm(String url, Map<String , String> map){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
for(Map.Entry<String ,String> me : map.entrySet()){
params.add(me.getKey() , me.getValue());
}
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class).getBody();
}

}

然后实际发起HTTP请求的时候使用上面的工具类即可!

参考:
https://www.cnblogs.com/yangzhilong/p/6640207.html

0%