SpringCloud---Eureka服务注册与发现
Eureka简介
- Eureka是一种基于REST(具像状态传输)的服务,主要用于AWS云中定位服务,以实现中层服务器的负载平衡和故障转移。本文记录一个简单的服务注册与发现实例。
- GitHub地址:https://github.com/Netflix/eureka
- 官网文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.RC2/single/spring-cloud-netflix.html
Eureka-Server配置
- 服务注册中心
- 新建一个Maven项目,并删除src文件夹,保留pox.xml,作为parent,当然也可以不用
- 项目结构
- 父工程的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>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"> 4.0.0 cn.huanzi.qch parent 1.0.0 pom org.springframework.boot spring-boot-starter-parent 2.1.1.RELEASE
eureka-server
config-server
service-a
service-b1
service-b2
sso-server
service-c
txlcn-tm
zuul-server
UTF-8
1.8
UTF-8
UTF-8
UTF-8
5.1.34
5.22
1.4.7.RELEASE
3.4.3.1
3.1.12
2.0.3
2.3.2
2.2.1
2.4
2.1.1
2.3.2
Greenwich.RC1
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
- eureka-server的pom.xml,继承parent
<?xml version="1.0" encoding="UTF-8"?>xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 cn.huanzi.qch.eureka eureka-server 0.0.1-SNAPSHOT eureka-server eureka 注册中心 cn.huanzi.qch parent 1.0.0 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
- 配置文件
server.port=1111 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
- 启动应用,访问 http://localhost:1111/,注册中心启动成功,此时有0个服务
对Eureka-Client的配置
- 服务发现,可以新建一个springboot项目,maven中引入相关jar
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
- 配置文件加入注册中心的地址,也就是Eureka-Server的配置文件里面 eureka.client.serviceUrl.defaultZone
#eureka eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
- 启动类添加注解
@EnableEurekaClient @SpringBootApplication @RestController public class MyspringbootApplication{ public static void main(String[] args) { SpringApplication.run(MyspringbootApplication.class, args); } /** * 访问首页 */ @GetMapping("/index") public String index(){ return "hello springboot!"; } }
- 启动客户端服务
- 成功在注册中心注册成功,可以对外提供服务
健康检查
默认情况下,Eureka使用客户端心跳来确定客户端是否启动。除非另有说明,否则发现客户机不会根据Spring引导执行器传播应用程序的当前健康检查状态。因此,在成功注册后,Eureka总是宣布应用程序处于“UP”状态。可以通过启用Eureka健康检查来更改此行为,比如我现在将myspringboot服务停掉,但注册中心依旧显示为UP,这样就会造成我服务已经挂掉了,但注册中心依然会认为这个实例还活着。
Eureka-Client
#健康检查(需要spring-boot-starter-actuator依赖) eureka.client.healthcheck.enabled=true # 续约更新时间间隔(单位秒,默认30秒) eureka.instance.lease-renewal-interval-in-seconds=10 # 续约到期时间(单位秒,默认90秒) eureka.instance.lease-expiration-duration-in-seconds=10
org.springframework.boot
spring-boot-starter-actuator
Eureka-Server
#设为false,关闭自我保护 eureka.server.enable-self-preservation=false #清理间隔(单位毫秒,默认是60*1000) eureka.server.eviction-interval-timer-in-ms=10000
- 健康检查,注册中心将死去的服务剔除
总结
Eureka-Server
1、引入的是spring-cloud-starter-netflix-eureka-server,在启动类中使用的标注是@EnableEurekaServer
Eureka-Client
1、引入的是spring-cloud-starter-netflix-eureka-client,在启动类中使用的标注是@EnableEurekaClient