Eureka 基础知识
经过前面几天的准备工作,今天终于要来到 Spring Cloud 的一个核心组件的学习了,它就是服务注册中心 Eureka。
今天我们要添加服务注册中心 Eureka,并将 8001 和 80 两个服务注册到中心上。
大家肯定有一个疑问:80 调用 8001 ,这么简单的调用,为什么要加入服务注册中心呢?
这个先不着急,当服务多的时候,注册中心的作用就体现出来了(流量监控,是否有余量等等,进行统一调度协调)。
我们先从以下几个方面来认识一下 Eureka
什么是服务治理
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理,在传统的 rpc 远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
什么是服务注册
Eureka 采用了 CS 的设计架构,EurekaServer 作为服务注册功能的服务器,它是服务注册中心。而系统中的其也微服务,使用 Eureka 的客户端连接到 EurekaServer 并维持心跳连接。这样系统的维护人员就可以通过 EurekaServer 来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息比如服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地 RPC 调用RPC 远程调用框架核该心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc 远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))
Eureka 两组件
Eureka包含两个组件: Eureka Server 和 Eureka Client
Eureka Server 提供服务注册服务
各个微服务节点通过配置启动后,会在 EurekaServer 中进行注册,这样 EurekaServe 中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
EurekaClient 通过注册中心进行访问
是一个 Java 客户端,用于简化 EurekaServer 的交互,客户端同时也具备一个内置的、 使用轮询(round-robin)负载算法的负载均衡器
在应用启动后,将会向 EurekaServer 发送心跳默认周期为30秒。如果 Eureka Sever 在多个心跳周期内没有接收到某个节点的心跳,EurekaServer 将会从服务注册表中把这个服务节点移除(默认90秒)。
个人见解
对于服务注册中心,可以抽象理解为是你们公司所在地的物业,然后你们公司相当于一个微服务,你们公司注册到物业进行统一管理,就相当于微服务注册到服务注册中心进行统一管理。
Eureka Service 服务端安装
说了这么多,我们要来实践一下了,下面我们将搭建 Eureka 的服务端。
创建子模块 module:cloud-eureka-server7001
创建过程不再赘述。
改 pom 文件
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<!--Eureka的Service端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--引入自己定义发布的jar包 自己定义的jar通过maven的clean和install指令进行发布到本地仓库-->
<dependency>
<groupId>cn.com.dmg</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
编写 yml 文件
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名字 集群服务中地址为:eureka7001.com
client:
register-with-eureka: false #表识不向注册中心注册自己(相当于物业公司不和自己收物业费)
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
server:
enable-self-preservation: false #是否进行自我保护
eviction-interval-timer-in-ms: 2000 #心跳响应时间改为2秒
主启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
由于是服务注册中心,因此不需要业务类。
启动项目
启动项目,访问:http://localhost:7001/ 出现以下页面即为正常:
支付微服务 8001 和 订单微服务 80 入驻 Eureka service
服务注册中心启动完成之后后,下面我们要将微服务注册到其中,整体的一个架构如下:
修改 8001 和 80 的pom文件
eureka:
client:
register-with-eureka: true #注册到Eureka
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka #入住目标地址
此时微服务已经注册到了服务注册中心
至此,今天的任务已经完成,搭建了服务注册中心,并将之前创建的两个微服务 8001 和 80 注册到了服务注册中心,涉及到的源代码已经开源,可以自行获取:
28509410)]
至此,今天的任务已经完成,搭建了服务注册中心,并将之前创建的两个微服务 8001 和 80 注册到了服务注册中心,涉及到的源代码已经开源,可以自行获取: