1 微服务的注册中心

1.1 概述

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

微服务注册中心概述.jpg

1.2 注册中心的主要作用

  • 服务注册中心(简称注册中心)是微服务架构非常重要的一个组件,在微服务架构里面起到了协调者的作用。注册中心一般包含如下几个功能:
  • 服务注册Eureka基础(已过时) - 图2服务发现:
    • 服务注册/反注册:保存服务提供者和服务调用者的信息。
    • 服务订阅/取消订阅:服务调用者订阅服务提供者的信息,最好有实时推送的功能。
    • 服务路由(可选):具有筛选整合服务提供者的能力。
  • 服务注册Eureka基础(已过时) - 图3服务配置:
    • 配置订阅:服务提供者和服务调用者订阅服务相关的配置。
    • 配置下发:主动将配置推送给服务提供者和服务调用者。
  • 服务注册Eureka基础(已过时) - 图4服务健康检测:
    • 检测服务提供者的健康状况。

1.3 常见的注册中心

1.3.1 zookeeper

  • zookeeper是一个分布式服务框架,是Apache Hadoop的一个子项目,它主要是用来解决分布式应用经常遇到的一些数据管理问题,如统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等等。
  • 简单的说,zookeeper=文件系统+监听通知机制。

1.3.2 Eureka

  • Eureka是在Java语言上,基于RESTful API开发的服务注册和发现组件,SpringCloud Netflix中的重要组件。

1.3.3 Consul

  • Consul是HashiCorp基于Go语言开发的支持多数据中心分布式高可用的服务发现和注册软件,采用Raft算法保证服务的一致性,且支持健康检查。

1.3.4 Nacos

  • Nacos是一个更基于构建云原生应用的动态服务发现、配置管理和服务管理平台。简单的说,Nacos就是注册中心+配置中心的组合,提供简单易用的特性集,帮助我们解决微服务开发必会涉及到的服务注册和发现、服务配置、服务管理等问题。Nacos还是SpringCloud Alibaba组件之一,负责服务注册和发现。

1.3.5 总结

组件名 语言 CAP 一致性算法 服务健康检查 对外暴露接口
Eureka Java AP 可以配置支持 HTTP
Consul Go CP Raft 支持 HTTP/DNS
Zookeeper Java CP Paxos 支持 客户端
Nacos Java AP Raft 支持 HTTP

2 Eureka的概述

2.1 Eureka的基础知识

  • Eureka是Netflix开发的服务发现框架,SpringCloud将它集成到自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能。

Eureka基础知识.png

  • 上图简要描述了Eureka的基本架构,由3个角色组成:
  • 服务注册Eureka基础(已过时) - 图6Eureka Server:提供服务注册和发现。
  • 服务注册Eureka基础(已过时) - 图7Service Provider:服务提供方(将自身服务注册到Eureka,从而使得服务消费方能够找到)。
  • 服务注册Eureka基础(已过时) - 图8Service Consumer:服务消费方(从Eureka获取注册服务列表,从而能够消费服务)。

2.2 Eureka的交互流程和原理

Eureka交互原理.png

  • 由上图可知,Eureka包含两个组件:Eureka Server和Eureka Client,它们的作用如下:
    • 服务注册Eureka基础(已过时) - 图10Eureka Client是一个Java客户端,用于简化和Eureka Server的交互。
    • 服务注册Eureka基础(已过时) - 图11Eureka Server提供服务发现能力,各个微服务启动时,会通过Eureka Client向Eureka Server进行注册自己的信息(例如网络信息),Eureka Server会存储该服务的信息。
    • 服务注册Eureka基础(已过时) - 图12微服务启动后,会周期性的向Eureka Server发送心跳(默认周期为30秒)以续约自己的信息。如果Eureka Server在一定时间内(默认为90秒)没有接收到某个微服务节点的心跳,Eureka Server将会注销该微服务节点。
    • 服务注册Eureka基础(已过时) - 图13每个Eureka Server同时也是Eureka Client,多个Eureka Server之间通过复制的方式完成服务注册表的同步。
    • 服务注册Eureka基础(已过时) - 图14Eureka Client会缓存Eureka Server中的信息。即使所有的Eureka Server节点都宕机,服务消费者依然可以使用缓存中的信息找到服务提供者。
  • 综上,Eureka通过心跳检测、健康检查和客户端缓存等机制,提高了系统的灵活性、可伸缩性和可用性。

