springcloud 入门系列(一) 高可用注册中心-eureka

超、凢脫俗 2021-12-11 15:49 441阅读 0赞

  1. 最近公司小组内部做springcloud主题分享,后续这边会同步更新springcloud 系列分享,这个系列的主旨是带大家入门springcloud,当然会穿插一些自己踩的坑做单独讲解。本系列主题默认大家已经熟知springboot.
  2. 下面简单介绍下什么是springcloud:“Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式会话,集群状态)。分布式系统的协调导致了样板模式, 使用Spring Cloud开发人员可以快速地支持实现这些模式的服务和应用程序。他们将在任何分布式环境中运行良好,包括开发人员自己的笔记本电脑,裸机数据中心,以及Cloud Foundry等托管平台”

以上其实是 spring 官方文档的中文翻译,说白了就是一个分布式开发框架,他能够组装我们所需要的常见分布式服务组件功能,比如服务自动发现,服务注册,服务调用,服务的负载,路由,熔断等等。

  1. 本节主要讲springcloud的服务自动注册于发现组件实现:springcoud eureka,这里再简单说明下为什么需要注册中心,其实没有注册中心,我们依然可以搭建分布式服务,我们可以把服务列表做成配置形式,或者直接存储db一样可以实现,如果服务比较少靠这种方式进行维护服务列表还算勉强接受,但是想想下如果有几千个应用并且每个服务都有至少有3个节点,可以想象下运维同事的肯定会崩溃,服务的上线和下线也成了致命的问题,可能会导致这个服务崩溃,所以这里服务的自动注册于发现的需求就是必然了,他屏蔽了应用对服务的发现与注册的底层实现,只关心自身需要调用什么服务即可。
  2. 下面我们开始搭建注册中心,交代下springboot 的版本以及springboot 版本:springcloudGreenwich.SR1),springboot (2.1.3.RELEASE). 笔者发现各个版本之间的差异还是很大的(比如个版本之间的配置差异),因此踩的坑也是不少,希望大家注意,不要因此引发血案。
  3. 这里搭建借助于intel ideal ,如图:

640_wx_fmt_png

这个相信大部分同学比较熟悉了,这里不再累述。这里直接贴出pom文件依赖:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.3.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <groupId>com.ethan.springcloud</groupId>
  8. <artifactId>eureka-server</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10. <name>eureka-server</name>
  11. <description>Demo project for Spring Boot</description>
  12. <properties>
  13. <java.version>1.8</java.version>
  14. <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  24. </dependency>

引导类:

  1. @EnableEurekaServer
  2. @SpringBootApplication
  3. public class EurekaServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaServerApplication.class, args);
  6. }
  7. }

使用 @EnableEurekaServer 注解开启 eureka Server 服务功能。

配置文件,application.properties:

  1. spring.application.name=eurekaServer
  2. server.port=8070
  3. eureka.instance.appname=eurekaServer
  4. # 禁止自己注册为eureka 服务
  5. eureka.client.register-with-eureka=false
  6. ## 检索服务
  7. eureka.client.fetch-registry=false

默认情况下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,配置文件中已经有注释。

我们启动应用之后,访问http://localhost:8070/查看界面:

640_wx_fmt_png 1

可以看到注册中心启动成功,并且当前还没有服务进行注册,至此但单节点的注册中心启动成功。

那么如何搭建高可用注册中心呢,很简单,我们把eurekaServer 自己也注册成服务,并且注册到其他的注册中心即可。我们复制一份刚才的配置文件进行修改:

640_wx_fmt_png 2

application-dev.properties:

  1. spring.application.name=eurekaServer
  2. server.port=8070
  3. #eureka.instance.hostname=node1
  4. eureka.instance.appname=eurekaServer
  5. # 禁止自己注册为eureka 服务
  6. eureka.client.register-with-eureka=true
  7. ## 检索服务
  8. eureka.client.fetch-registry=true
  9. eureka.client.service-url.defaultZone=http://localhost:8080/eureka

application-test.properties:

  1. spring.application.name=eurekaServer
  2. server.port=8080
  3. eureka.instance.hostname=localhost
  4. eureka.client.register-with-eureka=true
  5. # 检索服务
  6. eureka.client.fetch-registry=true
  7. eureka.server.enable-self-preservation=false
  8. eureka.client.service-url.defaultZone=http://localhost:8070/eureka
  9. eureka.instance.prefer-ip-address=true

然后我们再分别启动dev 和 test 配置,然后访问http://localhost:8070/,结果如图:

640_wx_fmt_png 3

访问:http://localhost:8080/,如图:

640_wx_fmt_png 4

我们看到两个注册中心节点都注册 成了eureka 节点了,

EUREKASERVER 是服务名,注册中心默认会全部大写显示,至此高可用注册中心搭建完成,实际应用中,我们可以根据需要进行水平扩展注册中心。

  1. 下面再看下如何注册eureka 客户端,先看 pom文件:
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.1.3.RELEASE</version>
  6. <relativePath/> <!-- lookup parent from repository -->
  7. </parent>
  8. <groupId>com.ethan.springcloud</groupId>
  9. <artifactId>hello-service</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <name>hello-service</name>
  12. <description>Demo project for Spring Boot</description>
  13. <properties>
  14. <java.version>1.8</java.version>
  15. <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  25. </dependency>

引导类:

  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. public class HelloServiceApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(HelloServiceApplication.class, args);
  6. }
  7. }

配置文件:

  1. spring:
  2. application:
  3. name: helloService
  4. eureka:
  5. client:
  6. service-url:
  7. defaultZone: http://localhost:8080/eureka
  8. server:
  9. port: 8081

然后启动应用,访问注册中心:

640_wx_fmt_png 5

发现服务已经注册到注册中了,这里提一句,eureka 服务名默认为

  1. spring.application.name

本节分享到此结束,如有不对或者疑问之处,欢迎各位指正!

640_wx_fmt_png 6

发表评论

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

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

相关阅读