2017-06-01 42 views
2

春云尤里卡问题:有3个微服务,1个尤里卡服务器,2个尤里卡客户端;如何使用应用程序名称替换ip:port关于spring cloud eureka?

  1. 微服务A,B使用注释@EnableEurekaClient;

  2. microservice A拥有一个RESTful API“http://localhost:8080/hi”。 api返回“你好”。

  3. 现在,我打电话给api,使用url“http://client/hi”,但它不起作用。

  4. 如何使用应用程序名称替换ip:port关于spring cloud eureka?

bootstrap.yml内容:

spring: 
    application: 
    name: client 

eureka: 
    client: 
    service-url: 
     defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/ 

回答

0

有很多方法可以做到这一点,这取决于你如何调用REST API在你的代码。

如果使用RestTemplate调用API,你可以做到这一点与@LoadBalanced RestTemplate

在你的代码,需要调用REST API,请与@LoadBalanced定义RestTemplate像下面。

@LoadBalanced 
@Bean 
RestTemplate restTemplate(){ 
    return new RestTemplate(); 
} 

当你调用API,只是使用的应用程序名称,而不是host:port像下面。

this.restTemplate.getForObject("http://client/hi", String.class) 

如果使用SpringCloud假死,您可以定义接口来调用你的REST API像下面(无URL)

@FeignClient(name="client") 
public interface ProductResource { 
    : 
} 

而在你的春天启动的应用程序添加注释@EnableFeignClients像下面。

@SpringBootApplication 
@EnableFeignClients 
    : // other annotations you need. 
public class YourAPIInvokerApplication { 

在两种方式中,您都需要添加一些依赖关系。

相关问题