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的代理和超时时间的更多相关文章

  1. SpringBoot修改默认端口号,session超时时间

    有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...

  2. 【Spring Cloud 源码解读】之 【如何配置好OpenFeign的各种超时时间!】

    关于Feign的超时详解: 在Spring Cloud微服务架构中,大部分公司都是利用Open Feign进行服务间的调用,而比较简单的业务使用默认配置是不会有多大问题的,但是如果是业务比较复杂,服务 ...

  3. hystrix ,feign,ribbon的超时时间配置,以及原理分析

    背景,网上看到很多关于hystrix的配置都是没生效的,如: 一.先看测试环境搭建: order 服务通过feign 的方式调用了product 服务的getProductInfo 接口 //---- ...

  4. nginx限制上传大小和超时时间设置说明/php限制上传大小

    现象说明:在服务器上部署了一套后台环境,使用的是nginx反向代理tomcat架构,在后台里上传一个70M的视频文件,上传到一半就失效了! 原因是nginx配置里限制了上传文件的大小 client_m ...

  5. (转)nginx限制上传大小和超时时间设置说明/php限制上传大小

    nginx限制上传大小和超时时间设置说明/php限制上传大小 原文:http://www.cnblogs.com/kevingrace/p/6093671.html 现象说明:在服务器上部署了一套后台 ...

  6. Nginx上传和超时时间限制 (php上传限制) - 运维笔记

    现象说明:在服务器上部署了一套后台环境,使用的是nginx反向代理tomcat架构,在后台里上传一个70M的视频文件,上传到一半就失效了! 原因:nginx配置里限制了上传文件的大小 client_m ...

  7. scrapy 如何使用代理 以及设置超时时间

    使用代理 1. 单文件spider局部使用代理 entry = 'http://xxxxx:xxxxx@http-pro.abuyun.com:xxx'.format("帐号", ...

  8. config文件中可以配置查询超时时间

    web.config配置数据库连接 第一种:获取连接字符串 首先要定义命名空间 system.configuration 1.  string connstr= string constr = Con ...

  9. GRUB2配置详解:默认启动项,超时时间,隐藏引导菜单,配置文件详解,图形化配置

    配置文件详解: /etc/default/grub # 设定默认启动项,推荐使用数字 GRUB_DEFAULT=0 # 注释掉下面这行将会显示引导菜单 #GRUB_HIDDEN_TIMEOUT=0 # ...

随机推荐

  1. 架构模式数据源模式之:表数据入口(Table Data Gateway)、行数据入口(Row Data Gateway)、活动记录(Active Record)

    一:表数据入口(Table Data Gateway) 表数据入口提供了用于访问单个表或者视图(也包含了联表查询)的所有SQL,通常一个表一个类.其它代码通过它来实现对数据库的交互.基于这个特点,表数 ...

  2. js如何判断用户是在pc端和还是移动端访问

    js如何判断用户是在pc端和还是移动端访问 来源:A5技术交流 作者:wofa 时间:2014-04-25收藏本页 最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的 ...

  3. Visual Studio提示“无法启动IIS Express Web服务器”的解决方法 vs调试显示无法显示此页面 ,vs调试浏览器白页

    有时,在使用Visual Studio运行ASP.NET项目时,会提示“无法启动IIS Express Web服务器”,无法运行,如图: 这一般出现在重装系统之后,或者项目是从别的电脑上复制过来的.解 ...

  4. POJ 1719 Shooting Contest(二分图匹配)

    POJ 1719 Shooting Contest id=1719" target="_blank" style="">题目链接 题意:给定一个 ...

  5. Parallels Desktop与VirturalBox对比

    笔者用了这两款产品,Parallels 和VirtualBox. 下面各讲下各自的优势吧. 先说说Parallels:Parallels和Mac的系统整合非常紧密,并且对于Mac系统,在性能上有很大的 ...

  6. Validate Binary Search Tree leetcode java

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  7. 不能从const char *转换为LPCWSTR --VS经常碰到

    不能从const char *转换为LPCWSTR 在VC 6.0中编译成功的项目在VS2005 vs2005.vs2008.vs2010中常会出现类型错误. 经常出现的错误是:不能从const ch ...

  8. POJ 1755 Triathlon 半平面交

    看的这里:http://blog.csdn.net/non_cease/article/details/7820361 题意:铁人三项比赛,给出n个人进行每一项的速度vi, ui, wi;  对每个人 ...

  9. Graph 卷积神经网络:概述、样例及最新进展

    http://www.52ml.net/20031.html [新智元导读]Graph Convolutional Network(GCN)是直接作用于图的卷积神经网络,GCN 允许对结构化数据进行端 ...

  10. JPA(二):HellWord工程

    使用JPA持久化对象的操作步骤: 1)创建persistence.xml,在这个文件中配置持久化单元: --- 需要指定跟哪个数据库进行交互: --- 需要指定JPA使用哪个持久化的框架以及配置该框架 ...