SpringCloud入门(一)Eureka 服务注册与发现
springcloud的中文文档网站:https://www.springcloud.cc/spring-cloud-dalston.html
简述
服务发现是基于微服务架构的关键原则之一。尝试配置每个客户端或某种形式的约定可能非常困难,可以非常脆弱。Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现 服务注册和发现。Eureka 采用了 C/S 的 设计架构。Eureka Server 作为 服务注册中心,系统中的 其他微服务,使用 Eureka 的 客户端 连接到 Eureka Server,并通过 心跳连接 检测服务的 存活状态。
Eureka Server 服务注册与发现中心端 提供服务注册和发现的能力(通常就是微服务中的注册中心)
Eureka Client
-
Service Provider服务提供者端 一个Java客户端,用于简化与 Eureka Server 的交互(通常就是微服务中的客户端和服务端)
Service Consumer服务调用者端 一个Java客户端,用于简化与 Eureka Server 的交互(通常就是微服务中的客户端和服务端)
各个微服务启动时,会通过 Eureka Client 向 Eureka Server 注册自己,Eureka Server 会存储该服务的信息也就是说,每个微服务的客户端和服务端,都会注册到 Eureka Server,这就衍生出了微服务相互识别的话题
同步:每个 Eureka Server 同时也是 Eureka Client(逻辑上的),多个 Eureka Server之间通过复制的方式完成服务注册表的同步,形成 Eureka 的高可用。
识别:Eureka Client 会缓存 Eureka Server中的信息,即使所有 Eureka Server 节点都宕掉,服务消费者仍可使用缓存中的信息找到服务提供者。
续约:微服务会周期性(默认30s)地向 Eureka Server 发送心跳以Renew(续约)信息(类似于heartbeat)
续期:Eureka Server 会定期(默认60s)执行一次失效服务检测功能,它会检查超过一定时间(默认90s)没有Renew的微服务,发现则会注销该微服务节点。
一. 简单实例 eureka server 和client
1. 搭建单点的服务注册中心
新建一个项目,本次使用的springcloud和boot版本为:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
加入eureka server的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
修改boot配置文件j加入相关配置
server:
port: 8761
eureka:
instance:
hostname: localhost #实例名
instance-id: ${spring.cloud.client.ip-address}:${server.port} # 实例详情显示
prefer-ip-address: true
client:
fetch-registry: false #设置是否从 Eureka Server 获取 注册信息,默认为 true。因为本例是一个 单点 的 Eureka Server,不需要 同步 其他 Eureka Server 节点的数据,所以设置为 false
register-with-eureka: false #设置是否将自己作为 Eureka Client 注册到 Eureka Server,默认为 true。
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置的是与 Eureka Server 的 交互地址,查询 和 注册服务 都依赖这个地址,如果有多个可以使用 英文逗号分隔。
server:
enable-self-preservation: true #Eureka的自我保护机制关闭,红字提醒(生产环境不推荐关闭)
eviction-interval-timer-in-ms: 60000 # 默认为60 * 1000ms (一分钟)
在启动类加入@EnableEurekaServer注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8761Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer8761Application.class, args);
}
}
启动服务,打开浏览器访问 localhost:8761,可以看到eureka的服务面板。
2. 搭建客户端client
再新建一个项目
pom文件加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在yml文件中加入配置
server:
port: 9090
spring:
application:
name: client-9090 #应用名称
eureka:
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port} # 实例id详情显示
prefer-ip-address: true #显示ip
client:
service-url:
defaultZone: http://localhost:8761/eureka #服务端的地址
在启动类上加入@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient
public class EurekaClient9090Application {
public static void main(String[] args) {
SpringApplication.run(EurekaClient9090Application.class, args);
}
}
启动之后,再启动服务端,打开eureka的服务面板
localhost:8761可以看到,实例已经出现在面板中
二. 搭建eureka集群
通过运行多个实例并请求他们相互注册,可以使Eureka更具弹性和可用性。Eureka 通过节点 对等注册 的方式实现 高可用的部署,所以只需要为每一个 Eureke Server 配置 其他可用的 Eureke Server 的 serviceUrl,就能实现高可用部署。
首先我们按照单机如法炮制1个相同的服务端项目。建立8762端口的项目。
8761的项目yml配置为:
server:
port: 8761
eureka:
instance:
hostname: localhost8761 #实例名
client:
service-url:
defaultZone: http://localhost:8762/eureka/ #设置的是与 Eureka Server 的 交互地址,查询 和 注册服务 都依赖这个地址,如果有多个可以使用 英文逗号分隔。
spring:
application:
name: eureka-server-8761
8762的项目:
server:
port: 8762
eureka:
instance:
hostname: localhost8762
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #设置的是与其他Eureka Server 的 交互地址
spring:
application:
name: eureka-server-8762
启动,咱们之前的9090和8761、8762三个项目。
访问服务面板
集群搭建成功,在8761和8762中都可以看到互相的服务,在客户端9090项目中serverurl可以只写一个,集群会进行同步,在8762中也能看到。
想在单机中实验的更真的话,可以修改hosts文件,再试一遍。
127.0.0.1 localhost8761 localhost8762
本文代码地址: