1.说明

本文详细介绍Spring Cloud创建Eureka模块集群的方法,
基于已经创建好的Spring Cloud Eureka Server模块,
请参考SpringCloud创建Eureka模块,
通过配置文件启动多个Eureka Server模块,
演示Eureka Server集群的创建和使用方法,
包括2个Eureka Server服务作为集群,
以及1个Eureka Client服务作为客户端。

2.模块打包

使用如下Maven命令,打包Eureka Server模块:

mvn clean install -DskipTests

打包成功后,在模块target目录下,
可以找到jar包:
eureka-server-0.0.1-SNAPSHOT.jar
另外一个jar包没有用,不需要管:
eureka-server-0.0.1-SNAPSHOT.jar.original

3.配置文件

Eureka Server 1对应eureka-server-dev-1.yml:

server:
port: 7001
eureka:
instance:
hostname: eureka1
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka1:7001/eureka,http://eureka2:7002/eureka
server:
enable-self-preservation: false

Eureka Server 2对应eureka-server-dev-2.yml:

server:
port: 7002
eureka:
instance:
hostname: eureka2
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka1:7001/eureka,http://eureka2:7002/eureka
server:
enable-self-preservation: false

注意上面配置的eureka1,eureka2主机名,
不能和原来单机的时候相同,使用localhost了,
否则访问Eureka控制台页面的时候,
会无法看到集群中的其他Eureka Server。
另外eureka.client.service-url.defaultZone这个属性,
可以配置Eureka集群中所有可用的实例,
Eureka会自动排除掉与自身重复的实例:

http://eureka1:7001/eureka,http://eureka2:7002/eureka

4.配置主机名

在Windows一台主机上启动这两个服务,
需要修改C:\Windows\System32\drivers\etc\hosts,
增加对应的主机名:

127.0.0.1 eureka1
127.0.0.1 eureka2

这里为了演示,
在同一个主机上部署了两个Eureka Server,
eureka1和eureka2都对应本机的127.0.0.1,
所以他们的端口不能相同为7001、7002,
如果部署在不同的主机上面,
可以配置成同一个端口,
这样只需要主机名不同就行了。

5.启动Eureka集群

启动Eureka Server 1:

java -jar -Dspring.config.location=eureka-server-dev-1.yml eureka-server-0.0.1-SNAPSHOT.jar

启动Eureka Server 2:

java -jar -Dspring.config.location=eureka-server-dev-2.yml eureka-server-0.0.1-SNAPSHOT.jar

