0

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

续订低于门槛。自我保护模式已关闭。这可能无法在网络/其他问题的情况下保护实例到期。

尤里卡 application.xml 是这样的:

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,我会看到没有注册微服务。

知道为什么吗?

4

2 回答 2

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;
    }
}
于 2020-03-17T00:46:59.480 回答
0

它不是 XML,您需要将其重命名为“application.YML”

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

于 2015-09-04T11:30:09.737 回答