我尝试通过直接提供网址,通过zuul
呼叫启用RestTemplate
的服务器。
例如:restTemplate.getForObject(“http://localhost:8090/emp-api”,Employee [] .class);
但它给我一个错误:
java.lang.IllegalStateException:没有可用于localhost的实例 在org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:90)〜[spring-cloud-netflix-core-1.2.3.RELEASE.jar:1.2.3.RELEASE]
详细问题: 我有四个项目(Github链接(分支主):https://github.com/vickygupta0017/microservice-poc)
microservice-consumer(spring-mvc)端口:8087
如果我直接从浏览器(“http://localhost:8090/emp-api”)调用zuul server
,那么它会将请求重定向到生产者。
但是如果我通过RestTemplate
从消费者那里调用这个URL,那么它就会给我这个错误。
有关信息:如果我没有使用zuul服务器,那么我可以使用RestTemplate
成功地从'microservice-consumer'调用'microservice-producer'。
答案 0 :(得分:2)
我已成功执行了以下更改的代码,删除了
enablediscovery和LoadBalanced注释
如果这将启用,那么总是休息模板将转到eureka进行发现,因为您正在使用代理服务器,那么您的消费者不需要发现,因为您提供绝对路径也从application.yml中删除了eureka配置。
您不能同时使用发现和绝对路径,一次只能使用一个。
package com.eureka.discovery.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.eureka.discovery.client.bo.Employee;
@SpringBootApplication
@RestController
//@EnableDiscoveryClient
public class UserApplication {
@Bean
//@Loadbalancer
RestTemplate restTemplate(){
return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hi")
public String hi() {
Employee[] employeeList = this.restTemplate.getForObject("http://localhost:8090/emp-api", Employee[].class);
return String.format("%s, %s!", "Testing", "Abhay");
}
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
答案 1 :(得分:1)
groupBy
实例可能已配置为通过RestTemplate
查找服务localhost
,而不是假设Eureka
已经是http://localhost:....
答案 2 :(得分:0)
如果你使用ip和port调用服务,则不需要Eureka,因为你正在进行微服务通信,而eureka已经在服务发现和注册表中。你想做负载平衡,然后zuul会出现在图片中。
使用mricroservice名称而不是ip和port更改消费者中的代码,然后它将起作用。
@SpringBootApplication
@EnableDiscoveryClient
public class WebclientConsumerMicroserviceApplication {
public static final String EMPLOYEE_SERVICE_URL = "http://employee-microservice/";
并且还会更改employeeServicesImpl中剩余的uri。
public List<Employee> getEmployeeList() {
// Employee[] employeeList = restTemplate.getForObject("http://employee-microservice/employees", Employee[].class);
Employee[] employeeList = restTemplate.getForObject(url+"employees, Employee[].class);
它将起作用就这样开始:
注意:我们只需要通过服务名称调用服务,并且需要避免使用ip和port(它们是动态的)。
如果您想使用zuul运行,请使用zuul服务名称:zuul-gateway,消费者具有以下配置
zuul-gateway:
ribbon:
eureka:
enabled: false
listOfServers: localhost:8090
ServerListRefreshInterval: 1500