Spring Cloud实战系列(一) - 服务注册与发现Eureka

今天药忘吃喽~ 2022-03-20 05:22 325阅读 0赞

前言

Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现 服务注册和发现Eureka 采用了 C-S设计架构Eureka Server 作为 服务注册中心,系统中的 其他微服务,使用 Eureka客户端 连接到 Eureka Server,并通过 心跳连接 检测服务的 存活状态

正文

  • Eureka Server: 作为 服务注册中心,提供 服务注册和发现
  • Eureka Client: 所有注册到 服务中心 的服务。

    • Service Provider: 把 自身的服务 注册到 Eureka Server,从而使 服务消费方 能够找到。
    • Service Consumer: 从 Eureka Server 获取 服务注册列表,从而能够 消费服务

1. 创建服务注册中心

创建 2 个项目 Module,一个 Module(即 Spring Boot)工程作为 服务注册中心,即 Eureka Server,另一个作为 Eureka Client

Eureka Server 创建完后的工程 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. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>1.5.3.RELEASE</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <groupId>io.ostenant.github.springcloud</groupId>
  11. <artifactId>eureka-server</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>eureka-server</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. <spring-cloud.version>Dalston.SR1</spring-cloud.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. </dependencies>
  30. <dependencyManagement>
  31. <dependencies>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-dependencies</artifactId>
  35. <version>${spring-cloud.version}</version>
  36. <type>pom</type>
  37. <scope>import</scope>
  38. </dependency>
  39. </dependencies>
  40. </dependencyManagement>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>

2. 启动服务注册中心

Eureka 是一个 高可用 的组件,它没有 后端缓存。每一个 实例 注册之后,需要 定时注册中心 发送 心跳(因此可以在内存中完成)。在默认情况下 Eureka Server 也是一个 Eureka Client,必须要指定一个 Server。在启动之前,首先对 Eureka Server 配置 application.yml 文件。

  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. registerWithEureka: false
  8. fetchRegistry: false
  9. serviceUrl:
  10. defaultZone: http://${ eureka.instance.hostname}:${ server.port}/eureka/
  • eureka.client.register-with-eureka:

设置是否将自己作为 Eureka Client 注册到 Eureka Server,默认为 true

  • eureka.client.fetch-registry

设置是否从 Eureka Server 获取 注册信息,默认为 true。因为本例是一个 单点Eureka Server,不需要 同步 其他 Eureka Server 节点的数据,所以设置为 false

  • eureka.client.service-url.defaultZone

设置的是与 Eureka Server交互地址查询注册服务 都依赖这个地址,如果有多个可以使用 英文逗号分隔

然后再把注解 @EnableEurekaServer 加在 Spring Boot 工程的启动类 Application 上面:

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

Eureka Server 是有界面的,启动项目后,打开浏览器访问 http://localhost:8761 即可查看。

3. 创建服务提供者

当一个 Eureka ClientEureka Server 发起 注册 时,它会提供一些 元数据,例如 主机端口 等等。Eureka Server 从每个 Eureka Client 实例接收 心跳消息。 如果 心跳超时,则通常将该实例从 Eureka Server 中删除。

创建一个 service-hiModule,创建完成后的 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. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>1.5.3.RELEASE</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <groupId>io.ostenant.github.springcloud</groupId>
  11. <artifactId>service-hi</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>service-hi</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. <spring-cloud.version>Dalston.SR1</spring-cloud.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-eureka</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-cloud-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. </dependencies>
  34. <dependencyManagement>
  35. <dependencies>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-dependencies</artifactId>
  39. <version>${spring-cloud.version}</version>
  40. <type>pom</type>
  41. <scope>import</scope>
  42. </dependency>
  43. </dependencies>
  44. </dependencyManagement>
  45. <build>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-maven-plugin</artifactId>
  50. </plugin>
  51. </plugins>
  52. </build>
  53. </project>

通过 注解 @EnableEurekaClient 表明自己是一个 Eureka Client

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @RestController
  4. public class ServiceHiApplication {
  5. @Value("${server.port}")
  6. private String port;
  7. public static void main(String[] args) {
  8. SpringApplication.run(ServiceHiApplication.class, args);
  9. }
  10. @RequestMapping("/hi")
  11. public String home(@RequestParam String name) {
  12. return "Hi " + name + ", I am from port: " + port;
  13. }
  14. }

仅仅 @EnableEurekaClient 是不够的,还需要在 配置文件 中注明的 服务注册中心 的地址,application.yml 配置文件如下:

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8763
  7. spring:
  8. application:
  9. name: service-hi

Eureka 客户端 需要指明 spring.application.name,用于服务的 唯一标识服务之间 相互调用会基于这个 name

启动并访问 Eureka Server 的地址 http://localhost:8761,会发现服务名称为 SERVICE-HI,端口为7862 的服务,已注册到 Eureka Server 的列表上。

3. 高可用Eureka Server

在一个 分布式系统 中,服务注册中心 是最重要的基础部分,必须处于 可以提供服务 的状态。为了维持其 可用性,使用 集群 是很好的解决方案。

Eureka 通过节点 对等注册 的方式实现 高可用的部署,所以只需要为每一个 Eureke Server 配置 其他可用的 Eureke ServerserviceUrl,就能实现高可用部署。

  1. spring:
  2. profiles:
  3. active: peer1 #peer2
  4. ---
  5. spring:
  6. profiles: peer1
  7. server:
  8. port: 8761
  9. eureka:
  10. instance:
  11. hostname: localhost
  12. client:
  13. serviceUrl:
  14. defaultZone: http://localhost:8762/eureka/
  15. ---
  16. spring:
  17. profiles: peer2
  18. server:
  19. port: 8762
  20. eureka:
  21. instance:
  22. hostname: localhost
  23. client:
  24. serviceUrl:
  25. defaultZone: http://localhost:8761/eureka/

更改 Eureka Server 的配置文件,再分别以 spring.profiles.active=peer1spring.profiles.active=peer2 作为参数,启动两次 Eureka Server 即可。

  • 访问 http://localhost:8761/。可以发现,Eureka Client 已经向 端口号8761Eureka Server 发起注册。
  • 服务提供者配置文件 并没有向 端口号8762Eureka Server 注册。访问 http://localhost:8762/。可以发现,服务提供者 的信息已经向 8762 发起注册了,即 8761注册信息 已经同步到 8762 节点。

参考

  • 方志朋《深入理解Spring Cloud与微服务构建》

欢迎关注技术公众号: 零壹技术栈

零壹技术栈

本帐号将持续分享后端技术干货,包括虚拟机基础,多线程编程,高性能框架,异步、缓存和消息中间件,分布式和微服务,架构学习和进阶等学习资料和文章。

发表评论

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

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

相关阅读