Eureka Server 1的启动日志,
可以看到启动时,找到了集群的备用节点eureka2,
同样Eureka Server 2的启动日志也是类似的:

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE) 2020-09-22 17:51:43.902 INFO 16748 --- [ main] c.y.s.eureka.EurekaServerApplication : No active profile set, falling back to default profiles: default
2020-09-22 17:51:45.940 WARN 16748 --- [ main] o.s.boot.actuate.endpoint.EndpointId : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2020-09-22 17:51:46.124 INFO 16748 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=c94dfcb8-a92b-3043-9fe7-9ab9d1567d69
2020-09-22 17:51:47.247 INFO 16748 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 7001 (http)
2020-09-22 17:51:47.262 INFO 16748 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-09-22 17:51:47.262 INFO 16748 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-09-22 17:51:47.407 INFO 16748 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-09-22 17:51:47.408 INFO 16748 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3470 ms
2020-09-22 17:51:47.506 WARN 16748 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2020-09-22 17:51:47.506 INFO 16748 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-09-22 17:51:47.531 INFO 16748 --- [ main] c.netflix.config.DynamicPropertyFactory : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@5c80cf32
2020-09-22 17:51:48.152 INFO 16748 --- [ main] c.s.j.s.i.a.WebApplicationImpl : Initiating Jersey application, version 'Jersey: 1.19.1 03/11/2016 02:08 PM'
2020-09-22 17:51:48.295 INFO 16748 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2020-09-22 17:51:48.297 INFO 16748 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2020-09-22 17:51:48.561 INFO 16748 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2020-09-22 17:51:48.561 INFO 16748 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2020-09-22 17:51:49.245 WARN 16748 --- [ main] o.s.c.n.a.ArchaiusAutoConfiguration : No spring.application.name found, defaulting to 'application'
2020-09-22 17:51:49.246 WARN 16748 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2020-09-22 17:51:49.246 INFO 16748 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-09-22 17:51:49.699 INFO 16748 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-22 17:51:51.647 WARN 16748 --- [ main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2020-09-22 17:51:51.694 INFO 16748 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2020-09-22 17:51:51.730 INFO 16748 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2020-09-22 17:51:51.730 INFO 16748 --- [ main] com.netflix.discovery.DiscoveryClient : Client configured to neither register nor query for data.
2020-09-22 17:51:51.740 INFO 16748 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1600768311739 with initial instances count: 0
2020-09-22 17:51:51.791 INFO 16748 --- [ main] c.n.eureka.DefaultEurekaServerContext : Initializing ...
2020-09-22 17:51:51.793 INFO 16748 --- [ main] c.n.eureka.cluster.PeerEurekaNodes : Adding new peer nodes [http://eureka2:7002/eureka/]
2020-09-22 17:51:52.373 INFO 16748 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2020-09-22 17:51:52.373 INFO 16748 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2020-09-22 17:51:52.373 INFO 16748 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2020-09-22 17:51:52.373 INFO 16748 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2020-09-22 17:51:52.477 INFO 16748 --- [ main] c.n.eureka.cluster.PeerEurekaNodes : Replica node URL: http://eureka2:7002/eureka/
2020-09-22 17:51:52.484 INFO 16748 --- [ main] c.n.e.registry.AbstractInstanceRegistry : Finished initializing remote region registries. All known remote regions: []
2020-09-22 17:51:52.484 INFO 16748 --- [ main] c.n.eureka.DefaultEurekaServerContext : Initialized
2020-09-22 17:51:52.493 INFO 16748 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-09-22 17:51:52.514 INFO 16748 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application UNKNOWN with eureka with status UP
2020-09-22 17:51:52.516 INFO 16748 --- [ Thread-383] o.s.c.n.e.server.EurekaServerBootstrap : Setting the eureka configuration..
2020-09-22 17:51:52.516 INFO 16748 --- [ Thread-383] o.s.c.n.e.server.EurekaServerBootstrap : Eureka data center value eureka.datacenter is not set, defaulting to default
2020-09-22 17:51:52.517 INFO 16748 --- [ Thread-383] o.s.c.n.e.server.EurekaServerBootstrap : Eureka environment value eureka.environment is not set, defaulting to test
2020-09-22 17:51:52.538 INFO 16748 --- [ Thread-383] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false
2020-09-22 17:51:52.539 INFO 16748 --- [ Thread-383] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2020-09-22 17:51:52.539 INFO 16748 --- [ Thread-383] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2020-09-22 17:51:52.539 INFO 16748 --- [ Thread-383] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2020-09-22 17:51:52.539 INFO 16748 --- [ Thread-383] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2020-09-22 17:51:52.549 INFO 16748 --- [ Thread-383] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2020-09-22 17:51:52.576 INFO 16748 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 7001 (http) with context path ''
2020-09-22 17:51:52.576 INFO 16748 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 7001
2020-09-22 17:51:53.160 INFO 16748 --- [ main] c.y.s.eureka.EurekaServerApplication : Started EurekaServerApplication in 11.497 seconds (JVM running for 12.053)

6.查看Eureka服务

访问http://eureka1:7001/

访问http://eureka2:7002/

可以从上面看出集群信息,
Eureka Server 1对应的DS Replicas是eureka2;
Eureka Server 2对应的DS Replicas是eureka1。

7.配置Eureka Client

Eureka Client访问Eureka Sever集群配置:

eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka1:7001/eureka,http://eureka2:7002/eureka

只需要指定eureka.client.service-url.defaultZone这个属性,
为所有Eureka Server集群信息即可,
区别在于单机的时候配置一个Eureka Server即可。

8.参考文章

Eureka集群搭建
SpringCloud服务注册中心双节点集群(Eureka集群)

SpringCloud创建Eureka模块集群的更多相关文章

  1. SpringCloud创建Eureka模块

    1.说明 本文详细介绍Spring Cloud创建Eureka模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 在里面创建Eureka模块, ...

  2. springcloud(四)-Eureka Server集群

    Eureka Server的高可用 这一节我们接着上一节说. 有分布式应用开发经验的朋友应该发现,前文编写的单节点Eureka Server并不适合线上生产环境.Eureka Client会定时连接E ...

  3. SpringCloud之Eureka:集群搭建

    上篇文章<SpringCloud之Eureka:服务发布与调用例子>实现了一个简单例子,这次对其进行改造,运行两个服务器实例.两个服务提供者实例,服务调用者请求服务,使其可以进行集群部署. ...

  4. SpringCloud创建Config模块

    1.说明 本文详细介绍Spring Cloud创建Config模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 创建Config模块这个子工 ...

  5. SpringCloud创建Gateway模块

    1.说明 本文详细介绍Spring Cloud创建Gateway模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 和已经创建好的Eureka ...

  6. SpringCloud的入门学习之Netflix-eureka(Eureka的集群版搭建)

    1.Eureka单机版的话,可能会出现单点故障,所以要保障Eureka的高可用,那么可以进行搭建Eureka的集群版. 高可用的Eureka的注册中心,将注册中心服务部署到多台物理节点上,形成一个集群 ...

  7. SpringCloud升级之路2020.0.x版-20. 启动一个 Eureka Server 集群

    本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford 我们的业务集群结构 ...

  8. SpringCloud系列四:实现Eureka Server的高可用并将应用注册到Eureka Sever集群上

    1. 回顾 上一篇博客中,实现了单节点的Eureka Server.Eureka Client会定时连接Eureka Server,获取注册表中的信息并缓存到本地.微服务在消费远程API时总是使用本地 ...

  9. Spring Cloud Eureka的集群配置(六)

    1.再次创建2个Eureka工程 工程名:microservicecloud-eureka-7002 工程名:microservicecloud-eureka-7003 2.pom.xml文件 < ...

随机推荐

  1. c学习 - 第七章:数组

    7.3.6 字符串处理函数 (1).puts(字符数组) 字符串输出到终端 (2).gets(字符数组) 从标准输入获取字符串(包括空格) (3).strcat(字符数组1,字符数组2) 连接两个字符 ...

  2. 转Android Canvas和Paint基本使用

    Android Canvas和Paint基本使用   这篇文章主要介绍下画笔Paint和画布Canvas的基本使用  1.Paint 创建对象Paint mPaint = new Paint(); 常 ...

  3. java面试--(生成随机数,获取重复次数最多,并且数是最大的一个,打印出来)

    import java.util.*; public class MaxRandom { public static void main(String[] args){ int[] num = new ...

  4. C++ friend详解

    私有成员只能在类的成员函数内部访问,如果想在别处访问对象的私有成员,只能通过类提供的接口(成员函数)间接地进行.这固然能够带来数据隐藏的好处,利于将来程序的扩充,但也会增加程序书写的麻烦. C++ 是 ...

  5. 图书管理系统总结——JAVA Swing控件简介

    断断续续学习JAVA语言,写了一个多月数据库大作业,终于在五一过后写完了.由于第一次使用JAVA和数据库,遇到了许多问题,记录下来,以备以后查看. 我使用的JAVA SE,说实话,在开发后期,觉得JA ...

  6. 3、回溯算法解题套路框架——Go语言版

    前情提示:Go语言学习者.本文参考https://labuladong.gitee.io/algo,代码自己参考抒写,若有不妥之处,感谢指正 关于golang算法文章,为了便于下载和整理,都已开源放在 ...

  7. 一文详解TDSQL PG版Oracle兼容性实践

    TDSQL PG版分布式关系型数据库,是一款同时面向在线事务交易和MPP实时数据分析的高性能HTAP数据库系统.面对应用业务产生的不定性数据爆炸需求,不管是高并发交易还是海量实时数据分析,TDSQL ...

  8. 淘宝自动抢购, Webdriver浏览器常用的元素定位

    https://www.cnblogs.com/diaosicai/p/5909660.html #!/usr/bin/env python ''' 作者:张铭达 功能:淘宝秒杀购物 版本:0.2 日 ...

  9. windows串口之虚拟串口和Access port

    关于 本文将介绍自己常用的串口工具,演示环境为Windows. 1. 虚拟串口 1.1 没有那么多的物理串口, 用虚拟串口来凑.需要软件Configure Virtual Serial Port Dr ...

  10. c(++)可变参数之格式化字符串

    0.序言 使用printf函数,其参数就是可变参数.下面将使用 C语言  的库函数实现可变参数的函数 . 用途(欢迎补充): A.记录日志,可能需要将变量格式化输出到日志文件. B.格式化字符串,显示 ...