尝试进行服务发现时出现Eureka未知主机异常

8

我有两个微服务,

  1. eureka-client-1 运行在 localhost:8081 上
  2. eureka-client-2 运行在 localhost:8082 上

这两个微服务都是 DiscoveryClients,注册到运行在 localhost:8761 上的 'eureka-server'。

在下面的代码片段中,我试图从 eureka-client-1 调用 eureka-client-2。但是,我想调用 http://eureka-client-2 而不是 http://localhost:8082,但是这会在 Eureka 服务发现期间抛出 java.net.UnknownHostException 异常。

经过搜索,我发现需要使用“Brixton”才能完成它。

是否有一种方法可以在 Camden.SR3 中实现呢?

请建议。

@Component
public class HystrixDemoService {   

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

    @HystrixCommand(fallbackMethod = "getFallbackCustomerName")
    public String getCustomerName() {
        RestTemplate restTemplate = new RestTemplate();
        URI uri = URI.create("http://eureka-client-2");     // fails here
        return restTemplate.getForObject(uri, String.class);
    }

    public String getFallbackCustomerName() {
        System.out.println("coming inside fallback method");
        return "Resillient Customer";
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo-pranay-eureka-client1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-pranay-eureka-client1</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

客户端1的application.properties,客户端2类似(只需更改名称即可,例如eureka-client-2)

spring.application.name=eureka-client-1
server.port=8081
eureka:
  client:
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    statusPageUrlPath: /info
    healthCheckUrlPath: /health

eureka服务器的application.properties

spring.application.name=eureka-service
server.port=8761
eureka:
  client:
    registerWithEureka: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    statusPageUrlPath: /info
    healthCheckUrlPath: /health

你能展示一下你的配置文件吗?(application.yml,application.properties,bootstrap.yml等) - ootero
嗨 @ootero,我已经更新了问题中的 application.properties 文件。 - prranay
当您打开Eureka服务器:http://localhost:8761/eureka/时,您能否看到eureka-client-1和eureka-client-2已注册?我认为您在eureka-client-1中缺少Ribbon配置。 - ootero
@ootero,我仍然在努力弄清楚它。您能否看一下这个代码https://github.com/pranayhere/SpringBootDemo。这将非常有帮助。 - prranay
在你的微服务中添加以下属性。 eureka.instance.hostname=localhost - გენო მუმლაძე
显示剩余2条评论
4个回答

26

对我来说,解决这个问题的唯一方法是在代码中添加Spring注解@LoadBalanced。

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

9
解决方案是您缺少@LoadBalanced。如果您使用Spring Boot或Sprint Cloud,我建议使用RestTemplateBuilder builder
  @Bean
  @LoadBalanced
  public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.build();
  }

如果您只使用Spring,我建议您使用new RestTemplate()
  @Bean
  @LoadBalanced
  public RestTemplate restTemplate() {
      return new RestTemplate();
  }

3
以下更改对我有效。
(Note: 保留了原文中的HTML标签)
@SpringBootApplication
public class DemoPranayEurekaClient1Application {
    public static void main(String[] args) {
        SpringApplication.run(DemoPranayEurekaClient1Application.class, args);
    }
}

@EnableDiscoveryClient
@EnableCircuitBreaker
@RestController
class HystrixDemoApplication {
    @Autowired
    HystrixDemoService hystrixDemoService;

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

    @RequestMapping("/")
    public String name() {
        String str = hystrixDemoService.getCustomerName();
        return "I'm A talking to "+str;
    }

}

以下是用于选择eureka-client-2实例的代码行...

ServiceInstance instance = loadBalancer.choose("eureka-client-2");

(该代码用于从负载平衡器中选择eureka-client-2的一个实例。)
@Component
public class HystrixDemoService {

    @Autowired
    private LoadBalancerClient loadBalancer;

    @HystrixCommand(fallbackMethod = "getFallbackCustomerName")
    public String getCustomerName() {
        RestTemplate restTemplate = new RestTemplate();
        ServiceInstance instance = loadBalancer.choose("eureka-client-2");
        URI uri = instance.getUri();
        return restTemplate.getForObject(uri, String.class);
    }

    public String getFallbackCustomerName() {
        System.out.println("coming inside fallback method");
        return "Resillient Customer";
    }
}

2

如之前所评论的,我认为您可能遗漏了eureka-client-1中的Ribbon配置。

首先,我建议您移动:

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

转换为配置类。

application.yml中添加Ribbon配置,例如:

the-eureka-client-2:
   ribbon:
     # Eureka vipAddress of the target service
     DeploymentContextBasedVipAddresses: eureka-client-2

     #listOfServers: localhost:${SERVER.PORT}
     NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList

     # Interval to refresh the server list from the source (ms)
     ServerListRefreshInterval: 30000

HystrixDemoService类中注入restTemplate而不是为每个请求实例化一个新的。RestTemplate是线程安全的:

@Component
public class HystrixDemoService {   

    @Autowired
    public RestTemplate restTemplate;
...
    @HystrixCommand(fallbackMethod = "getFallbackCustomerName")
    public String getCustomerName() {
      URI uri = URI.create("http://eureka-client-2");
      return this.restTemplate.getForObject(uri, String.class);
    }

the-eureka-client-2是一个关键字,用于映射名称为eureka-client-2的已注册服务。

我在博客《使用Spring Cloud Eureka、Ribbon和Feign进行微服务注册和发现》中介绍了Discovery服务器和两个客户端(分别使用Jersey 1和Spring MVC开发),并提供了源代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接