Spring Boot+Cloud RestTemplate 调用IP或域名
在SpringBoot+Cloud的项目中,我们使用了自动配置的OAuth2RestTemplate,RestTemplate,但是在使用这些restTemplate的时候,url必须是服务的名称,如果要调用真实的域名或者ip的url,会有错误,如下:
UriComponents b = UriComponentsBuilder.fromUriString("http://www.baidu.com").build();
String str = rest.getForObject(b.toUri(), String.class);
错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No instances available for www.baidu.com
查看错误的跟踪链发现,自动注入的restTemplate中加入了cloud.netflix*包下面的interceptor,所以默认会通过RibbonLoadBalancerClient去查找注册中心的instances,如上面的代码,www.baidu.com肯定不存在,所以就报错了。 找了半天,解决方案是,自己自定义或者产生一个resttemplate即可,不适用自动配置的。 配置:
@Bean(name="remoteRestTemplate")
public RestTemplate restTemplate() {
return new RestTemplate();
}
使用:
@Autowired
private OAuth2RestTemplate o2rest;
@Autowired
@Qualifier(value = "remoteRestTemplate")
private RestTemplate rest;
Spring Boot+Cloud RestTemplate 调用IP或域名的更多相关文章
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...
- spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法
spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...
- spring boot / cloud (五) 自签SSL证书以及HTTPS
spring boot / cloud (五) 自签SSL证书以及HTTPS 前言 什么是HTTPS? HTTPS(全称:Hyper Text Transfer Protocol over Secur ...
- spring boot / cloud (六) 开启CORS跨域访问
spring boot / cloud (六) 开启CORS跨域访问 前言 什么是CORS? Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一 ...
- Spring Boot使用RestTemplate消费REST服务的几个问题记录
我们可以通过Spring Boot快速开发REST接口,同时也可能需要在实现接口的过程中,通过Spring Boot调用内外部REST接口完成业务逻辑. 在Spring Boot中,调用REST Ap ...
- spring boot / cloud (三) 集成springfox-swagger2构建在线API文档
spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...
- spring boot / cloud (七) 使用@Retryable来进行重处理
spring boot / cloud (七) 使用@Retryable来进行重处理 前言 什么时候需要重处理? 在实际工作中,重处理是一个非常常见的场景,比如:发送消息失败,调用远程服务失败,争抢锁 ...
- spring boot / cloud (十五) 分布式调度中心进阶
spring boot / cloud (十五) 分布式调度中心进阶 在<spring boot / cloud (十) 使用quartz搭建调度中心>这篇文章中介绍了如何在spring ...
- spring boot / cloud (十二) 异常统一处理进阶
spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...
随机推荐
- linux下查看系统信息
lspci: 这个工具用来查看所有连接到pci总线上的所有设备信息. 如果有些系统没有lspci,安装后即可使用,有些老死的版本中需要root权限才可使用. 一些用法: lspci 列出所有 ...
- Web 应用程序中的安全向量 – ASP.NET MVC 4 系列
Web 程序运行在标准的.基于文本的协议(HTTP 和 HTML)之上,所以特别容易受到自动攻击的伤害.本章主要介绍黑客如何滥用应用程序,以及针对这些问题的应对措施. 威胁:跨站脚本 ...
- Android应用开发-Activity(重制版)
Android四大组件:Activity,Service,Broadcast Receiver,Content Provider Activity是Context的子类,同时实现了Window.Cal ...
- Sublime Text 注册码 License Key
Sublime Text (3103版本可用) 注册码 License Key
- ruby中 Win32ole的各应用操作方法(word excel Outlook IE)
Win32ole为标准Ruby发行版的一部分.Win32ole是访问Windows自动化的接口,可以让Ruby和Windows应用进行交互.具体说来Win32ole可以操作Word,Excel,IE, ...
- JQuery获取元素本身HTML
$('<p>').append($(this).clone()).html() 原理:创建一个匿名对象,克隆本身,追加到匿名对象中,再获取匿名对象的HTML
- 一个人独立开发 3D 游戏引擎可能吗?
作者:孙志超链接:https://www.zhihu.com/question/24733255/answer/42000966来源:知乎著作权归作者所有,转载请联系作者获得授权. 当然可以,但难道有 ...
- 解决Dropbox无法连接的问题
同步共享服务Dropbox从6月18日开始再次遭到封锁,原因是DNS污染.Dropbox上次在2010年5月曾遭到IP封锁和网址关键字过 滤,2012年5月除文件外链功能外其它功能可正常访问:2014 ...
- SQL Server 维护计划实现数据库备份(Step by Step)(转)
SQL Server 维护计划实现数据库备份(Step by Step) 一.前言 SQL Server 备份和还原全攻略,里面包括了通过SSMS操作还原各种备份文件的图形指导,SQL Server ...
- (Array)169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...