Spring cloud Eureka server is NOT replicating each other, displaying warning
Asked Answered
S

2

0

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.

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/

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.

Any idea why?

Spaceman answered 31/8, 2015 at 5:36 Comment(2)
The warning has to do with the number of instances of services registered with eureka, not with eureka replicating.Abiogenesis
see this SO question to see howto setup peer replication: #30289459Abiogenesis
S
3

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:

Application.yml

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

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());
    }
}

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;
    }
}
Sutter answered 17/3, 2020 at 0:46 Comment(9)
May I ask you what is SimpleEurekaClientConfig? Did you create your implementation of the config interface or is it located in another dependency?Punctate
@Punctate Yes, it's my implementation. I updated the reply to include the code of SimpleEurekaClientConfig. ThanksSutter
@mostafacs Thanks for the example, its works, but when shutting down the client it only does with defaultZone, not with additionalZones, it still showing UP instance.Wayfarer
@Yougesh Can y create a demo project with the same issue ? then I can help. Thanks.Sutter
@mostafacs I have created example in the repo, and added README.md to run the projects. github.com/ypkkhatri/sb-eureka-multi-zone-exampleWayfarer
@Yougesh I have tried to make it removed from registered additional eureka servers. But I can't. I can only change status to Down. when deleted. I will update the answer.Sutter
@Yougesh, I will do another try later today.Sutter
@mostafacs Thanks, I will waitWayfarer
@mostafacs Did you updated the code above?Wayfarer
D
0

It's not a XML you need rename it to "application.YML"

https://en.wikipedia.org/wiki/YAML

Dippold answered 4/9, 2015 at 11:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.