Eureka 注册中心
作用:服务注册和发现
-
提供者(Provider)
向注册中心注册自己的地址 -
消费者(Consumer)
从注册中心发现其他服务
eureka的运行机制
- 注册 - 一次次反复连接eureka,直到注册成功为止
- 拉取 - 每隔30秒拉取一次注册表,更新注册信息
- 心跳 - 每30秒发送一次心跳,3次收不到心跳eureka会删除这个服务
- 自我保护模式 - 特殊情况,由于网络不稳定15分钟内85%服务器出现心跳异常
- 保护所有的注册信息不删除
- 网络恢复后,可以自动退出保护模式
- 开发测试期间,可以关闭保护模式
一 :搭建eureka server 项目
流程
- 创建eureka项目
- 配置依赖 pom.xml
- 配置 application.yml
- 主程序启用 eureka 服务器 添加 @EnableEurekaServer
- 启动,访问测试
1.创建 eureka server 项目
sp05-eureka
2. 配置依赖 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 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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp05-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp05-eureka</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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
3. 配置 application.yml
spring:
application:
name: eureka-server //主机名(集群中区分每台服务器)
server:
port: 2001
eureka:
server:
enable-self-preservation: false //关闭保护模式
instance:
hostname: eureka1
client:
register-with-eureka: false //不向自己注册
fetch-registry: false //不从自己拉取
yml配置
- 主机名(集群中区分每台服务器)
- 关闭保护模式
- 针对单台服务器,不向自己注册,不从自己拉取
eureka 的自我保护状态:
心跳失败的比例,在15分钟内是否超过85%,如果出现了超过的情况,Eureka Server会将当前的实例注册信息保护起来,同时提示一个警告,一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据。也就是不会注销任何微服务
4.主程序启用 eureka 服务器
- 添加 @EnableEurekaServer
package cn.tedu.sp05;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Sp05EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(Sp05EurekaApplication.class, args);
}
}
5. 启动,访问测试
hosts修改
127.0.0.1 eureka1
127.0.0.1 eureka2
启动,并访问测试
- http://eureka1:2001
二 :service provider 服务提供者
修改 item-service、user-service、order-service,把微服务注册到 eureka 服务器
流程
- pom.xml 添加eureka依赖
- application.yml 添加eureka注册配置
- 主程序启用eureka客户端
- 启动服务,在eureka中查看注册信息
1. pom.xml 添加eureka依赖
右键点击项目,或点击pom.xml,用 STS 工具编辑起步依赖
安装插件EditStarters
上面的操作会在pom.xml中添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. application.yml 添加eureka注册配置
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka
-
eureka.instance.lease-renewal-interval-in-seconds
心跳间隔时间,默认 30 秒 -
eureka:client:service-url: defaultZone:http:// xxxxxx
默认位置,可以修改为具体地理位置,比如:beiJing, shangHai, shenZhen 等,表示 eureka 服务器的部署位置, 需要云服务器提供 -
eureka.client.registry-fetch-interval-seconds
拉取注册信息间隔时间,默认 30 秒
3. 主程序启用eureka客户端
修改 item-service、user-service 和 order-service,主程序添加 @EnableDiscoveryClient 注解 (可选)
新的Springboot项目可以不添加 @EnableDiscoveryClient 注解
4. 启动服务,在eureka中查看注册信息
- http://eureka1:2001
三:eureka 和 “服务提供者”的高可用
item-service 高可用
启动参数 --server.port 可以覆盖yml中的端口配置
配置启动参数
- item-service-8001
- item-service-8002
启动测试
访问 eureka 查看 item-service 注册信息
eureka 高可用
添加两个服务器的 profile 配置文件
profile的配置会覆盖公用配置
application-eureka1.yml
eureka:
instance:
hostname: eureka1
client:
register-with-eureka: true #profile的配置会覆盖公用配置
fetch-registry: true #profile的配置会覆盖公用配置
service-url:
defaultZone: http://eureka2:2002/eureka #eureka1启动时向eureka2注册
application-eureka2.yml
eureka:
instance:
hostname: eureka2
client:
register-with-eureka: true #profile的配置会覆盖公用配置
fetch-registry: true #profile的配置会覆盖公用配置
service-url:
defaultZone: http://eureka1:2001/eureka #eureka2启动时向eureka1注册
配置启动参数 --spring.profiles.active 和 --server.port
- eureka1 启动参数:
--spring.profiles.active=eureka1 --server.port=2001
- eureka2 启动参数:
--spring.profiles.active=eureka2 --server.port=2002
访问 eureka 服务器,查看注册信息
- http://eureka1:2001/
- http://eureka2:2002/
eureka客户端注册时,向两个服务器注册
修改以下微服务
- sp02-itemservice
- sp03-userservice
- sp04-orderservice
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka