高可用eureka服务注册与发现代码例子

代码

Eureka server 1

pom.xml

XML 复制代码
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

application.yml

XML 复制代码
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

java 复制代码
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer1Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer1Application.class, args);
    }
}

Eureka server 2

pom.xml

XML 复制代码
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

application.yml

java 复制代码
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

java 复制代码
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer2Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer2Application.class, args);
    }
}

Eureka client

pom.xml

XML 复制代码
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

application.yml

XML 复制代码
spring:
  application:
    name: eureka-client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
复制代码
EurekaClientApplication
XML 复制代码
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

依次运行Eureka server1 , Eureka server2, EurekaClient

你会发现两个server上都有了EurekaClient的注册信息。

Eureka server1

Eureka server2

相关问题与总结

  1. Eureka server 不能用localhost,否则不会复制注册信息到peer Eureka上。

相关代码PeerAwareInstanceRegistryImpl,ApplicationResource.addInstance(), PeerEurekaNodes.resolvePeerUrls()

相关推荐
小小寂寞的城1 小时前
JAVA观察者模式demo【设计模式系列】
java·观察者模式·设计模式
探索java1 小时前
Java并发编程中的StampedLock详解:原理、实践与性能优化
java·stampedlock
界面开发小八哥2 小时前
「Java EE开发指南」如何用MyEclipse将Java项目转换为Web项目?
java·ide·java-ee·eclipse·开发工具·myeclipse
pobu1682 小时前
aksk前端签名实现
java·前端·javascript
一个天蝎座 白勺 程序猿2 小时前
飞算JavaAI进阶:重塑Java开发范式的AI革命
java·开发语言·人工智能
前端 贾公子2 小时前
tailwindCSS === 使用插件自动类名排序
java·开发语言
没有bug.的程序员2 小时前
JAVA面试宝典 -《Spring Boot 自动配置魔法解密》
java·spring boot·面试
hnlucky3 小时前
《Nginx + 双Tomcat实战:域名解析、静态服务与反向代理、负载均衡全指南》
java·linux·服务器·前端·nginx·tomcat·web
hnlucky3 小时前
同时部署两个不同版本的tomcat要如何配置环境变量
java·服务器·http·tomcat·web