Eureka
简介
Eureka
是Netflix
开源的服务发现组件,本身是一个基于 REST 的服务。包含Server
和Client
两部分。Spring Cloud
将它集成在子项目Spring Cloud Netflix
中,从而实现微服务的注册与发现。
Application Service
相当于服务提供者。Application Client
相当于服务消费者。Make Remote Call
可以理解成调用RESTful API
的行为。us-east-1c
,us-east-1d
等都是 zone,它们都是属于us-east-1
这个 Region(Spring Cloud
默认的 Region 是us-east-1
)
Eureka
包含两个组件Eureka Server
和Eureka Client
:
Eureka Server
提供服务发现能力,各个微服务启动时,会向Eureka Server
注册自己的信息,Eureka Server
存储这些信息。Eureka Client
是一个 Java 客户端,用于简化和Eureka Server
的交互。- 微服务启动后,会周期性(默认 30s)地向
Eureka Server
发送心跳以续约自己的租期。 - 如果
Eureka Server
在一定时间内没有接收到某个微服务实例的心跳,Eureka Server
将注销该实例(默认 90s)。 - 默认情况下,
Eureka Server
同时也是Eureka Client
。多个Eureka Server
实例相互之间通过复制的方式来实现微服务注册表中数据的同步。 Eureka Client
会缓存服务注册表中的信息,这种方式有一定的优势——首先,微服务无需每次请求都查询Eureka Server
,从而降低Eureka Server
的压力;其次,即使Eureka Server
所有节点宕机,服务消费者依然可以使用缓存中的信息找到服务提供者并完成调用。
创建单机Eureka Server
- 创建 Maven 工程,
pom
文件添加已下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
- 在配置文件
application.yml
中添加:
server:
port: 8761
eureka:
client:
# 表明是否将自己注册到Eureka Server中,默认为true。由于当前应用为Eureka Server,设为false。
registerWithEureka: false
# 表示是否从Eureka Server获取注册信息,默认为true,由于这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点数据,设为false。
fetchRegistry: false
# 设置与Eureka Server交互的地址,查询服务个注册服务都需要依赖这个地址。多个地址间可使用,分隔。
serviceUrl:
defaultZone: http://localhost:8761/eureka/
- 创建启动类,添加
@EnableEurekaServer
注解声明是一个Eureka Server
。
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryEurekaApplication.class, args);
}
}
将微服务注册到Eureka Server
上
- 在
pom
文件中添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 在配置文件
application.yml
中添加:
spring:
application:
# 指定注册到Eureka Server上的应用名称
name: microeservice-provider-user
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
# 表示将自己的IP注册到Eureka Server
prefer-ip-address: true
- 创建启动类。
@SpringBootApplication
public class ProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderUserApplication.class, args);
}
}
- 在
Spring Cloud Edgware
之前,要想将微服务注册到Eureka Server
,必须在启动类上添加@EnableEurekaClient
或@EnableDiscoverClient
。在Spring Cloud Edgware
以及更高版本,只需添加相关依赖,即可自动注册。 - 若不想讲服务注册到
Eureka Server
,只需设置spring.cloud.service-register.auto-registration.enabled=false
,或@EnableDiscoveryClient(autoRegister = false)
即可。
Eureka Server
的高可用
- 修改配置文件
application.yml
:
spring:
application:
name: microservice-discovery-eureka-ha
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
---
spring:
# 指定profile=peer1
profiles: peer1
server:
port: 8761
---
spring:
profiles: peer2
server:
port: 8762
IDEA
启动两个 Configurations,Program arguments
分别设置--spring.profiles.active=peer1
和--spring.profiles.active=peer2
。