SpringBoot配置RestTemplate的代理和超时时间
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请求的时候使用上面的工具类
SpringBoot配置RestTemplate的代理和超时时间的更多相关文章
- SpringBoot修改默认端口号,session超时时间
有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...
- 【Spring Cloud 源码解读】之 【如何配置好OpenFeign的各种超时时间!】
关于Feign的超时详解: 在Spring Cloud微服务架构中,大部分公司都是利用Open Feign进行服务间的调用,而比较简单的业务使用默认配置是不会有多大问题的,但是如果是业务比较复杂,服务 ...
- hystrix ,feign,ribbon的超时时间配置,以及原理分析
背景,网上看到很多关于hystrix的配置都是没生效的,如: 一.先看测试环境搭建: order 服务通过feign 的方式调用了product 服务的getProductInfo 接口 //---- ...
- nginx限制上传大小和超时时间设置说明/php限制上传大小
现象说明:在服务器上部署了一套后台环境,使用的是nginx反向代理tomcat架构,在后台里上传一个70M的视频文件,上传到一半就失效了! 原因是nginx配置里限制了上传文件的大小 client_m ...
- (转)nginx限制上传大小和超时时间设置说明/php限制上传大小
nginx限制上传大小和超时时间设置说明/php限制上传大小 原文:http://www.cnblogs.com/kevingrace/p/6093671.html 现象说明:在服务器上部署了一套后台 ...
- Nginx上传和超时时间限制 (php上传限制) - 运维笔记
现象说明:在服务器上部署了一套后台环境,使用的是nginx反向代理tomcat架构,在后台里上传一个70M的视频文件,上传到一半就失效了! 原因:nginx配置里限制了上传文件的大小 client_m ...
- scrapy 如何使用代理 以及设置超时时间
使用代理 1. 单文件spider局部使用代理 entry = 'http://xxxxx:xxxxx@http-pro.abuyun.com:xxx'.format("帐号", ...
- config文件中可以配置查询超时时间
web.config配置数据库连接 第一种:获取连接字符串 首先要定义命名空间 system.configuration 1. string connstr= string constr = Con ...
- GRUB2配置详解:默认启动项,超时时间,隐藏引导菜单,配置文件详解,图形化配置
配置文件详解: /etc/default/grub # 设定默认启动项,推荐使用数字 GRUB_DEFAULT=0 # 注释掉下面这行将会显示引导菜单 #GRUB_HIDDEN_TIMEOUT=0 # ...
随机推荐
- Java和C#差异点
语法:----------------------------------------------------------1. Java的byte为-128~127相当于c#的sbyte,c#byte ...
- ab命令作apache压力测试
ab命令作apache压力测试 ./ab -c 100 -n 10000 http://127.0.0.1/index.php -c 100 即:每次并发100个 -n 10000 即: 共发送100 ...
- httpModules 不起作用 modules 不起作用 血泪经验
本人也搜索了哏多解决方案.最后都没有解决... 劝您还会放弃把.. 如果非要用,劝您吧代码写到 Global.asax 里...(血泪经验)
- Vue.js vs React vs Angular 深度对比[转]
这个页面无疑是最难编写的,但我们认为它也是非常重要的.或许你曾遇到了一些问题并且已经用其他的框架解决了.你来这里的目的是看看 Vue 是否有更好的解决方案.这也是我们在此想要回答的. 客观来说,作为核 ...
- MFC/Windows API 使用过的函数(持续更新)
/*******************使用默认画笔对象**************************** // //绘制矩形 pDC->MoveTo(50, 50); //返回值是一个指 ...
- Python3 简单验证码识别思路及实例
1.介绍 在爬虫中经常会遇到验证码识别的问题,现在的验证码大多分计算验证码.滑块验证码.识图验证码.语音验证码等四种.本文就是识图验证码,识别的是简单的验证码,要想让识别率更高, 识别的更加准确就需要 ...
- Android -- 状态栏高度
干货 Class<?> c = null; Object obj = null; Field field = null; int x = 0, sbar = 0; try { c = Cl ...
- Android -- java代码设置margin
我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...
- spark 指定相关的参数配置 num-executor executor-memory executor-cores
num-executors参数说明:该参数用于设置Spark作业总共要用多少个Executor进程来执行.Driver在向YARN集群管理器申请资源时,YARN集群管理器会尽可能按照你的设置来在集群的 ...
- HMM与分词、词性标注、命名实体识别
http://www.hankcs.com/nlp/hmm-and-segmentation-tagging-named-entity-recognition.html HMM(隐马尔可夫模型)是用来 ...