1、SpringCloud Eureka介绍
1.1 Eureka服务治理
- 服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现。
Spring Cloud Eureka是Spring Cloud Netflix微服务套件中的一部分, 它基于Netflix Eureka做了二次封装。 主要负责完成微服务架构中的服务治理功能。
1.2 Eureka集群架构原理
引用Netflix官网的原理图说明一下,Eureka架构原理:
如上图,是Eureka集群配置图。
- 不同节点的Eureka Server通过Replicate(复制)进行数据同步
- Application Service为服务提供者
- Application Client为服务消费者
- Make Remote Call完成一次服务调用
- 服务启动后向Eureka注册,Eureka Server会将注册信息向其他Eureka
Server进行同步,当服务消费者要调用服务提供者,则向服务注册中心获取服务提供者地址(即:服务应用名,spring.application.name参数配置),然后会将服务提供者地址缓存在本地,下次再调用时,则直接从本地缓存中取,完成一次调用。 - 当服务注册中心EurekaServer检测到服务提供者因为宕机、网络原因不可用时,则在服务注册中心将服务置为DOWN状态,并把当前服务提供者状态向订阅者发布,订阅过的服务消费者更新本地缓存。
- 服务提供者在启动后,周期性(默认30秒)向Eureka Server发送心跳,以证明当前服务是可用状态。EurekaServer在一定的时间(默认90秒)未收到客户端的心跳,则认为服务宕机,注销该实例。
1.3 Eureka自我保护机制
- 在默认配置中,EurekaServer在默认90s没有得到客户端的心跳,则注销该实例,但是往往因为微服务跨进程调用,网络通信往往会面临着各种问题,比如微服务状态正常,但是因为网络分区故障时,EurekaServer注销服务实例则会让大部分微服务不可用,这很危险,因为服务明明没有问题。
- 为了解决这个问题,Eureka 有自我保护机制,通过在Eureka Server配置如下参数,可启动保护机制。
eureka.server.enable-self-preservation=true
它的原理是,当Eureka Server节点在短时间内丢失过多的客户端时(比如网络故障),那么这个节点将进入自我保护模式,不再注销任何微服务,当网络故障回复后,该节点会自动退出自我保护模式。
1.4 Eureka高可用集群
- 理论上来讲,因为服务消费者本地缓存了服务提供者的地址,即使EurekaServer宕机,也不会影响服务之间的调用,但是一旦新服务上线,已经在缓存在本地的服务提供者不可用了,服务消费者也无法知道,所以保证EurekaServer的高可用还是很有必要的。
- 在分布式系统中,任何的地方存在单点,整个体系就不是高可用的,Eureka 也一样,在上面的架构图中EurekaServer不是以单点存在的,而是以集群的方式对外提供服务。
2、Eureka双中心节点集群
本节以具体示例说明Eureka集群实现及相关配置。
2.1 创建Eureka Server
(1)新建microservice-eureka项目作为Eureka Server集群项目,如下所示:
pom.xml如下:
4.0.0
<groupId>com.xxxxx.microservice</groupId>
<artifactId>microservice-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>microservice-item</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<!-- 导入Spring Cloud的依赖管理 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--springboot 整合eureka服务端 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories><!-- 代码库 -->
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<layout>default</layout>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
(2)配置application.yml。
在EurekaServer集群时,需要部署多个节点,则需要同一个项目,使用不同的配置。因此,在本例中使用microservice-eureka一个项目通过不同的启动参数来启动占用不同端口的两个Server服务,来模拟Eureka集群;即通过配置文件application.yml配置不同的端口即可:
###服务端口号
server:
port: 8100 ##通过配置不同端口号打包jar即可
###服务名称
spring:
application:
name: app-eureka-center
security:
user:
name: ken
password: 123456
eureka:
instance:
#注册中心地址
hostname: 127.0.0.1
###客户端调用地址、单机及集群配置,单机直接配置自己的端口,集群配置对方端口
###搭建Eureka集群非常简单,只要启动多个Eureka Server服务并且让这些Server端之间彼此进行注册即可实现
client:
serviceUrl:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:9100/eureka/
###是否将自己注册到Eureka服务中,因为该应用本身就是注册中心,不需要再注册自己(集群的时候为true)
register-with-eureka: true
###是否从Eureka中获取注册信息,因为自己为注册中心,不会在该应用中的检索服务信息
fetch-registry: true
server:
enable-self-preservation: false #禁用自我保护模式
(3)新建启动类。
创建如下的启动类:
重点添加@EnableEurekaServer注解,开启对EurekaServer的支持,即:作为Eureka服务端。
启动服务后,修改application.yml配置文件中的端口号,改为
###服务端口号
server:
port: 9100 ##通过配置不同端口号打包jar即可
再次启动服务
访问服务注册中心:http://localhost:8100/,将出现如下界面:
访问服务注册中心:http://localhost:9100/,将出现如下界面:
到此双节点的集群就搭建完成了。
总结,Eureka集群还是比较简单的,主要是配置文件application.yml改变而已;配置文件中增加security配置项是为了控制访问注册中心。