高可用指设计系统减少服务不能工作的时间,在分布式系统里面是必须考虑设计的。前面搭建的是一台eureka注册中心,要保证eureka注册中心高可用,最少需要两台eureka注册中心,且这两台的数据需要保持一致,才能保障当其中一台出现故障时,整个系统可以正常运行。话不多说,下面上代码
1.新建eureka-register-high-available模块,pom文件和eureka-register模块一样,只需要改动artifactId
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.feng</groupId>
<artifactId>cloud-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>eureka-register-high-available</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-register-high-available</name>
<description>cloud study for two month</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.yml需要做修改,新建application-peer1.yml,application-peer2.yml,bootstrap.yml三个配置文件,application-peer1.yml是eureka注册中心节点1的配置,application-peer2.yml是eureka注册中心节点2的配置,bootstrap.yml是系统运行的公共参数配置
application-peer1.yml配置如下:
server:
port: 8701
eureka:
instance:
instanceId: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
hostname: peer1
client:
registerWithEureka: true #注意多注册中心,向eureka注册自己
fetchRegistry: true #注意多注册中心,拉取eureka的注册信息
serviceUrl:
defaultZone: http://peer2:8702/register/eureka/
application-peer2.yml配置如下:
server:
port: 8702
eureka:
instance:
instanceId: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
hostname: peer2
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://peer1:8701/register/eureka/
bootstrap.yml配置如下:
spring:
application:
name: eureka-register
profiles:
active: ${profile:peer1}
server:
servlet:
context-path: /register
注意:application-peer1.yml,application-peer2.yml的serviceUrl配置的均是对方的注册中心地址
3.启动eureka-register-high-available模块
启动方式有两种:idea启动和命令行启动。笔者使用的idea开发工具,其他开发工具启动可以参考idea启动配置
启动之前需要做一件事:配置ip和主机名。笔者是mac电脑,编辑hosts文件,添加
127.0.0.1 peer1
127.0.0.1 peer2
截图如下:
第一种idea启动:
(1):点击Edid Configurations,编辑启动脚本
(2)找到eureka-register-high-available模块的Main class,重命名Name(因为要启动两个eureka-register-high-available模块)。设置Active profiles为 peer1或peer2。 笔者是EurekaHighAvailableRegisterApplication2对应peer2,EurekaHighAvailableRegisterApplication1对应peer1 ,这样方便,不至于产生启动错误。
到这idea启动配置工作就完成了
第二种命令行启动:
(1)使用idea打开maven Projects,选中eureka-register-high-available,点击install,即可将该模块打成jar包。只要能打成jar包,任何方式都可以
(2)找到jar包地址,先后输入命令 java -jar eureka-register-high-available-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
,java -jar eureka-register-high-available-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 即可启动项目
到此位置,命令行启动已经完成。
4.访问http://localhost:8701/register/,http://localhost:8702/register/ 都可以看到注册中心的信息。可以看到DS Replicas:peer2,就代表高可用注册中心配置成功了
到此为止,eureka高可用注册中心eureka-register-high-available已经搭建完成了,代码git地址:https://github.com/fengyantao/cloud-study/tree/master
大家如果有疑问,欢迎下方留言沟通!