1、简介

https://github.com/Netflix/eureka/wiki

Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务注册和发现功能。 Eureka包含两个组件:Eureka Server和Eureka Client。

2、server 相关配置

2.1 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"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.5.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.morrow</groupId>
  12. <artifactId>springcloud-eureka-6101</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springcloud-eureka-6101</name>
  15. <description>springcloud-eureka-6101 project for Spring Boot</description>
  16. <properties>
  17. <java.version>11</java.version>
  18. <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <!--引入 eureka server-->
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  29. </dependency>
  30. </dependencies>
  31. <!--全局管理springcloud版本,并不会引入具体依赖-->
  32. <dependencyManagement>
  33. <dependencies>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-dependencies</artifactId>
  37. <version>${spring-cloud.version}</version>
  38. <type>pom</type>
  39. <scope>import</scope>
  40. </dependency>
  41. </dependencies>
  42. </dependencyManagement>
  43. <build>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-maven-plugin</artifactId>
  48. </plugin>
  49. </plugins>
  50. </build>
  51. </project>

2.2 yaml文件配置

  1. server:
  2. port: 6101
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone: http://localhost:6101/eureka #指定服务注册中心的地址
  7. fetch-registry: false #关闭作为客户端时从eureka server获取服务信息
  8. register-with-eureka: false #不再将自己同时作为客户端进行注册
  9. spring:
  10. application:
  11. name: eureka-server

2.3 开启@EnableEurekaServer注解

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

2.4 访问地址

http://localhost:6101/

3、client 相关配置

3.1 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"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.5.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.morrow</groupId>
  12. <artifactId>springcloud-eureka-client-6102</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springcloud-eureka-client-6102</name>
  15. <description>springcloud-eureka-client-6102 project for Spring Boot</description>
  16. <properties>
  17. <java.version>11</java.version>
  18. <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <!--引入 eureka server-->
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  34. </dependency>
  35. </dependencies>
  36. <!--全局管理springcloud版本,并不会引入具体依赖-->
  37. <dependencyManagement>
  38. <dependencies>
  39. <dependency>
  40. <groupId>org.springframework.cloud</groupId>
  41. <artifactId>spring-cloud-dependencies</artifactId>
  42. <version>${spring-cloud.version}</version>
  43. <type>pom</type>
  44. <scope>import</scope>
  45. </dependency>
  46. </dependencies>
  47. </dependencyManagement>
  48. <build>
  49. <plugins>
  50. <plugin>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-maven-plugin</artifactId>
  53. </plugin>
  54. </plugins>
  55. </build>
  56. </project>

3.2 yaml文件配置

  1. server:
  2. port: 6102
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone: http://localhost:6101/eureka #指定服务注册中心的地址
  7. spring:
  8. application:
  9. name: eureka-client

3.3 开启@EnableEurekaServer注解

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

image.png

自我保护机制

  • 官网地址: https://github.com/Netflix/eureka/wiki/Server-Self-Preservation-Mode
  • 默认情况下,如果Eureka Server在一定时间内(默认90秒)没有接收到某个微服务实例的心跳,Eureka Server将会移除该实例。但是当网络分区故障发生时,微服务与Eureka Server之间无法正常通信,而微服务本身是正常运行的,此时不应该移除这个微服务,所以引入了自我保护机制。Eureka Server在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期。这种设计的哲学原理就是”宁可信其有不可信其无!”。自我保护模式正是一种针对网络异常波动的安全保护措施,使用自我保护模式能使Eureka集群更加的健壮、稳定的运行。

官方并不建议在生产情况下关闭

4、Eureka 停止更新

image.png

5、示例代码

springcloud-eureka-server-6101 springcloud-eureka-client-6102