Spring Cloud 系列之 Netflix Eureka 注册中心(一)

服务注册中心是服务实现服务化管理的核心组件,类似于目录服务的作用,主要用来存储服务信息,譬如提供者 url 串、路由信息等。服务注册中心是微服务架构中最基础的设施之一。

  在微服务架构流行之前,注册中心就已经开始出现在分布式架构的系统中。比如 Dubbo 是一个在国内比较流行的分布式框架,被大量的中小型互联网公司所采用,它提供了比较完善的服务治理功能,而服务治理的实现主要依靠的就是注册中心。

  

1|0什么是注册中心

  

  注册中心可以说是微服务架构中的“通讯录”,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用。

  举个现实生活中的例子,比如说,我们手机中的通讯录的两个使用场景:

当我想给张三打电话时,那我需要在通讯录中按照名字找到张三,然后就可以找到他的手机号拨打电话。—— 服务发现

李四办了手机号并把手机号告诉了我,我把李四的号码存进通讯录,后续,我就可以从通讯录找到他。—— 服务注册

通讯录 —— ?什么角色(提示:服务注册中心)

  总结:服务注册中心的作用就是服务的注册服务的发现

  

2|0常见的注册中心

  

  • Netflix Eureka
  • Alibaba Nacos
  • HashiCorp Consul
  • Apache ZooKeeper
  • CoreOS Etcd
  • CNCF CoreDNS

  

特性EurekaNacosConsulZookeeper
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|0为什么需要注册中心

  

  了解了什么是注册中心,那么我们继续谈谈,为什么需要注册中心。在分布式系统中,我们不仅仅是需要在注册中心找到服务和服务地址的映射关系这么简单,我们还需要考虑更多更复杂的问题:

  • 服务注册后,如何被及时发现
  • 服务宕机后,如何及时下线
  • 服务如何有效的水平扩展
  • 服务发现时,如何进行路由
  • 服务异常时,如何进行降级
  • 注册中心如何实现自身的高可用

  这些问题的解决都依赖于注册中心。简单看,注册中心的功能有点类似于 DNS 服务器或者负载均衡器,而实际上,注册中心作为微服务的基础组件,可能要更加复杂,也需要更多的灵活性和时效性。所以我们还需要学习更多 Spring Cloud 微服务组件协同完成应用开发。

  

4|0注册中心解决了什么问题

  

  • 服务管理
  • 服务的依赖关系管理

  

5|0什么是 Eureka 注册中心

  

  Eureka 是 Netflix 开发的服务发现组件,本身是一个基于 REST 的服务。Spring Cloud 将它集成在其子项目 Spring Cloud Netflix 中,实现 Spring Cloud 的服务注册与发现,同时还提供了负载均衡、故障转移等能力。

  

6|0Eureka 注册中心三种角色

  

6|1Eureka Server

  

  通过 Register、Get、Renew 等接口提供服务的注册和发现。

  

6|2Application Service(Service Provider)

  

  服务提供方,把自身的服务实例注册到 Eureka Server 中。

  

6|3Application Client(Service Consumer)

  

  服务调用方,通过 Eureka Server 获取服务列表,消费服务。

  

  

7|0Eureka 入门案例

  

  点击链接观看:Eureka 入门案例视频(获取更多请关注公众号「哈喽沃德先生」)

  eureka-demo 聚合工程。SpringBoot 2.2.4.RELEASESpring Cloud Hoxton.SR1

  

7|1创建项目

  

  我们创建聚合项目来讲解 Eureka,首先创建一个 pom 父工程。

  

7|2添加依赖

  

  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>com.example</groupId> <!-- 项目模块名称 --> <artifactId>eureka-demo</artifactId> <!-- 项目版本名称 快照版本SNAPSHOT、正式版本RELEASE --> <version>1.0-SNAPSHOT</version> <!-- 继承 spring-boot-starter-parent 依赖 --> <!-- 使用继承方式,实现复用,符合继承的都可以被使用 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> </parent> <!-- 集中定义依赖组件版本号,但不引入, 在子工程中用到声明的依赖时,可以不加依赖的版本号, 这样可以统一管理工程中用到的依赖版本 --> <properties> <!-- Spring Cloud Hoxton.SR1 依赖 --> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <!-- 项目依赖管理 父项目只是声明依赖,子项目需要写明需要的依赖(可以省略版本信息) --> <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>

  

7|3注册中心 eureka-server

  

  在刚才的父工程下创建 eureka-server 注册中心的项目。

  

创建项目

  

  

添加依赖

  

  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>com.example</groupId> <artifactId>eureka-server</artifactId> <version>1.0-SNAPSHOT</version> <!-- 继承父依赖 --> <parent> <groupId>com.example</groupId> <artifactId>eureka-demo</artifactId> <version>1.0-SNAPSHOT</version> </parent> <!-- 项目依赖 --> <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> </project>

  

配置文件

  

  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/

  此时如果直接启动项目是会报错的,错误信息:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect,这是因为 Eureka 默认开启了将自己注册至注册中心从注册中心获取服务注册信息的配置,如果该应用的角色是注册中心并是单节点的话,要关闭这两个配置项。

  

启动类

  

  EurekaServerApplication.java

package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication // 开启 EurekaServer 注解 @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }

  

访问

  

  访问:http://localhost:8761/

下一篇我们讲解 Eureka 集群、架构原理、自我保护、优雅停服、安全认证等功能实现。记得关注噢~

posted @ 2020-05-11 10:28  YoungDeng  阅读(172)  评论(0编辑  收藏  举报