我有两个微服务,
- eureka-client-1 运行在 localhost:8081 上
- 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
Eureka
服务器:http://localhost:8761/eureka/时,您能否看到eureka-client-1和eureka-client-2已注册?我认为您在eureka-client-1中缺少Ribbon
配置。 - ooteroeureka.instance.hostname=localhost
- გენო მუმლაძე