三.SpringCloud极简入门-服务注册与发现-Eureka

﹏ヽ暗。殇╰゛Y 2023-07-17 06:51 106阅读 0赞

老鸟飞过,只做学习使用,欢迎交流

三. 服务注册与发现-Eureka

1. Eureka介绍

1.1. 什么是Eureka

微服务的其中一个特点是服务之间需要进行网络通信,服务器之间发起调用时调用服务得知道被调用服务的通信地址,试问当微服务数量成百上千之多,程序员该如何管理众多的服务通信地址,对于随时新增加的微服务和下线的微服务,又应该如何去动态添加和删除这些微服务的通信地址呢?所以手工管理服务的通信地址是一件遥不可及的事情,我们需要借助一个强大的工具帮我们实现这一功能 - Eureka,同类型的组件还有 zookeeper,consul等

1.2. Eureka的工作原理

服务注册

Eureka是一个服务注册与发现组件,简单说就是用来统一管理微服务的通信地址的组件,它包含了EurekaServer 服务端(也叫注册中心)和EurekaClient客户端两部分组成,EurekaServer是独立的服务,而EurekaClient需要集成到每个微服务中。

微服务(EurekaClient)在启动的时候会向EurekaServer提交自己的服务信息(通信地址如:服务名,ip,端口等),在 EurekaServer会形成一个微服务的通信地址列表存储起来。 — 这叫服务注册

服务发现

微服务(EurekaClient)会定期(RegistryFetchIntervalSeconds:默认30s)的从EurekaServer拉取一份微服务通信地址列表缓存到本地。当一个微服务在向另一个微服务发起调用的时候会根据目标服务的服务名找到其通信地址,然后基于HTTP协议向目标服务发起请求。—这叫服务发现

服务续约

另外,微服务(EurekaClient)采用定时(LeaseRenewalIntervalInSeconds:默认30s)发送“心跳”请求向EurekaServer发请求进行服务续约,其实就是定时向 EurekaServer发请求报告自己的健康状况,告诉EurekaServer自己还活着,不要把自己从服务地址清单中剔除掉,那么当微服务(EurekaClient)宕机未向EurekaServer续约,或者续约请求超时,注册中心机会从服务地址清单中剔除该续约失败的服务。

服务下线

微服务(EurekaClient)关闭服务前向注册中心发送下线请求,注册中心(EurekaServer)接受到下线请求负责将该服务实例从注册列表剔除

下面我们用一张图来介绍Eureka的工作流程

mark

2. EurekaServer实战

文章采用“用户”,“订单”,“支付”等业务来演示整个SpringCloud的各个组件

2.1.多模块项目结构

1.搭建项目结构

欲善其事,比利其器,为了方便演示SpringCloud的各个组件,我这里采用多模块的方式搭建项目,所有的jar包交给父工程来管理,搭建好的项目结构如下:

  1. springcloud-parent //父项目
  2. pom.xml //父项目的pom
  3. springcloud-eureka-server-1010 //注册中心EurekaServer
  4. springcloud-user-server-1020 //用户服务EurekaClient ,提供者
  5. springcloud-order-server-1030 //订单服务EurekaClient ,消费者

啥?不懂SpringBoot?找篇文章学一学!!! , 啥?多模块项目不会搭建?找篇文章学一学!!!

2.父项目管理依赖

SpringCloud的Jar包管理参照文档:https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/multi/multi\_\_quick\_start.html\#\_client\_side\_usage

springcloud-parent父工程负责管理SpringBoot和SpringCloud的jar包 ,pom.xml如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>cn.itsource.springboot</groupId>
  5. <artifactId>springcloud-parent</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <modules>
  8. <module>springcloud-eureka-server-1010</module>
  9. <module>springcloud-user-server-1020</module>
  10. <module>springcloud-order-server-1030</module>
  11. </modules>
  12. <packaging>pom</packaging>
  13. <!--公共的一些配置-->
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  19. </properties>
  20. <!--1.管理 SpringBoot的jar包-->
  21. <parent>
  22. <groupId> org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-parent</artifactId>
  24. <version>2.0.5.RELEASE</version>
  25. </parent>
  26. <!--2.管理 SpringCloud的jar包 -->
  27. <dependencyManagement>
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-dependencies</artifactId>
  32. <version>${spring-cloud.version}</version>
  33. <type>pom</type>
  34. <scope>import</scope>
  35. </dependency>
  36. </dependencies>
  37. </dependencyManagement>
  38. <!--3.这里是所有子项目都可以用的jar包-->
  39. <dependencies>
  40. <dependency>
  41. <groupId>junit</groupId>
  42. <artifactId>junit</artifactId>
  43. <version>4.12</version>
  44. <scope>test</scope>
  45. </dependency>
  46. </dependencies>
  47. </project>

2.2. 搭建Eureka Server

