一、Eureka服务治理机制
Eureka服务治理三大元素:服务提供者、服务消费者和注册中心;三者的关系如下图:
服务提供者
服务注册
服务提供者启动时会通过REST请求向注册中心注册服务,并附加一些自身的元数据信息,Eureka Server会将这些元数据信息存储在一个双层的map结构中(第一层key是服务名-application.name,第二层是实例名称-instance-id)
服务续约
服务提供者会维持一个心跳告诉注册中心,实例还活着,以免注册中心将其剔除
服务下线
服务的实例正常关闭或者重启,会通过REST请求告诉注册中心下线状态,注册中心就会把其实例剔除或状态改为DOWN,若服务非正常退出,则注册中心会等到服务失效再将其剔除掉。
服务消费者
服务获取
服务消费者通过REST请求向注册中心获取一份只读服务清单,默认30秒更新一次清单
服务调用
服务消费者通过获取的服务清单中,根据服务名获得具体要调用的实例名和元数据,元数据中就包含了调用的地址,通过Ribbon实现负载均衡的调用。
注册中心
失效剔除
服务非正常情况下退出,则不会通过REST请求告知注册中心,而注册中心在启动时就已经创建了一个定时器:定时将服务列表中超时的服务剔除掉。
服务同步
由于注册中心是集群,因此服务一旦注册到其中的一个注册中心,那么该注册中心就以广播的模式向其他注册中心注册该服务,实现服务同步
自我保护
默认情况下,在一定时间内注册中心没有收到服务的续约心跳,就会把该服务从服务列表中剔除掉,但可能由于网络的原因而导致服务和注册中心不能通讯,而服务是健康的,因此这种剔除操作就非常危险,所以注册中心在短时间内失去过多的实例,则进入自我保护模式,在这期间,不会删除服务列表中的服务,直到通讯恢复(收到的心跳阈值到达设定值)
二、Eureka Client端搭建
项目结构
pom
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>serviceclient01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>serviceclient01</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.33</version>
</dependency>
<!--定义公共接口-->
<dependency>
<groupId>com.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
application.yml
server:
servlet:
context-path: /serviceclient
port: 9000
spring:
application:
name: serviceclient #实例名称
eureka:
client:
#注册到注册中心,默认true
register-with-eureka: true
#是否偏好使用处于相同zone的服务提供者
prefer-same-zone-eureka: true
#可以理解为地区
region: guangzhou
#可用机房
availability-zones:
guangzhou: zone-1,zone-2
service-url:
#defaultZone: http://localhost:8080/eureka01/eureka/,http://localhost:8081/eureka02/eureka/
zone-1: http://localhost:8080/eureka01/eureka/
zone-2: http://localhost:8081/eureka02/eureka/
instance:
instance-id: serviceclient01 #status下面的超链接名称
#显示IP
prefer-ip-address: true
#定义服务任务续约的调用间隔时间
lease-renewal-interval-in-seconds: 5
#定义服务失效时间
lease-expiration-duration-in-seconds: 10
#实例信息的元数据
metadata-map:
#实例属于哪个机房
zone: zone-1
主启动类:Serviceclient01Application.java
package com.example.serviceclient01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
//开启熔断
@EnableCircuitBreaker
public class Serviceclient01Application {
public static void main(String[] args) {
SpringApplication.run(Serviceclient01Application.class, args);
}
}
三、Eureka配置详解
服务注册类配置,EurekaClientConfigBean,前缀:eureka.client
service-url:指定注册中心,map结构,默认defaultZone=http://localhost:8761/eureka,也可以配置安全校验
enabled:启用客户端,默认true
registryFetchIntervalSeconds:服务清单的缓存刷新时间间隔
instanceInfoReplicationIntervalSeconds:更新实例变化到注册中心的时间间隔
initialInstanceInfoReplicationIntervalSeconds:初始化实例信息到注册中心的时间间隔
eurekaServiceUrlPollIntervalSeconds:轮询服务端地址更改的时间间隔,集成config配置中心时,动态刷新eureka注册地址时需要关注的参数
eurekaServerReadTimeoutSeconds:读取eureka server信息的超时时间
eurekaServerConnectTimeoutSeconds:连接eureka server的超时时间
eurekaServerTotalConnections:eureka客户端到所有server的连接总数
eurekaServerTotalConnectionsPerHost:client到每个server主机的连接总数
eurekaConnectionIdleTimeoutSeconds:server连接的空闲关闭时间
heartbeatExecutorThreadPoolSize:心跳连接池的初始化线程数
heartbeatExecutorExponentialBackOffBound:心跳超时重试延迟时间的最大乘数值
cacheRefreshExecutorThreadPoolSize:缓存刷新线程池的初始化线程数
cacheRefreshExecutorExponentialBackOffBound:缓存刷新重试延迟时间的最大乘数值
registerWithEureka:是否注册到server,默认true
preferSameZoneEureka:是否偏好使用处于相同Zone的服务提供者,使用region和zone时才关注
filterOnlyUpInstances:获取实例时是否过滤,仅保留up状态的实例
fetchRegistry:检索服务
服务实例类配置,EurekaInstanceConfigBean,前缀:eureka.instance
使用random.int[start,end]随机指定启动端口,同时使用
{server.port}指定实例名
instanceId:实例名称,区分统一服务中不同实例
statusPageUrlPath:info的页面
healthCheckUrlPath:health页面
preferIpAddress:是否优先使用IP作为主机名的显示
leaseRenewalIntervalInSeconds:心跳间隔时间
leaseExpirationDurationInSeconds:服务剔除时间
nonSecurePort:非安全端口,就是http通道
securePort:安全端口,https通道
nonSecurePortEnable:是否开启
securePortEnable:是否开启
后续
后面会再出一篇讲讲源码