Eureka:服务发现工具
一、Eureka介绍
1. 什么是Eureka
Eureka是由Netflix公司推出的服务注册和发现工具(Service Discovery,平时说的注册中心)
2. 包含注册中心的软件架构图
3. Eureka角色
Eureka中分为两个角色:Eureka Server(Eureka服务)和Eureka Client(Eureka客户端)。无论是服务端还是客户端其本质都是一个Java项目,在Spring Cloud中主要通过启动类上添加@EnableEurekaServer和@EnableEurekaClient(可以省略)来区分当前应用程序是服务端还是客户端。
3.1 Eureka Server
可以理解成之前我们讲解的Zookeeper注册中心,只是现在使用的是Java项目实现的(Spring Cloud内嵌Eureka)。
3.2 Eureka Client
可以理解成所有需要注册到Eureka Server中的应用。为什么需要向注册中心中注册呢?因为注册后别人才能通过注册中心获取到项目信息和项目所在服务器信息,通过这些信息调用这个项目。Spring Cloud中每个项目调用的信息都存储在了注册中心中(Eureka)。
3.2.1 Application Service
应用服务端。是Eureka Client中的一个相对角色。
3.2.2 Application Client
应用客户端。是Eureka Client中的一个相对角色。
注意:在这里,Spring Cloud中没有绝对的Application Service和Application Client说法。如果A项目访问B项目,称A项目为Application Client,称B项目为Application Service。同时可能存在C访问A的情况,这是C项目是Application Client,A项目是Application Service。发现A项目又是Application Service又是Application Client,主要看针对哪个业务场景。无论是Applicatin Service还是Application Client都是Eureka Client。
三、搭建单机版Eureka Server
搭建Eureka Server时就相当于在安装Eureka软件。这是在Spring Cloud学习过程中接触的一种全新环境搭建方式,替换了之前需要安装、配置软件的问题。
1. POM依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>