在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或域名的更多相关文章

  1. spring boot / cloud (八) 使用RestTemplate来构建远程调用服务

    spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...

  2. spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法

    spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...

  3. spring boot / cloud (五) 自签SSL证书以及HTTPS

    spring boot / cloud (五) 自签SSL证书以及HTTPS 前言 什么是HTTPS? HTTPS(全称:Hyper Text Transfer Protocol over Secur ...

  4. spring boot / cloud (六) 开启CORS跨域访问

    spring boot / cloud (六) 开启CORS跨域访问 前言 什么是CORS? Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一 ...

  5. Spring Boot使用RestTemplate消费REST服务的几个问题记录

    我们可以通过Spring Boot快速开发REST接口,同时也可能需要在实现接口的过程中,通过Spring Boot调用内外部REST接口完成业务逻辑. 在Spring Boot中,调用REST Ap ...

  6. spring boot / cloud (三) 集成springfox-swagger2构建在线API文档

    spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...

  7. spring boot / cloud (七) 使用@Retryable来进行重处理

    spring boot / cloud (七) 使用@Retryable来进行重处理 前言 什么时候需要重处理? 在实际工作中,重处理是一个非常常见的场景,比如:发送消息失败,调用远程服务失败,争抢锁 ...

  8. spring boot / cloud (十五) 分布式调度中心进阶

    spring boot / cloud (十五) 分布式调度中心进阶 在<spring boot / cloud (十) 使用quartz搭建调度中心>这篇文章中介绍了如何在spring ...

  9. spring boot / cloud (十二) 异常统一处理进阶

    spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...

随机推荐

  1. MsXml创建和解析XML示例

    一.MsXml创建XML文档示例 // XmlCreationDemo.cpp #include <stdlib.h> #include <stdio.h> // 引入MSXM ...

  2. js 循环切换图片

    function changeLot(){ var minIndex = 1; var maxIndex = 100; var curIndex = 10; var src = $("ul ...

  3. DBA-mysql-init-password-5.7

    1.Mysql5.7 Password; 查找临时密码:grep "A temporary password"  /var/log/mysqld.log 修改临时密码:alter ...

  4. ios 写项目的时候遇到的问题及解决方案(3)

    22.看了苹果的文档,里面有这一句话:All launch images must be PNG files and must reside in the top level of your appl ...

  5. Lab_6_SysOps_AutoScaling_Linux_v2.5

    System Operations - Lab 6: Using Amazon Elastic Load Balancer (Linux) - 2.5 ======================== ...

  6. Hibernate5.2关联关系之双向一对多(三)

                                                           Hibernate之双向一对多(三) 一.简介 本篇博文接着上一章的内容接着开展,代码也是 ...

  7. R语言-基本图形

    barplot() 条形图 spine() 棘状图 pie() 饼图 hist() 直方图 plot(density(x))核密度图 boxplot(X) 箱线图 vioplot() 小提琴图 dot ...

  8. 流水灯 外侧<->中间<->外侧

    /* //side<-center->side //side->center<-side //loop *// #include "reg52.h" voi ...

  9. EventBus--介绍

    注意: 1,post()方法里面的类型和onEvent()中的类型要一致., 2,订阅者对象中 必须有 onEvent 的 public 方法     ---public void onEvent(O ...

  10. JavaScript:彻底理解同步、异步和事件循环(Event Loop) (转)

    原文出处:https://segmentfault.com/a/1190000004322358 一. 单线程 我们常说"JavaScript是单线程的". 所谓单线程,是指在JS ...