示例代码码云地址
https://gitee.com/zhang-xin-1/springcloud1
一.创建Eureka项目module
创建eureka项目
创建spring Lnitializr项目
直接搜索导入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 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.3.2.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.SR12</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>
</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
spring:
application:
name: eureka-server
# 8001 8101 8201
# 2001 3001 4001 5001 6001
server:
port: 2001
eureka:
server:
# 禁用自我保护模式
enable-self-preservation: false
instance:
# 主机名
hostname: eureka1
client:
# 针对单台服务器,不向自己注册,不从自己拉取
register-with-eureka: false
fetch-registry: false
eureka配置文件详解
eureka 集群服务器之间,通过 hostname 来区分
eureka.server.enable-self-preservation
eureka 的自我保护状态:心跳失败的比例,在15分钟内是否超过85%,如果出现了超过的情况,Eureka Server会将当前的实例注册信息保护起来,同时提示一个警告,一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据。也就是不会注销任何微服务
eureka.client.register-with-eureka=false
不向自身注册
eureka.client.fetch-registry=false
不从自身拉取注册信息
eureka.instance.lease-expiration-duration-in-seconds
最后一次心跳后,间隔多久认定微服务不可用,默认90
主程序添加注解
添加 @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);
}
}
修改 hosts 文件,添加 eureka 域名映射
hosts文件地址:
C:\Windows\System32\drivers\etc\hosts
127.0.0.1 eureka1
127.0.0.1 eureka2
启动,并访问测试
二.service provider 服务提供者(服务注册eureka客户端)
修改 item-service、user-service、order-service,把微服务注册到 eureka 服务器
pom.xml 添加eureka依赖(添加)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
application.yml 添加 eureka注册配置(添加)
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka
eureka.instance.lease-renewal-interval-in-seconds
心跳间隔时间,默认 30 秒defaultZone,默认位置,可以修改为具体地理位置,比如:beiJing, shangHai, shenZhen 等,表示 eureka 服务器的部署位置, 需要云服务器提供
eureka.client.registry-fetch-interval-seconds
拉取注册信息间隔时间,默认 30 秒
主程序启用服务注册发现客户端(@EnableDiscoveryClient)
修改 item-service、user-service 和 order-service,
主程序添加 @EnableDiscoveryClient
注解
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);
}
}
启动,并访问 eureka 查看注册信息
三.Eureka 和 “服务提供者”的高可用
item-service 高可用
启动参数
--server.port
可以覆盖yml中的端口配置
配置启动参数
--server.port=8001
启动另一个端口服务
拷贝springboot项目
--server.port=8002
启动测试
访问 eureka 查看 item-service 注册信息
访问两个端口测试
http://localhost:8001/35
http://localhost:8002/35
eureka 高可用(profile配置文件知识点)
添加两个服务器的 profile 配置文件
application-eureka1.yml(profile配置文件命名格式application-xxxxx.yml)
# application-eureka1.yml
eureka:
instance:
# 主机名
hostname: eureka1
client:
# 集群服务器之间互相注册、互相拉取
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka2:2002/eureka
application-eureka2.yml
# application-eureka2.yml
eureka:
instance:
# 主机名
hostname: eureka2
client:
# 集群服务器之间互相注册、互相拉取
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka1:2001/eureka
配置启动参数
--spring.profiles.active
和--server.port
eureka1 启动参数:
--spring.profiles.active=eureka1 --server.port=2001
eureka2 启动参数:
如果在命令行运行,可以在命令行中添加参数:
java -jar xxx.jar --spring.profiles.active=eureka1 --server.port=2001
访问 eureka 服务器,查看注册信息
eureka客户端注册时,向两个服务器注册
修改以下微服务
sp02-itemservice
sp03-userservice
sp04-orderservice
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
当一个 eureka 服务宕机时,仍可以连接另一个 eureka 服务