3 Eureka的使用步骤

  • 服务注册Eureka基础(已过时) - 图15搭建Eureka Server。
    • 创建工程(eureka_server)。
    • 导入Eureka对应的坐标。
    • 配置application.yml。
    • 配置启动类。
  • 服务注册Eureka基础(已过时) - 图16将服务提供者注册到Eureka Server上。
  • 服务注册Eureka基础(已过时) - 图17服务消费者通过注册中心获取服务列表,并调用。

4 搭建Eureka Server(注册中心)

4.1 搭建Eureka Server(注册中心)

4.1.1 在pom.xml中导入相关jar包的坐标

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>eureka_service</artifactId>
  12. <dependencies>
  13. <!-- 导入Eureka Server对应的坐标 -->
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  17. </dependency>
  18. </dependencies>
  19. </project>

4.1.2 配置yml

  • application.yml
  1. server:
  2. port: 9000 #端口
  3. #配置Eureka Server
  4. eureka:
  5. instance:
  6. # 主机地址名称
  7. hostname: localhost
  8. client:
  9. register-with-eureka: false # 是否将自己注册到注册中心
  10. fetch-registry: false # 是否从Eureka中获取服务列表
  11. service-url: # 配置暴露给Eureka Client的请求地址
  12. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4.1.3 配置启动类

  • EurekaApplication.java
  1. package com.sunxiaping.eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * @author 许威威
  7. * @version 1.0
  8. */
  9. @SpringBootApplication
  10. @EnableEurekaServer //开启Eureka Server
  11. public class EurekaApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(EurekaApplication.class, args);
  14. }
  15. }

4.2 服务中心管理平台

服务注册中心管理后台.png

5 服务注册到Eureka注册中心

5.1 在商品微服务中引入Eureka Client的坐标

  • 修改部分:
  1. <!-- 导入Eureka Client对应的坐标 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  • 完整部分:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>product_service</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-data-jpa</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>mysql</groupId>
  19. <artifactId>mysql-connector-java</artifactId>
  20. </dependency>
  21. <!-- 导入Eureka Client对应的坐标 -->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  25. </dependency>
  26. </dependencies>
  27. <build>
  28. <plugins>
  29. <plugin>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-maven-plugin</artifactId>
  32. <version>2.2.2.RELEASE</version>
  33. <configuration>
  34. <fork>true</fork>
  35. </configuration>
  36. <executions>
  37. <execution>
  38. <goals>
  39. <goal>repackage</goal>
  40. </goals>
  41. </execution>
  42. </executions>
  43. </plugin>
  44. </plugins>
  45. </build>
  46. </project>

5.2 修改yml配置文件

  • application.yml
  1. server:
  2. port: 9001 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-product # 微服务的名称
  6. datasource:
  7. url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. username: root
  10. password: 123456
  11. jpa:
  12. generate-ddl: true
  13. show-sql: true
  14. open-in-view: true
  15. database: mysql
  16. # 配置 eureka
  17. eureka:
  18. client:
  19. service-url: # Eureka Server的地址
  20. defaultZone: http://localhost:9000/eureka/

5.3 配置启动类

  • ProductApplication.java
  1. package com.sunxiaping.product;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. @SpringBootApplication
  6. @EnableEurekaClient //开启Eureka Client
  7. public class ProductApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(ProductApplication.class, args);
  10. }
  11. }

5.4 测试

服务提供者注册到Eureka Server中.png

5.5 actuator与注册微服务信息完善

5.5.1 主机名:服务名称修改

  • 当前问题:

