application.properties:

  1. #代理设置
  2. proxy.enabled=false
  3. proxy.host=192.168.18.233
  4. proxy.port=8888
  5.  
  6. #REST超时配置
  7. rest.ReadTimeout=35000
  8. rest.ConnectTimeout=5000

代理配置类:

  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.stereotype.Component;
  3.  
  4. import lombok.Data;
  5.  
  6. /**
  7. * 网络代理设置
  8. *
  9. * @author yangzhilong
  10. *
  11. */
  12. @Component
  13. @ConfigurationProperties(prefix="proxy")
  14. @Data
  15. public class ProxyConfig {
  16. /**
  17. * 是否启用代理
  18. */
  19. private Boolean enabled;
  20. /**
  21. * 代理主机地址
  22. */
  23. private String host;
  24. /**
  25. * 代理端口
  26. */
  27. private Integer port;
  28. }

SpringBoot的Configuration:

  1. import java.net.InetSocketAddress;
  2. import java.net.Proxy;
  3. import java.net.SocketAddress;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.http.client.SimpleClientHttpRequestFactory;
  11. import org.springframework.web.client.RestTemplate;
  12.  
  13. import com.yzl.vo.ProxyConfig;
  14.  
  15. @Configuration
  16. @ConditionalOnClass(ProxyConfig.class)
  17. public class RestConfiguration {
  18. @Value("${rest.ReadTimeout}")
  19. private int readTimeout;
  20. @Value("${rest.ConnectTimeout}")
  21. private int connectionTimeout;
  22. @Autowired
  23. private ProxyConfig proxyConfig;
  24.  
  25. @Bean
  26. public SimpleClientHttpRequestFactory httpClientFactory() {
  27. SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
  28. httpRequestFactory.setReadTimeout(readTimeout);
  29. httpRequestFactory.setConnectTimeout(connectionTimeout);
  30.  
  31. if(proxyConfig.getEnabled()){
  32. SocketAddress address = new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort());
  33. Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
  34. httpRequestFactory.setProxy(proxy);
  35. }
  36.  
  37. return httpRequestFactory;
  38. }
  39.  
  40. @Bean
  41. public RestTemplate restTemplate(SimpleClientHttpRequestFactory httpClientFactory) {
  42. RestTemplate restTemplate = new RestTemplate(httpClientFactory);
  43. return restTemplate;
  44. }
  45. }

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

  1. package com.yzl.autoconfig;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  7. import org.springframework.web.client.RestTemplate;
  8.  
  9. import com.yzl.util.RestClient;
  10.  
  11. /**
  12. * 工具类引导装配类
  13. * @author yangzhilong
  14. *
  15. */
  16. @Configuration
  17. public class RestClientAutoConfiguration {
  18. @Value("${rest.config.connectTimeout:10000}")
  19. private int connectTimeout;
  20. @Value("${rest.config.readTimeout:30000}")
  21. private int readTimeout;
  22.  
  23. /**
  24. * 使用Bootstrap来装配RestClient中的RestTemplate属性,
  25. * 避免直接装配RestTemplate来污染了正常的spring Cloud的调用
  26. * @return
  27. */
  28. @Bean
  29. public RestClientBootstrap bootstrap(){
  30. HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
  31. httpRequestFactory.setConnectTimeout(connectTimeout);
  32. httpRequestFactory.setReadTimeout(readTimeout);
  33. RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
  34. RestClient.setRestTemplate(restTemplate);
  35. return new RestClientBootstrap();
  36. }
  37.  
  38. /**
  39. * 空的引导类
  40. * @author yangzhilong
  41. *
  42. */
  43. static class RestClientBootstrap {
  44.  
  45. }
  46. }

