Eureka 服务注册中心
1、什么是注册中心
注册中心相当于微服务架构中的“通讯录”,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到注册中心,当服务需要调用其他服务时,就到这里找到服务的地址,进行调用。
举个例子
-
服务发现:你先要给张三打电话,于是你在通讯录中寻找张三的电话,并拨打电话
-
服务注册:李四告诉了你的电话号,你把电话号存在通讯录中以方便以后在通讯中找到他
总结:服务注册中心的作用就是服务的注册和服务的发现
2、常见的注册中心
- Netflix Eureka
- Alibaba Nacos
- HashiCorp Consul
- Apache Zookeeper
- CoreOS Etcd
- CNCF CoreDNS
特性 | Eureka | Nacos | Consul | Zookeeper |
---|---|---|---|---|
CAP | AP | CP+AP | CP | CP |
健康检查 | Client Beat | TCP/HTTP/MySQL/Client Beat | TCP/HTTP/gRPC/Cmd | Keep Alive |
雪崩保护 | 有 | 有 | 无 | 无 |
自动注销实例 | 支持 | 支持 | 不支持 | 支持 |
访问协议 | HTTP | HTTP/DNS | HTTP/DNS | TCP |
监听支持 | 支持 | 支持 | 支持 | 支持 |
多数据中心 | 支持 | 支持 | 支持 | 不支持 |
跨注册中心同步 | 不支持 | 支持 | 支持 | 不支持 |
SpringCloud集成 | 支持 | 支持 | 支持 | 支持 |
3、为什么需要注册中心
在分布式系统中,我们不仅仅是需要在注册中心找到服务和服务地址的映射关系这么简单,还需要考虑更多更复杂的问题:
- 服务注册后,如何被及时的发现
- 服务宕机后,如何及时下线
- 服务如何有效的水平扩展
- 服务发现后,如何进行路由
- 服务异常时,如何进行降级
- 注册中心如何实现自身的高可用
这些问题的解决都依赖于注册中心。简单来说,注册中心的功能类似于 DNS 服务器或者负载均衡器,而实际上,注册中心作为微服务的基础组件,可能要更加复杂,也需要更多的灵活性和时效性。所以我们还需要学习更多的 SpringCloud 微服务组件协同完成应用开发。
4、什么是 Eureka 注册中心
Eureka 是 Netflix 开发的服务发现组件,本身是一个基于 REST 的服务。SpringCloud 将它集成在其子项目 SpringCloud Netflix 中,实现 SpringCloud 的服务注册与发现,同时还提供了负载均衡、故障转移等能力。
5、Eureka 注册中心三种角色
- Eureka Server
- 通过 Register、Get、Renew 等接口提供服务的注册和发现
- Application Service(Service Provider)
- 服务提供方,把自身的服务实例注册到 Euraka Server 中
- Application Client(Service Consumer)
- 服务调用方,通过 Eureka Server 获取服务列表,消费服务
- 0、实例化服务
- 1、将服务注册到注册中心
- 2、注册中心收录服务
- 3、注册中心获取服务列表
- user-service 127.0.0.1:9091
- goods-service 127.0.0.1:9092
- sso-service 127.0.0.1:9093
- 4、基于负载均衡算法从地址列表选择一个服务地址进行服务调用
- 5、定期发送心跳
- 6、检查没有定期发送心跳的服务并在一定时间内剔除服务地址列表
6、Eureka 入门案例
单节点版本!
创建纯 maven 项目,不勾选 Create from archetype,作为父项目
添加依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fengluo</groupId>
<artifactId>eureka-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!-- Spring Cloud Hoxton.SR1 依赖 -->
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<!-- 集成 spring-boot-starter-parent 依赖 -->
<!-- 使用集成方式,实现复用,符合继承的都可以被使用 -->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.4.RELEASE</version>
</parent>
<!-- 项目依赖管理 父项目只是声明依赖,子项目需要写明需要的依赖(可以省略版本信息)-->
<dependencyManagement>
<dependencies>
<!-- spring cloud 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
创建 Eureka 节点,在 Create from archetype 中使用 quickstart 模板
添加 eureka-server01 的依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server01</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.fengluo</groupId>
<artifactId>eureka-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<name>eureka-server01</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- netflix eureka server 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- spring boot web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
EurekaServerApplication 启动类
package org.fengluo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
application.yml
server:
port: 8761 # 端口
spring:
application:
name: eureka-server # 注册名称
# 配置 Eureka Server 注册中心
eureka:
instance:
hostname: localhost # 主机名,不配置的时候将根据操作系统的主机名来获取
client:
register-with-eureka: false # 是否将自己注册到注册中心,默认为 true
fetch-registry: false # 是否从注册中心获取服务注册信息,默认为 true
service-url: # 注册中心对外暴露的注册地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动项目
至此,我们的单节点 Eureka Server 注册中心就配置成功了!