eureka基础
# eureka介绍
Eureka 是 Netflix 开源的服务注册发现组件,服务发现可以说是微服务架构的核心功能了,微服务部署之后,一定要有服务注册和发现的能力,Eureka 就是担任这个角色的。如果你用过 dubbo 的话,那一定知道 dubbo 中服务注册和发现的功能是用 zookeeper 来实现的。
Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件。它主要包括两个组件:Eureka Server 和 Eureka Client。 (1) Eureka Client:一个Java客户端,用于简化与 Eureka Server 的交互(通常就是微服务中的客户端和服务端) (2) Eureka Server:提供服务注册和发现的能力(通常就是微服务中的注册中心)
# eureka服务端
# 1、starter
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
2
3
4
5
6
# 2、开启注解@EnableEurekaServer
# 3、写配置
eureka:
instance:
hostname: localhost # eureka服务端的实例名字,集群时,配合hosts来使用
prefer-ip-address: true # 基于ip配置
client:
register-with-eureka: false #注册到其他eureka, 表示不向注册中心注册自己
fetch-registry: false #从其他eureka拉取信息,取消检索服务,表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
#注册中心路径,如果有多个eureka server,就相互注册,用","进行区分,如"http://address:8888/eureka,http://address:8887/eureka"
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #单机defaultZone
server:
enable-self-preservation: false # 自我保护,测试时关闭自我保护机制,保证不可用服务及时踢出, 生产环境时需默认开启
2
3
4
5
6
7
8
9
10
11
12
13
# 4、集群配置
方式一: idea里面复制一个配置,指定profiles,
方式二:复制多份单机eureka server, defaultZone配置相互注册
参考文章 :
Eureka集群搭建 - 小蜜疯 - 博客园 (cnblogs.com) (opens new window)
微服务Eureka使用详解 - 一响贪欢 - 博客园 (cnblogs.com) (opens new window)
# eureka客户端
# 1、引starter
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2
3
4
# 2、添加注解@EnableEurekaClient
@EnableEurekaClient注解也可以不写
# 3、写配置
eureka:
instance:
hostname: localhost
prefer-ip-address: true
lease-renewal-interval-in-seconds: 5 #心跳间隔默认30s
lease-expiration-duration-in-seconds: 10 #心跳超时时间默认90s。超时表示服务不可用,将它的实例从注册中心移除(server开启保护机制就不会)
client:
register-with-eureka: true #向注册中心注册自己, 默认,可省略不写
fetch-registry: true # 默认,可省略不写
service-url:
defaultZone: http://localhost:9001/eureka # 如果向多个注册中心注册,用“,”进行分隔
2
3
4
5
6
7
8
9
10
11
12
如果eureka server开启了security认证,则eureka-client注册到有登录认证的注册中心时,配置文件中需要修改注册中心地址格式
// eureka server端的security配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
// client端配置defaultZone
http://${username}:${password}@${hostname}:${port}/eureka/
2
3
4
5
6
7
8
9
# 自我保护机制
默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。
由于正式环境,经常会有网络故障,网络延迟问题发生,服务和注册中心无法正常通信,此时服务是正常的,不应该注销该服务,Eureka这时候,就通过“自我保护模式”来解决问题,当短时间和服务失去通信时,保留服务信息,当恢复网络和通信时候,退出“自我保护模式”;
通过“自我保护模式”,使Eureka集群更加的健壮和稳定,生产环境默认开启
# eureka常用配置:
eureka:
client: #eureka客户端配置
register-with-eureka: true #是否将自己注册到eureka服务端上去
fetch-registry: true #是否获取eureka服务端上注册的服务列表
service-url:
defaultZone: http://localhost:8001/eureka/ # 指定注册中心地址
enabled: true # 启用eureka客户端
registry-fetch-interval-seconds: 30 #定义去eureka服务端获取服务列表的时间间隔
instance: #eureka客户端实例配置
lease-renewal-interval-in-seconds: 30 #定义服务多久去注册中心续约
lease-expiration-duration-in-seconds: 90 #定义服务多久不去续约认为服务失效
metadata-map:
zone: guangdong #所在区域
hostname: localhost #服务主机名称
prefer-ip-address: false #是否优先使用ip来作为主机名
server: #eureka服务端配置
enable-self-preservation: false #关闭eureka服务端的保护机制
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
参考文章 :
Spring Cloud 系列之 Eureka 实现服务注册与发现 - 风的姿态 - 博客园 (cnblogs.com) (opens new window)