
pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
- dependency>
- dependencies>
application.yml
- server:
- port: 8761
- eureka:
- instance:
- hostname: 192.168.31.168
- # hostname: localhost
- client:
- register-with-eureka: false
- fetch-registry: true
- service-url:
- defaultZone: http://localhost:8762/eureka/
-
- spring:
- application:
- name: eureka-server1
EurekaServer1Application
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaServer1Application {
- public static void main(String[] args) {
- SpringApplication.run(EurekaServer1Application.class, args);
- }
- }
pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
- dependency>
- dependencies>
application.yml
- server:
- port: 8762
- eureka:
- instance:
- hostname: 192.168.31.168
- client:
- register-with-eureka: false
- fetch-registry: true
- service-url:
- defaultZone: http://localhost:8761/eureka/
-
-
- spring:
- application:
- name: eureka-server2
- cloud:
- compatibility-verifier:
- enabled: false
EurekaServer2Application
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaServer2Application {
- public static void main(String[] args) {
- SpringApplication.run(EurekaServer2Application.class, args);
- }
- }
pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- dependency>
- dependencies>
application.yml
- spring:
- application:
- name: eureka-client
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
EurekaClientApplication
- @SpringBootApplication
- @EnableDiscoveryClient
- public class EurekaClientApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaClientApplication.class, args);
- }
- }
你会发现两个server上都有了EurekaClient的注册信息。
Eureka server1

Eureka server2

相关问题与总结
1. Eureka server 不能用localhost,否则不会复制注册信息到peer Eureka上。
相关代码PeerAwareInstanceRegistryImpl,ApplicationResource.addInstance(), PeerEurekaNodes.resolvePeerUrls()