【问题标题】:eureka unknownHostException in service discovery服务发现中的 eureka unknownHostException
【发布时间】:2017-05-04 22:50:44
【问题描述】:

我有两个微服务,

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

这两个都是在 localhost:8761 上运行的“eureka-server”注册的 DiscoveryClients。

在下面的代码 sn-p 中,我试图从 eureka-client-1 调用 eureka-client-2。我不想调用http://localhost:8082,而是调用http://eureka-client-2,但这会在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>

client 1 的 application.properties,client 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

尤里卡服务器的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,我已经更新了这个问题。 application.properties 文件。
  • 当你打开Eureka服务器localhost:8761/eureka时,你能看到eureka-client-1和eureka-client-1注册了吗?我认为您在 eureka-client-1 中缺少 Ribbon 配置
  • @ootero ,我仍在努力做对。请您通过代码github.com/pranayhere/SpringBootDemo。这将非常有帮助。
  • @ootero 我可以看到 eureka-client-1 和 eureka-client-2 在 localhost:8761 上注册。根据代码,当我点击 localhost:8081 时,它应该返回“我正在 A 与 B 交谈”,但由于我收到了 unknownHostException,结果是“我正在与弹性客户交谈”。

标签: java spring spring-boot netflix-eureka service-discovery


【解决方案1】:

对我来说,解决这个问题唯一需要做的就是添加 spring 注释 @LoadBalanced 到

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

【讨论】:

    【解决方案2】:

    解决方法是你错过了@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】:

      如前所述,我相信您可能在eureka-client-1 中缺少Ribbon 配置。

      首先,我要搬家:

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

      到一个配置类。

      将Ribbon 配置添加到application.yml,类似于:

      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 的注册服务的键

      我写了一篇关于 Microservices Registration and Discovery using Spring Cloud Eureka, Ribbon and Feign 的博客,其中还包括 Discovery 服务器的源代码和使用 Jersey 1 和 Spring MVC 开发的两个客户端。

      【讨论】:

        【解决方案4】:

        以下更改对我有用。

        @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 实例 = loadBalancer.choose("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";
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-11-28
          • 2020-05-22
          • 2016-11-13
          • 2016-05-26
          • 2018-04-15
          • 2017-02-20
          • 2019-05-11
          • 2020-11-12
          • 2018-07-29
          相关资源
          最近更新 更多