RestClient工具类:

  1. package com.nike.gcsc.auth.utils;
  2.  
  3. import java.util.Map;
  4.  
  5. import org.springframework.http.HttpEntity;
  6. import org.springframework.http.HttpHeaders;
  7. import org.springframework.http.HttpMethod;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.util.LinkedMultiValueMap;
  10. import org.springframework.util.MultiValueMap;
  11. import org.springframework.web.client.RestTemplate;
  12.  
  13. import com.alibaba.fastjson.JSON;
  14.  
  15. /**
  16. * HTTP Rest Util
  17. * @author yangzhilong
  18. *
  19. */
  20. public class RestClient {
  21.  
  22. private static RestTemplate restTemplate;
  23.  
  24. /**
  25. * @param client
  26. */
  27. public static void setRestTemplate(RestTemplate client) {
  28. restTemplate = client;
  29. }
  30.  
  31. /**
  32. *
  33. * @param <T>
  34. * @param url
  35. * @param clasz
  36. * @return
  37. */
  38. public static <T> T get(String url, Class<T> clasz) {
  39. return restTemplate.getForObject(url , clasz);
  40. }
  41.  
  42. /**
  43. *
  44. * @param <T>
  45. * @param url
  46. * @param headMap
  47. * @param bodyObj
  48. * @param clasz
  49. * @return
  50. */
  51. public static <T> T postJson(String url, Map<String, String> headMap, Object bodyObj, Class<T> clasz) {
  52. HttpHeaders headers = new HttpHeaders();
  53. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  54. headers.setContentType(type);
  55. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  56. if(null != headMap) {
  57. headMap.entrySet().forEach(item -> {
  58. headers.add(item.getKey(), item.getValue());
  59. });
  60. }
  61. String result = null;
  62. if(bodyObj == null){
  63. result = "{}";
  64. }else{
  65. result = JSON.toJSONString(bodyObj);
  66. }
  67. HttpEntity<String> formEntity = new HttpEntity<String>(result,headers);
  68. return restTemplate.postForObject(url , formEntity, clasz);
  69. }
  70.  
  71. /**
  72. *
  73. * @param <T>
  74. * @param url
  75. * @param attrMap
  76. * @param clasz
  77. * @return
  78. */
  79. public static <T> T postForm(String url, Map<String , String> attrMap, Class<T> clasz){
  80. HttpHeaders headers = new HttpHeaders();
  81. headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  82. MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
  83. attrMap.entrySet().forEach(item -> {
  84. params.add(item.getKey() , item.getValue());
  85.  
  86. });
  87. HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
  88. return restTemplate.exchange(url, HttpMethod.POST, requestEntity, clasz).getBody();
  89. }
  90.  
  91. }

然后实际发起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. 关于 as 播放器的记录

    一:文件结构 1:代码 2:编译后   二:IDE展示区 1处还有6个层,2处为代码和设计文件,3处是主类. 资源文件的位置如下:   三:数据交互 AS中代码: JS中代码: 更多需要注意的地方在这 ...

  2. 基于Python的卷积神经网络和特征提取

    基于Python的卷积神经网络和特征提取 用户1737318发表于人工智能头条订阅 224 在这篇文章中: Lasagne 和 nolearn 加载MNIST数据集 ConvNet体系结构与训练 预测 ...

  3. C# 根据注册表获取当前用户的常用目录整理

    1.使用C#获取当前程序或解决方案的路径 2.使用C#获取当前登录用户的相关目录 3.也可以获取当前系统通用目录 4.获取Windows系统的目录,从注册表中获取. 一.当前用户的目录,HKEY_Cu ...

  4. Hadoop2.6.0版本号MapReudce演示样例之WordCount(一)

    一.准备測试数据 1.在本地Linux系统/var/lib/hadoop-hdfs/file/路径下准备两个文件file1.txt和file2.txt,文件列表及各自内容例如以下图所看到的: wate ...

  5. 混沌分形之迭代函数系统(IFS)

    IFS是分形的重要分支.它是分形图像处理中最富生命力而且最具有广阔应用前景的领域之一.这一工作最早可以追溯到Hutchinson于1981年对自相似集的研究.美国科学家M.F.Barnsley于198 ...

  6. BUG的严重级别分类 BUG状态标准

    英文参考 BUG的严重级别分类 Severity This field describes the impact of a bug. Blocker Blocks development and/or ...

  7. Java复习3-类的继承

    前言 本次学习面向对象设计的另外一个基本概念:继承(inheritance).这是Java程序设计中的一项核心技术.另外,还要学习反射(reflection)的概念. 继承 类.超类.子类 publi ...

  8. 【转】TensorFlow四种Cross Entropy算法实现和应用

    http://www.jianshu.com/p/75f7e60dae95 作者:陈迪豪 来源:CSDNhttp://dataunion.org/26447.html 交叉熵介绍 交叉熵(Cross ...

  9. mysql的sql分页函数limit使用 (转)

    http://www.cnblogs.com/beijingstruggle/p/5631603.html mysql的sql分页函数limit使用 My sql数据库最简单,是利用mysql的LIM ...

  10. Centos6.4下安装protobuf及简单使用

    1.protobuf是google公司提出的数据存储格式,详细介绍可以参考:https://code.google.com/p/protobuf/ 2.下载最新的protobuf,下载地址:https ...