【发布时间】:2020-09-06 17:53:53
【问题描述】:
目前正在使用 eureka 服务发现器对我们的 Springboot 微服务后端进行集群化。第一个问题是确保服务发现器不选择入口 IP 地址,而是从覆盖网络中选择 IP 地址。经过一番搜索,我发现了一篇建议以下 Eureka 客户端配置的帖子:
@Configuration
@EnableConfigurationProperties
public class EurekaClientConfig {
private ConfigurableEnvironment env;
public EurekaClientConfig(final ConfigurableEnvironment env) {
this.env = env;
}
@Bean
@Primary
public EurekaInstanceConfigBean eurekaInstanceConfigBean(final InetUtils inetUtils) throws IOException {
final String hostName = System.getenv("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.printf("Inet used: %s", netInt.getName());
}
System.out.printf("Inet %s: %s / %s\n", netInt.getName(), inetAddress.getHostName(), inetAddress.getHostAddress());
}
}
if (hostAddress == null) {
throw new UnknownHostException("Cannot find ip address for hostname: " + hostName);
}
final int nonSecurePort = Integer.valueOf(env.getProperty("server.port", env.getProperty("port", "8080")));
final EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);
instance.setHostname(hostName);
instance.setIpAddress(hostAddress);
instance.setNonSecurePort(nonSecurePort);
System.out.println(instance);
return instance;
}
}
部署新发现器后,我得到了正确的结果,并且服务发现器具有正确的覆盖 IP 地址。
为了理解下一步,这里是一些关于我们运行这个 docker swarm 的环境的信息。我们目前有 2 个液滴,一个用于开发,另一个用于生产。目前我们只在开发服务器上进行 Swarmify。生产已经好几个月没有动了。
下一步是部署一个发现客户端 Springboot 应用程序,该应用程序将连接到正确的服务发现器,并且还具有过度的 IP 地址而不是入口。但是当我构建应用程序时,它总是连接到我们在 docker swarm 之外的生产服务发现器进入另一个 droplet。我可以看到应用程序正在部署在 swarm 上,但从生产服务器查看 Eureka 仪表板,我可以看到它已连接到它。
第二个问题是应用程序也有你在上面看到的 EurekaClient 配置,但它被忽略了。启动应用时,甚至方法内的日志也不会被调用。
这是来自 Discovery Client 应用程序的配置:
eureka:
client:
serviceUrl:
defaultZone: service-discovery_service:8761/eureka
enabled: false
instance:
instance-id: ${spring.application.name}:${random.value}
prefer-ip-address: true
spring:
application:
name: account-service
我假设您可以使用 defaultZone 指向正确的服务发现器,但我可能错了。
【问题讨论】:
标签: spring-boot microservices docker-swarm netflix-eureka service-discovery