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 ...
随机推荐
- LeetCode 【Single Number I II III】
Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...
- MATLAB 绘图时的相关心得
matlab中如何调整legend的位置? legend('sinx',-1); %----位于图形框外面-----------------------legend('sinx',0);------- ...
- 在实现和使用上与select和poll有很大差异
在看此课程的读者,希望先阅读关于函数基础内容 函数定义与函数作用域 的章节,因为此课程或多或少会涉及函数基础的内容,而基础内容,本人放在 函数定义函数作用域 章节. 本文直接赘述函数参数与闭包,若涉及 ...
- Oracle 11.2.0.1的一个Bug,客户端报ORA-03113: 通信通道的文件结尾
半小时前,一个项目反馈应用系统部分功能报错,ORA-03113: 通信通道的文件结尾.好像是个常见的错误. 异常信息:ORA-03113: 通信通道的文件结尾 进程 ID: 2392 会话 ID: 2 ...
- nginx-502错误,老是提示busy.优化php-fpm如下
[global]pid = /usr/local/php/var/run/php-fpm.piderror_log = /usr/local/php/var/log/php-fpm.loglog_le ...
- Javascript模式(第五章对象创建模式)------读书笔记
一 命名空间模式 1 命名空间模式的代码格式 var MYAPP={ name:"", version:"1.0", init:function(){ } }; ...
- AngularJS之directive
AngularJS之directive AngularJS是什么就不多舌了,这里简单介绍下directive.内容基本上是读书笔记,所以如果你看过<AngularJS up and runnin ...
- 安装和配置CentOS时钟同步服务
Type the following command to install ntp: # yum install -y ntp Turn on service: # chkconfig ntpd on ...
- [PHP] - Laravel 5 的 Hello Wold
吐槽一段 整了半天,Laravel下载麻烦得可以去死.先要安装composer,而composer又被共墙了,之后又要安装git,安装完git还要注册git,等等.... 最终放弃这种玩法,太恶心了. ...
- jQuery EasyUI:根据数据库内容生成适合于easyui-tree的JSON数据格式
1,jQuery EasyUI中easyui-tree特定的JSON数据格式 [ {"id":1,"text":"某公司","ch ...