服务注册中心是服务实现服务化管理的核心组件,相似于目录服务的做用,主要用来存储服务信息,譬如提供者 url 串、路由信息等。服务注册中心是微服务架构中最基础的设施之一。java
在微服务架构流行以前,注册中心就已经开始出如今分布式架构的系统中。好比 Dubbo 是一个在国内比较流行的分布式框架,被大量的中小型互联网公司所采用,它提供了比较完善的服务治理功能,而服务治理的实现主要依靠的就是注册中心。web
注册中心能够说是微服务架构中的“通信录”,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务须要调用其它服务时,就到这里找到服务的地址,进行调用。spring
举个现实生活中的例子,好比说,咱们手机中的通信录的两个使用场景:apache
当我想给张三打电话时,那我须要在通信录中按照名字找到张三,而后就能够找到他的手机号拨打电话。—— 服务发现李四办了手机号并把手机号告诉了我,我把李四的号码存进通信录,后续,我就能够从通信录找到他。—— 服务注册api
通信录 —— ?什么角色(提示:服务注册中心)安全
总结:服务注册中心的做用就是服务的注册和服务的发现。服务器
特性 | 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集成 | 支持 | 支持 | 支持 | 支持 |
了解了什么是注册中心,那么咱们继续谈谈,为何须要注册中心。在分布式系统中,咱们不单单是须要在注册中心找到服务和服务地址的映射关系这么简单,咱们还须要考虑更多更复杂的问题:架构
这些问题的解决都依赖于注册中心。简单看,注册中心的功能有点相似于 DNS 服务器或者负载均衡器,而实际上,注册中心做为微服务的基础组件,可能要更加复杂,也须要更多的灵活性和时效性。因此咱们还须要学习更多 Spring Cloud 微服务组件协同完成应用开发。app
Eureka 是 Netflix 开发的服务发现组件,自己是一个基于 REST 的服务。Spring Cloud 将它集成在其子项目 Spring Cloud Netflix 中,实现 Spring Cloud 的服务注册与发现,同时还提供了负载均衡、故障转移等能力。负载均衡
经过 Register、Get、Renew 等接口提供服务的注册和发现。
服务提供方,把自身的服务实例注册到 Eureka Server 中。
服务调用方,经过 Eureka Server 获取服务列表,消费服务。
点击连接观看:Eureka 入门案例视频(获取更多请关注公众号「哈喽沃德先生」)
eureka-demo
聚合工程。SpringBoot 2.2.4.RELEASE
、Spring Cloud Hoxton.SR1
。
咱们建立聚合项目来说解 Eureka,首先建立一个 pom 父工程。
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>
在刚才的父工程下建立 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 集群、架构原理、自我保护、优雅停服、安全认证等功能实现。记得关注噢~
本文采用 知识共享「署名-非商业性使用-禁止演绎 4.0 国际」许可协议
。
你们能够经过 分类
查看更多关于 Spring Cloud
的文章。
? 您的点赞
和转发
是对我最大的支持。
? 扫码关注 哈喽沃德先生
「文档 + 视频」每篇文章都配有专门视频讲解,学习更轻松噢 ~