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 # ...
随机推荐
- 关于 as 播放器的记录
一:文件结构 1:代码 2:编译后 二:IDE展示区 1处还有6个层,2处为代码和设计文件,3处是主类. 资源文件的位置如下: 三:数据交互 AS中代码: JS中代码: 更多需要注意的地方在这 ...
- 基于Python的卷积神经网络和特征提取
基于Python的卷积神经网络和特征提取 用户1737318发表于人工智能头条订阅 224 在这篇文章中: Lasagne 和 nolearn 加载MNIST数据集 ConvNet体系结构与训练 预测 ...
- C# 根据注册表获取当前用户的常用目录整理
1.使用C#获取当前程序或解决方案的路径 2.使用C#获取当前登录用户的相关目录 3.也可以获取当前系统通用目录 4.获取Windows系统的目录,从注册表中获取. 一.当前用户的目录,HKEY_Cu ...
- Hadoop2.6.0版本号MapReudce演示样例之WordCount(一)
一.准备測试数据 1.在本地Linux系统/var/lib/hadoop-hdfs/file/路径下准备两个文件file1.txt和file2.txt,文件列表及各自内容例如以下图所看到的: wate ...
- 混沌分形之迭代函数系统(IFS)
IFS是分形的重要分支.它是分形图像处理中最富生命力而且最具有广阔应用前景的领域之一.这一工作最早可以追溯到Hutchinson于1981年对自相似集的研究.美国科学家M.F.Barnsley于198 ...
- BUG的严重级别分类 BUG状态标准
英文参考 BUG的严重级别分类 Severity This field describes the impact of a bug. Blocker Blocks development and/or ...
- Java复习3-类的继承
前言 本次学习面向对象设计的另外一个基本概念:继承(inheritance).这是Java程序设计中的一项核心技术.另外,还要学习反射(reflection)的概念. 继承 类.超类.子类 publi ...
- 【转】TensorFlow四种Cross Entropy算法实现和应用
http://www.jianshu.com/p/75f7e60dae95 作者:陈迪豪 来源:CSDNhttp://dataunion.org/26447.html 交叉熵介绍 交叉熵(Cross ...
- mysql的sql分页函数limit使用 (转)
http://www.cnblogs.com/beijingstruggle/p/5631603.html mysql的sql分页函数limit使用 My sql数据库最简单,是利用mysql的LIM ...
- Centos6.4下安装protobuf及简单使用
1.protobuf是google公司提出的数据存储格式,详细介绍可以参考:https://code.google.com/p/protobuf/ 2.下载最新的protobuf,下载地址:https ...