首页 技术 正文
技术 2022年11月17日
0 收藏 614 点赞 2,513 浏览 5057 个字

application.properties:

 #代理设置
proxy.enabled=false
proxy.host=192.168.18.233
proxy.port=8888 #REST超时配置
rest.ReadTimeout=35000
rest.ConnectTimeout=5000

代理配置类:

 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:

 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的时间设置,可以使用如下方法:

 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工具类:

package com.nike.gcsc.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) {
return restTemplate.getForObject(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.APPLICATION_JSON.toString());
if(null != headMap) {
headMap.entrySet().forEach(item -> {
headers.add(item.getKey(), item.getValue());
});
}
String result = null;
if(bodyObj == null){
result = "{}";
}else{
result = JSON.toJSONString(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<>();
attrMap.entrySet().forEach(item -> {
params.add(item.getKey() , item.getValue()); });
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, clasz).getBody();
}}

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849