在本地搭建eureka的消费者调用时报错
java.lang.IllegalStateException: No instances available for 127.0.0.1
package com.fqyd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConsumerApplication.class, args);
}
/**
* 解决restTemplate bean找不到的问题,加入spring boot容器中@bean
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.fqyd.api.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* Description:
* Author: wude
* Date: 2019/7/10 17:29
* Modified By:
*/
@RestController
public class OrderController {
//restTemplate是由spring web组件提供的 默认是聚合rabiion负载均衡起
//rest方式底层是采用httclient技术
@Autowired
private RestTemplate restTemplate;
/**
* 在springcloud中,又两种方式调用rest,fegin(springcloud)
* @return
*/
//订单服务调用会员服务
@RequestMapping("getOrder")
public String getOrder(){
String result = restTemplate.getForObject("http://127.0.0.1:8000/getMember", String.class);
System.out.println("订单服务调用会员服务返回结果result:"+result);
return result;
};
}
经过多次分析发现,增加@LoadBalanced,就不能使用127.0.0.1,只能使用应用名(等于上图的fqyd-member不区分大小写)
欢迎关注公众号,免费领取各种学习视频,学习相关java各个技术点面试知识,有相关学习疑问或者面试困惑,也可以通过公众号私信博主