我目前正在尝试在Docker容器中复制eureka客户端,但没有这样做。基本上所有容器副本都使用相同的IP地址注册。为了解决此问题,我尝试动态配置Eureka Client,但配置不会卡死。
这是我的配置:
application.yml
server:
port: 8765
spring:
application:
name: zuul-gateway
zuul:
sensitive-headers:
add-proxy-headers: false
eureka:
client:
serviceUrl:
defaultZone: http://${EUREKA:10.0.2.15:8761}/eureka/
register-with-eureka: true
fetch-registry: true
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
prefer-ip-address: true
# Increase the Hystrix timeout to 60s (for all)
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 60000
通过设置instance-id,我可以确保eureka可以选择不同的实例,但是不能解决选择相同IP的问题。
EurekaClientConfig.java
@Configuration
public class EurekaClientConfig {
@Bean
@Autowired
@Profile("docker")
public EurekaInstanceConfigBean eurekaInstanceConfig(final InetUtils inetUtils) throws IOException {
final String hostName = System.getenv("HOSTNAME");
System.out.println("HOSTNAME: " + hostName);
String hostAddress = null;
final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netInt : Collections.list(networkInterfaces)) {
for (InetAddress inetAddress : Collections.list(netInt.getInetAddresses())) {
if (hostName.equals(inetAddress.getHostName())) {
hostAddress = inetAddress.getHostAddress();
System.out.println("IP ADDRESS: " + hostAddress);
}
System.out.printf("%s: %s / %s\n", netInt.getName(), inetAddress.getHostName(), inetAddress.getHostAddress());
}
}
if (hostAddress == null) {
throw new UnknownHostException("Cannot find ip address for hostname: " + hostName);
}
final EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(inetUtils);
config.setHostname(hostName);
config.setIpAddress(hostAddress);
return config;
}
}
当我打印主机和IP地址时,我得到了正确的值。但似乎配置没有卡住。
ZuulGatewayApplication.java
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
@Bean
public EurekaClientConfig eurekaClientConfig() {
return new EurekaClientConfig();
}
@Bean
public TimeTrackerFilter timeTrackerFilter() {
return new TimeTrackerFilter();
}
@Bean
public InfoRequestFilter infoRequestFilter() {
return new InfoRequestFilter();
}
}
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>pt.fabiopina</groupId>
<artifactId>zuul-gateway</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Zuul-Gateway</name>
<description>Netflix Zuul reverse proxy gateway with metrics collection</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</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>
<finalName>ZuulGateway</finalName>
</build>
</project>
你们能发现我在做什么错吗?我不知道问题是否可能是依赖关系或我调用Bean的方式。顺便说一句,当我运行它时,我没有遇到任何错误,但是我的目标是使每个容器副本具有不同的IP地址,但是不幸的是,这种情况没有发生。
谢谢!