主机名称服务名称修改之前.png

  • 修改服务提供者的application.yml文件。
  • 修改部分:
  1. # 配置 eureka
  2. eureka:
  3. instance:
  4. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  5. instance-id: service-product:9001
  6. client:
  7. service-url: # Eureka Server的地址
  8. defaultZone: http://localhost:9000/eureka/
  • 完整部分:
  1. server:
  2. port: 9001 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-product # 微服务的名称
  6. datasource:
  7. url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. username: root
  10. password: 123456
  11. jpa:
  12. generate-ddl: true
  13. show-sql: true
  14. open-in-view: true
  15. database: mysql
  16. # 配置 eureka
  17. eureka:
  18. instance:
  19. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  20. instance-id: service-product:9001
  21. client:
  22. service-url: # Eureka Server的地址
  23. defaultZone: http://localhost:9000/eureka/
  • 修改之后:

主机名称服务名称修改之后.png

5.5.2 显示IP信息

  • 当前问题:

显示IP信息修改之前.png

  • 修改服务提供者的application.yml文件。
  • 修改部分:
  1. # 配置 eureka
  2. eureka:
  3. instance:
  4. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  5. instance-id: service-product:9001
  6. # 显示IP信息
  7. prefer-ip-address: true
  8. client:
  9. service-url: # Eureka Server的地址
  10. defaultZone: http://localhost:9000/eureka/
  • 完整部分:
  1. server:
  2. port: 9001 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-product # 微服务的名称
  6. datasource:
  7. url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. username: root
  10. password: 123456
  11. jpa:
  12. generate-ddl: true
  13. show-sql: true
  14. open-in-view: true
  15. database: mysql
  16. # 配置 eureka
  17. eureka:
  18. instance:
  19. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  20. instance-id: service-product:9001
  21. # 显示IP信息
  22. prefer-ip-address: true
  23. client:
  24. service-url: # Eureka Server的地址
  25. defaultZone: http://localhost:9000/eureka/
  • 修改之后:

显示IP信息修改之后.png

5.5.3 微服务info内容详细信息

  • 当前问题:点击超链接报告ErrorPage