参照文档:https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/multi/multi\_spring-cloud-eureka-server.html\#netflix-eureka-server-starter

在springcloud-parent父工程下面搭建好子工程springcloud-eureka-server-1010,然后我们来集成EurekaServer。

1.导入依赖
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <!--集成自己的父工程-->
  4. <parent>
  5. <artifactId>springcloud-parent</artifactId>
  6. <groupId>cn.itsource.springboot</groupId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <modelVersion>4.0.0</modelVersion>
  10. <artifactId>springcloud-eureka-server-1010</artifactId>
  11. <name>springcloud-eureka-server-1010</name>
  12. <dependencies>
  13. <!--spring-cloud-starter-netflix-eureka-server -->
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. </dependencies>
  23. </project>

提示:spring-cloud-starter-netflix-eureka-server作为EurekaServer端的基础依赖,但同时这个包也把EurekaClient端的以来也导入进来了,spring-boot-starter-web作为web服务的基础依赖是不可缺少的。

2.主配置类
  1. /** * 注册中心启动类 * @EnableEurekaServer : 开启EurekaServer服务端 */
  2. @SpringBootApplication
  3. @EnableEurekaServer
  4. public class EurekaServerApplication1010
  5. {
  6. public static void main( String[] args )
  7. {
  8. SpringApplication.run(EurekaServerApplication1010.class);
  9. }
  10. }

提示:在主配置类上通过 @EnableEurekaServer 注解开启了EurekaServer端的功能。

3.application.yml配置文件
  1. server:
  2. port: 1010 #端口
  3. eureka:
  4. instance:
  5. hostname: localhost #主机
  6. client: #客户端配置
  7. registerWithEureka: false #EurekaServer自己不要注册到EurekaServer自己 ,只有EurekaClient才注册
  8. fetchRegistry: false #EurekaServer不要拉取服务的通信地址列表 ,只有EurekaClient才拉取地址列表
  9. serviceUrl: #注册中心的注册地址
  10. defaultZone: http://localhost:1010/eureka/ #http://${eureka.instance.hostname}:${server.port}/eureka/
  11. server:
  12. enable-self-preservation: false #关闭自我保护警告

提示:这里配置了EurekaServer的端口为 1010 ,主机名 localhost ,需要特别说明的是我们再引入EurekaServer的基础依赖spring-cloud-starter-netflix-eureka-server时,这个依赖即引入了EurekaServer所需要的包,也引入了EurekaClient的包,换句话说,现在的springcloud-eureka-server-1010工程既是一个 EurekaServer,也是一个EurekaClient。

我们这里暂时把EurekaClient的功能屏蔽掉 ,即关闭它的服务注册和发现功能,让他做好EurekaServer该做的事情即可。

  • serviceUrl是服务注册地址,EurekaClient需要注册到EurekaServer就得跟上该地址。
  • registerWithEureka=false :禁止自己向自己注册
  • fetchRegistry=false : 禁止拉取服务注册列表
4.启动测试

启动springcloud-eureka-server-1010工程,浏览器访问 http://localhost:1010 ,出现如下界面代码EurekaServer集成成功:

mark

2.3.Eureka自我保护

默认情况下,当EurekaServer接收到服务续约的心跳失败比例在15分钟之内低于85%,EurekaServer会把这些服务保护起来,即不会把该服务从服务注册地址清单中剔除掉,但是在此种情况下有可能会出现服务下线,那么消费者就会拿到一个无效的服务,请求会失败,那我们需要对消费者服务做一些重试,或在熔断策略。

当EurekaServer开启自我保护时,监控主界面会出现红色警告信息,我们可以使用eureka.server.enable-self-preservation=false来关闭EurekaServer的保护机制,这样可以确保注册中心中不可用的实例被及时的剔除,但是不推荐

关闭Eureka自我保护后的配置如下:

  1. server:
  2. port: 1010 #端口
  3. eureka:
  4. instance:
  5. hostname: localhost #主机
  6. client: #客户端配置
  7. registerWithEureka: false #EurekaServer自己不要注册到EurekaServer自己
  8. fetchRegistry: false #不要拉取服务的通信地址列表
  9. serviceUrl: #注册中心的注册地址
  10. defaultZone: http://localhost:1010/eureka/ #http://${eureka.instance.hostname}:${server.port}/eureka/
  11. server:
  12. enable-self-preservation: false #关闭自我保护

3.EurekaClient实战-用户服务

参照官方文档 : https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/multi/multi\_\_service\_discovery\_eureka\_clients.html\#netflix-eureka-client-starter
根据上一章节我们的Eureka的理解,Eureka分为服务端和客户端,服务端已经搭建成功,我们来搭建客户端。再来看一下我们之前的那张图:
mark

