Spring Cloud Eureka 服务器不相互复制,显示警告 [英] Spring cloud Eureka server is NOT replicating each other, displaying warning

查看:147
本文介绍了Spring Cloud Eureka 服务器不相互复制,显示警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 http://localhost:8761<打开页面时,我在 Spring Cloud 中使用两个 Eureka 服务器相互复制/a>,我看到了这条消息:

I am using two Eureka server in spring cloud to replicate each other, when I open the page at http://localhost:8761, I saw this message:

更新少于阈值.自我保护模式已关闭.如果出现网络/其他问题,这可能无法保护实例到期.

RENEWALS ARE LESSER THAN THE THRESHOLD. THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

尤里卡 application.xml 是这样的:

The eureka application.xml is this:

server:
  port: ${server.instance.port:5678}
spring:
  application:
    name: nodeservice

sidecar:
  port: ${nodeserver.instance.port:3000}
  health-uri: http://localhost:${nodeserver.instance.port:3000}/health.json

eureka:
  instance:
    hostname: ${nodeserver.instance.name:localhost}
    preferIpAddress: ${preferipaddress:false}
    leaseRenewalIntervalInSeconds: 5 #default is 30, recommended to keep default
    metadataMap:
      instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

所以如果我去http://localhost:8761,我会看到所有的服务都注册了,但是如果我去到http://localhost:8762,然后看到没有微服务注册.

So if I go to http://localhost:8761, I see all the services registered, but if I go to http://localhost:8762, I then see no micro-service registered.

知道为什么吗?

推荐答案

Eureka: 只注册第一个成功的 url.在你的情况下,第一个成功的 url 是 http://localhost:8761/eureka/所以它不会继续注册下一个 url http://localhost:8762/eureka/.

Eureka: register only the first success url. In your case the first success url is http://localhost:8761/eureka/ so it's not continue to register the next url http://localhost:8762/eureka/.

您可以通过以下方式覆盖它:

You can override that by:

应用程序.yml

 eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
      additionalZones: http://localhost:8762/eureka

您的应用程序.java

Your Application.java

@SpringBootApplication
@EnableEurekaClient
public class Application implements ApplicationContextAware {


    @Value("${eureka.client.serviceUrl.additionalZones:}")
    String additionalZones;

    ConfigurableApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public Map<String, EurekaClient> additionalEurekaClients(ApplicationInfoManager manager,
                                                             @Autowired(required = false) HealthCheckHandler healthCheckHandler) {
        HashMap clients = new HashMap<>();

        if(Text.isEmpty(additionalZones))
            return clients;

        String[] hosts = additionalZones.split(",");
        for(int i=0; i < hosts.length; i++)
        {
            EurekaClient client = new CloudEurekaClient(manager, new SimpleEurekaClientConfig(hosts[i].trim(),"defaultZone"), null,
                    this.applicationContext);
            client.registerHealthCheck(healthCheckHandler);
            String clientName = "client_"+ (i+1);
            clients.put(clientName, client);
        }

        return clients;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = (ConfigurableApplicationContext) applicationContext;
    }


    @PreDestroy
    public void unRegisterInAllConfiguredDiscovery() {
        Map<String, EurekaClient> additionalEurekaClients = this.applicationContext.getBean("additionalEurekaClients", Map.class);
        additionalEurekaClients.forEach((k, v) -> v.shutdown());
    }
}

简单的EurekaClient.java

SimpleEurekaClient.java

package com.netflix.eureka;

import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import java.util.Arrays;
import java.util.List;

public class SimpleEurekaClientConfig extends EurekaClientConfigBean {

    private String eurekaUrl;
    private String zone;
    private String region = "us-east-1";

    public SimpleEurekaClientConfig(String eurekaUrl, String zone, String region) {
        this.eurekaUrl = eurekaUrl;
        this.zone = zone;
        this.region = region;
    }

    public SimpleEurekaClientConfig(String eurekaUrl, String zone) {
        this.eurekaUrl = eurekaUrl;
        this.zone = zone;
    }


    @Override
    public String getRegion() {
        return region;
    }

    @Override
    public String[] getAvailabilityZones(String s) {
        return new String[] {zone};
    }

    @Override
    public List<String> getEurekaServerServiceUrls(String s) {
        return Arrays.asList(eurekaUrl);
    }

    @Override
    public boolean shouldEnforceRegistrationAtInit() {
        return true;
    }

    @Override
    public boolean shouldRegisterWithEureka() {
        return true;
    }
}

这篇关于Spring Cloud Eureka 服务器不相互复制,显示警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