微服务info内容详细信息之前.gif

  • 修改服务提供者的pom.xml。
  • 修改部分:
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>
  • 完整部分:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>product_service</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-data-jpa</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>mysql</groupId>
  19. <artifactId>mysql-connector-java</artifactId>
  20. </dependency>
  21. <!-- 导入Eureka Client对应的坐标 -->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-actuator</artifactId>
  29. </dependency>
  30. </dependencies>
  31. <build>
  32. <plugins>
  33. <plugin>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-maven-plugin</artifactId>
  36. <version>2.2.2.RELEASE</version>
  37. <configuration>
  38. <fork>true</fork>
  39. </configuration>
  40. <executions>
  41. <execution>
  42. <goals>
  43. <goal>repackage</goal>
  44. </goals>
  45. </execution>
  46. </executions>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>
  • 在总工程添加build信息。
  • 修改部分:
  1. <build>
  2. <finalName>spring_cloud_demo</finalName>
  3. <resources>
  4. <resource>
  5. <directory>src/main/resources</directory>
  6. <filtering>true</filtering>
  7. </resource>
  8. </resources>
  9. <plugins>
  10. <plugin>
  11. <groupId>org.apache.maven.plugins</groupId>
  12. <artifactId>maven-resources-plugin</artifactId>
  13. <configuration>
  14. <delimiters>
  15. <delimit>$</delimit>
  16. </delimiters>
  17. </configuration>
  18. </plugin>
  19. </plugins>
  20. </build>
  • 完整部分:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <packaging>pom</packaging>
  7. <modules>
  8. <module>product_service</module>
  9. <module>spring_cloud_common</module>
  10. <module>order_service</module>
  11. <module>eureka_service</module>
  12. </modules>
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>2.1.6.RELEASE</version>
  17. </parent>
  18. <groupId>org.sunxiaping</groupId>
  19. <artifactId>spring_cloud_demo</artifactId>
  20. <version>1.0</version>
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. </properties>
  26. <dependencies>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-test</artifactId>
  34. <scope>test</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.projectlombok</groupId>
  38. <artifactId>lombok</artifactId>
  39. <version>1.18.4</version>
  40. <scope>provided</scope>
  41. </dependency>
  42. </dependencies>
  43. <dependencyManagement>
  44. <dependencies>
  45. <dependency>
  46. <groupId>org.springframework.cloud</groupId>
  47. <artifactId>spring-cloud-dependencies</artifactId>
  48. <version>Greenwich.RELEASE</version>
  49. <type>pom</type>
  50. <scope>import</scope>
  51. </dependency>
  52. </dependencies>
  53. </dependencyManagement>
  54. <repositories>
  55. <repository>
  56. <id>spring-snapshots</id>
  57. <name>Spring Snapshots</name>
  58. <url>http://repo.spring.io/libs-snapshot-local</url>
  59. <snapshots>
  60. <enabled>true</enabled>
  61. </snapshots>
  62. </repository>
  63. <repository>
  64. <id>spring-milestones</id>
  65. <name>Spring Milestones</name>
  66. <url>http://repo.spring.io/libs-milestone-local</url>
  67. <snapshots>
  68. <enabled>false</enabled>
  69. </snapshots>
  70. </repository>
  71. <repository>
  72. <id>spring-releases</id>
  73. <name>Spring Releases</name>
  74. <url>http://repo.spring.io/libs-release-local</url>
  75. <snapshots>
  76. <enabled>false</enabled>
  77. </snapshots>
  78. </repository>
  79. </repositories>
  80. <pluginRepositories>
  81. <pluginRepository>
  82. <id>spring-snapshots</id>
  83. <name>Spring Snapshots</name>
  84. <url>http://repo.spring.io/libs-snapshot-local</url>
  85. <snapshots>
  86. <enabled>true</enabled>
  87. </snapshots>
  88. </pluginRepository>
  89. <pluginRepository>
  90. <id>spring-milestones</id>
  91. <name>Spring Milestones</name>
  92. <url>http://repo.spring.io/libs-milestone-local</url>
  93. <snapshots>
  94. <enabled>false</enabled>
  95. </snapshots>
  96. </pluginRepository>
  97. </pluginRepositories>
  98. <build>
  99. <finalName>spring_cloud_demo</finalName>
  100. <resources>
  101. <resource>
  102. <directory>src/main/resources</directory>
  103. <filtering>true</filtering>
  104. </resource>
  105. </resources>
  106. <plugins>
  107. <plugin>
  108. <groupId>org.apache.maven.plugins</groupId>
  109. <artifactId>maven-resources-plugin</artifactId>
  110. <configuration>
  111. <delimiters>
  112. <delimit>$</delimit>
  113. </delimiters>
  114. </configuration>
  115. </plugin>
  116. </plugins>
  117. </build>
  118. </project>
  • 修改服务提供者的application.yml文件。
  • 修改部分:
  1. # 微服务info内容详细信息
  2. info:
  3. app.name: xxx
  4. company.name: xxx
  5. build.artifactId: $project.artifactId$
  6. build.version: $project.version$
  • 完整部分:
  1. server:
  2. port: 9001 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-product # 微服务的名称
  6. datasource:
  7. url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. username: root
  10. password: 123456
  11. jpa:
  12. generate-ddl: true
  13. show-sql: true
  14. open-in-view: true
  15. database: mysql
  16. # 配置 eureka
  17. eureka:
  18. instance:
  19. # 主机名称:服务名称修改,其实就是向eureka server中注册的实例id
  20. instance-id: service-product:9001
  21. # 显示IP信息
  22. prefer-ip-address: true
  23. client:
  24. service-url: # Eureka Server的地址
  25. defaultZone: http://localhost:9000/eureka/
  26. # 微服务info内容详细信息
  27. info:
  28. app.name: xxx
  29. company.name: xxx
  30. build.artifactId: $project.artifactId$
  31. build.version: $project.version$
  • 修改之后:

微服务info内容详细信息之后.gif

6 Eureka的元数据

6.1 概述

  • Eureka的元数据有两种:标准元数据和自定义元数据。
  • 服务注册Eureka基础(已过时) - 图26标注元数据:主机名、IP地址、端口号、状态页和健康检查等信息,这些信息都会发布在服务注册表中,用于服务之间的调用。
  • 服务注册Eureka基础(已过时) - 图27自定义元数据:可以使用eureka.instance.metadata-map配置,符合KEY/VALUE的存储格式。这些元数据可以在远程客户端中访问。

