我正在尝试通过ECS Fargate部署微服务。我正在使用Eureka作为发现服务。 客户已在eureka注册,但无法沟通。
首先,我尝试以这种方式获取IP
@Bean
@Autowired
public EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils){
EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(inetUtils);
String ip = null;
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
config.setNonSecurePort(8090);
config.setIpAddress(ip);
config.setPreferIpAddress(true);
return config;
}
此解决方案将引发连接被拒绝的异常。
我的下一个尝试是用负载均衡器的URL替换ip。
@Bean
@Autowired
public EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils){
EurekaInstanceConfigBean config = new EurekaInstanceConfigBean(inetUtils);
config.setNonSecurePort(8090);
config.setIpAddress(LOADBALANCER_URL);
config.setPreferIpAddress(true);
return config;
}
但这会抛出com.netflix.client.ClientException: Load balancer does not have available server for client: database
我希望这里能帮助我!
答案 0 :(得分:1)
您可以尝试执行以下步骤,这说明了如何在AWS Fargate上将Eureka部署为容器。 https://karthikeayan.github.io/post/netflix-eureka-on-aws-fargate/
答案 1 :(得分:0)
根据您的评论,您可以做几件事。
首先,由于您正在docker容器中运行,因此无需声明此@Bean
,可以将其替换为以下配置属性:
eureka:
instance:
non-secure-port: 8090
prefer-ip-address: true
要选择正确的IP地址,您必须配置some network interfaces to be ignored,可以通过添加以下内容来实现:
spring:
cloud:
inetutils:
ignoredInterfaces:
- docker0
- veth.*
希望这对您有所帮助。
答案 2 :(得分:0)
有几种解决方法,但是我解决的一种方法是创建一个bean post处理器,该处理器在postProcessAfterInitialization生命周期挂钩期间拦截EurekaInstanceConfigBean。
您可以使用InetAddress.getLocalHost()。getHostAddress()获得Fargate实例的IP地址。一旦确定该bean是EurekaInstanceConfigBean的实例,就可以使用获得的Fargate专用IP设置instanceId,ipAddress,主机名,statusPageUrl和healthCheckUrl。
只要所有服务都在同一VPC上运行,您就应该能够使用可用于服务间通信的地址正确地向Eureka注册服务。
编辑(添加代码段):
@Component
@Profile("dev")
public class EurekaInstanceConfigBeanPostProcessor implements BeanPostProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(EurekaInstanceConfigBeanPostProcessor.class);
@Value("${spring.application.name}")
private String serviceName;
@Value("${server.port}")
private int port;
private String fargateIp;
{
try {
fargateIp = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
LOGGER.warn("Could not get the Fargate instance ip address.");
}
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof EurekaInstanceConfigBean) {
LOGGER.info("EurekaInstanceConfigBean detected. Setting IP address to {}", fargateIp);
EurekaInstanceConfigBean instanceConfigBean = ((EurekaInstanceConfigBean) bean);
instanceConfigBean.setInstanceId(fargateIp + ":" + serviceName + ":" + port);
instanceConfigBean.setIpAddress(fargateIp);
instanceConfigBean.setHostname(fargateIp);
instanceConfigBean.setStatusPageUrl("http://" + fargateIp + ":" + port + "/actuator/info");
instanceConfigBean.setHealthCheckUrl("http://" + fargateIp + ":" + port + "/actuator/health");
}
return bean;
}
}