1、Eureka Server HA高可用
Eureka Client定时连接Eureka Server,获取服务注册表中的信息并缓存到本地。
微服务Client端在消费远程API的时候不用每次去服务注册Server端查询,而是使用本地缓存的数据。
也就是说,即使Server宕机,也不会影响微服务之间的调用,但是肯定会影响Client端服务的更新。
所以生产环境中,服务注册中心(Eureka Server)的高可用HA是必不可少的。
参考:
https://blog.csdn.net/yangshangwei/article/details/85727427
https://github.com/yangshangwei/SpringCloudMaster/tree/master/microservice-discovery-eureka-ha3
2、StandAlone模式
参考官方文档
https://cloud.spring.io/spring-cloud-static/Greenwich.SR1/single/spring-cloud.html#spring-cloud-eureka-server-standalone-mode
比如,https://blog.csdn.net/chengyuqiang/article/details/90645498 配置的StandAlone模式
spring.application.name=register-server
server.port=8800
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/
说明:
- eureka.client.register-with-eureka:表示十分将自己注册到Eureka Server,默认为true
- eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false。
- eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址之间是有逗号分隔;单个节点的默认地址是http://localhost:8761/eureka。这里端口号被修改为了8800
3、项目信息
新建Maven项目eureka,其中pom.xml如下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cntaiping.tpa</groupId>
<artifactId>eureka</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>eureka-ha</module>
<module>service-hello</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4、新建eureka-ha模块
在eureka项目中,新建eureka-ha模块。
4.1 application.yml
application.yml中使用连字符 — 将配置文件分为三段,
- 第一段未指定spring. profiles,即对所有的profile生效
- 第二段和第三段分别为spring. profiles指定了名称,该名称表示它所在的那段内容应该在哪个profile中。
spring:
# 注册到eureka上的微服务名称
application:
name: eureka-ha
eureka:
client:
serviceUrl:
defaultZone: http://peer1:7801/eureka/,http://peer2:7802/eureka/,http://peer3:7803/eureka/
---
spring:
# 指定profiles为peer1,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
profiles: peer1
server:
port: 7801
eureka:
instance:
# 当profiles为peer1,hostname是peer1 ,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
hostname: peer1
---
spring:
profiles: peer2
server:
port: 7802
eureka:
instance:
hostname: peer2
---
spring:
profiles: peer3
server:
port: 7803
eureka:
instance:
hostname: peer3
4.2 EurekaHaApplication类
package com.cntaiping.tpa.eurekaha;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaHaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaHaApplication.class, args);
}
}
4.3 编辑hosts
编辑C:\Windows\System32\drivers\etc\hosts,添加如下内容
127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3
4.4 运行
两种运行方式:IDE测试运行和jar包运行
(1)IDE测试运行
(2)jar包运行
编译成jar包,通过下面命令运行。
java -jar eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3
4.5 查看服务注册中心
http://peer1:7801/
http://peer1:7802/
http://peer1:7803/
5、服务提供者
新建service-hello模块,与之前的服务提供者模块类似,对应的application.properties如下
#服务名称
spring.application.name=service-hello
#端口号
server.port=7700
#这里注册到peer1上,因为两两相互注册,所以服务会同步到peer2和peer3上面。
eureka.client.serviceUrl.defaultZone=http://peer1:7801/eureka/
#启动服务发现的功能,开启了才能调用其它服务
spring.cloud.config.discovery.enabled=true
#发现的服务的名字--对应注测中心的服务名字
spring.cloud.config.discovery.serviceId=eureka-ha