其实我们的用户服务 springcloud-user-server-1020 ,订单服务springcloud-order-server-1030 两个工程都是EurekaClient客户端,都需要去集成EurekaClient,我们先从springcloud-user-server-1020 下手 。

3.1.导入依赖

修改springcloud-user-server-1020,导入EurekaClient基础依赖:spring-cloud-starter-netflix-eureka-client,导入web的基础依赖:spring-boot-starter-web,具体的pom.xml如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <parent>
  4. <artifactId>springcloud-parent</artifactId>
  5. <groupId>cn.itsource.springboot</groupId>
  6. <version>1.0-SNAPSHOT</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>springcloud-user-server-1020</artifactId>
  10. <name>springcloud-user-server-1020</name>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. </dependencies>
  21. </project>

提示:不要忘记导入web的基础依赖 spring-boot-starter-web

3.2.主配置类

我们可以在主配置通过注解@EnableEurekaClient标记服务作为Eureka客户端

  1. /** * 用户的启动类 * @EnableEurekaClient: 标记该应用是 Eureka客户端 */
  2. @SpringBootApplication
  3. @EnableEurekaClient
  4. public class UserServerApplication1020
  5. {
  6. public static void main( String[] args )
  7. {
  8. SpringApplication.run(UserServerApplication1020.class);
  9. }
  10. }

提示:主配置类通过打@EnableEurekaClient注解开启EurekaClient客户端功能,当然如果不打这个标签也能实现功能,因为导入spring-cloud-starter-netflix-eureka-client 依赖后,默认就开启了EurekaClient

3.3.application.yml配置

在配置文件中,我们需要通过eureka.client.serviceUrl配置EurekaServer的地址,EurekaClient根据该地址把自己注册给服务端。

  1. #注册到EurekaServer
  2. eureka:
  3. client:
  4. serviceUrl:
  5. defaultZone: http://localhost:1010/eureka/
  6. spring:
  7. application:
  8. name: user-server
  9. server:
  10. port: 1020

提示:serviceUrl是EurekaServer注册中的地址,主机和端口都应该指向springcloud-eureka-server-1010工程,这里额外指定了服务的名字,和端口,这些信息都会被注册到EurekaServer

3.4.测试EurekaClient

启动springcloud-eureka-server-1010 , 启动springcloud-user-server-1020 , 浏览器再次访问http://localhost:1010,那你应该可以看到我们的user-server服务已经被注册到EurekaServer。如下:

mark

3.5.使用IP进行注册

默认情况下EurekaClient使用hostname进行注册到EurekaServer,我们希望使用ip进行注册,可以通过配置eureka.instance.prefer-ip-address=true来指定,同时为了方便区分和管理服务实例,我们指定服务的实例ID,通过eureka.instance.instance-id为user-serer:1020来指定,具体配置如下:

  1. #注册到EurekaServer
  2. eureka:
  3. client:
  4. serviceUrl:
  5. defaultZone: http://localhost:1010/eureka/
  6. instance:
  7. prefer-ip-address: true #使用ip地址进行注册
  8. instance-id: user-server:1020 #实例ID
  9. spring:
  10. application:
  11. name: user-server
  12. server:
  13. port: 1020

重启springcloud-user-server-1020 工程,访问注册中心,可以看到实例ID已经发生改变,如下:

mark

4.Eureka Client实战-订单服务

订单服务和用户服务的做法一样,只是yml配置文件中有些稍微的不同。

4.1.导入依赖

同用户服务一样,省略…

  1. ...省略部分不重要的内容...
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. </dependencies>

4.2.主配置类

同用户服务一样,省略…

  1. /** * 订单的启动类 * @EnableEurekaClient: 标记该应用是 Eureka客户端 */
  2. @SpringBootApplication
  3. @EnableEurekaClient
  4. public class OrderServerApplication1030
  5. {
  6. public static void main( String[] args )
  7. {
  8. SpringApplication.run(OrderServerApplication1030.class);
  9. }
  10. }

4.3.application.yml配置

订单服务除了服务名,端口和用户服务不一样,其他的都一样,如下:

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:1010/eureka/
  5. instance:
  6. prefer-ip-address: true #使用ip地址进行注册
  7. instance-id: order-server:1030 #实例ID
  8. spring:
  9. application:
  10. name: order-server
  11. server:
  12. port: 1030

4.4.测试

启动订单服务,访问Eureka Server的监控界面:http://localhost:1010,可以看到订单服务也注册进去了。

4.5.做个小结

到这里Eureka服务注册与发现案例已经完成,服务端和客户端的搭建都相对简单,一般都是导个包,打个标签,配置文件都搞定了,作为一个合格的程序员,我们不能光停留在用的层面,它的一些重要工作方式与思想也是需要我们去掌握。

发表评论

表情:
评论列表 (有 0 条评论,106人围观)

还没有评论,来说两句吧...

相关阅读