6.2 服务消费者通过注册中心获取服务列表,并调用

6.2.1 在订单微服务中引入Eureka Client的坐标

  • 修改部分:
  1. <!-- 导入Eureka Client对应的坐标 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  • 完整部分:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>order_service</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-actuator</artifactId>
  16. </dependency>
  17. <!-- 导入Eureka Client对应的坐标 -->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-data-jpa</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>mysql</groupId>
  28. <artifactId>mysql-connector-java</artifactId>
  29. </dependency>
  30. </dependencies>
  31. </project>

6.2.2 修改yml配置文件

  • application.yml
  1. server:
  2. port: 9002 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-order # 微服务的名称
  6. datasource:
  7. url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. username: root
  10. password: 123456
  11. jpa:
  12. generate-ddl: true
  13. show-sql: true
  14. open-in-view: true
  15. database: mysql
  16. # 配置Eureka
  17. eureka:
  18. instance:
  19. # 实例的名称
  20. instance-id: service-order:9002
  21. # 显示IP信息
  22. prefer-ip-address: true
  23. client:
  24. service-url: # Eureka Server的地址
  25. defaultZone: http://localhost:9000/eureka/
  26. # 微服务info内容详细信息
  27. info:
  28. app.name: xxx
  29. company.name: xxx
  30. build.artifactId: $project.artifactId$
  31. build.version: $project.version$

6.2.3 修改Controller

  • OrderController.java
  1. package com.sunxiaping.order.controller;
  2. import com.sunxiaping.order.domain.Product;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.cloud.client.ServiceInstance;
  5. import org.springframework.cloud.client.discovery.DiscoveryClient;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.client.RestTemplate;
  11. import java.net.URI;
  12. import java.util.List;
  13. @RestController
  14. @RequestMapping(value = "/order")
  15. public class OrderController {
  16. @Autowired
  17. private RestTemplate restTemplate;
  18. /**
  19. * SpringCloud提供的获取元数据的工具类
  20. * 调用方法获取服务的元数据
  21. */
  22. @Autowired
  23. private DiscoveryClient discoveryClient;
  24. /**
  25. * 通过订单系统,调用商品微服务根据id查询商品信息
  26. *
  27. * @param id
  28. * @return
  29. */
  30. @GetMapping(value = "/buy/{id}")
  31. public Product buy(@PathVariable(value = "id") Long id) {
  32. List<ServiceInstance> instanceList = discoveryClient.getInstances("service-product");
  33. ServiceInstance serviceInstance = instanceList.get(0);
  34. URI uri = serviceInstance.getUri();
  35. if (null != uri) {
  36. return restTemplate.getForObject(uri.toString() + "/product/findById/" + id, Product.class);
  37. }
  38. return null;
  39. }
  40. }

7 Eureka中的自我保护

7.1 概述

  • 微服务第一次注册成功后,每30秒会发送一次心跳将服务的实例信息注册到注册中心。通知Eureka Server该实例依然存在。如果超过90秒没有发送心跳,则服务器将从注册中心将此服务移除。
  • Eureka Server在运行期间,会统计心跳失败的比例在15分钟内是否低于85%,如果出现低于85%的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),那么Eureka就会认为客户端和注册中心出现了网络故障,此时会做如下的处理:
    • 服务注册Eureka基础(已过时) - 图28Eureka不再从注册列表中删除因为长时间没有收到心跳而应该过期的服务。
    • 服务注册Eureka基础(已过时) - 图29Eureka仍然能够接受新服务的注册和查询请求,但是不会同步到其他节点上(保证当前节点依然可用)。
    • 服务注册Eureka基础(已过时) - 图30当网络稳定的时候,当前实例新的注册信息会被同步到其他节点中。
  • 验证自我保护机制开启,并不会马上呈现在web后台上,而是默认需要等待5分钟(可以通过eureka.server.wait-time-in-ms-when-sync-empty配置),即5分钟后你就会看到如下所示的提示信息:

Eureka的自我保护机制.png

7.2 如何关闭自我保护机制

  • 通过设置eureka.enableSelfPreservation=false来关闭自我保护机制。