1. Eureka基础

1.1 配置服务端 Eureka Server

1.1.1 构建项目

使用maven构建

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.2.3.RELEASE</version>
  5. <relativePath/>
  6. </parent>
  7. <dependencies>
  8. <!-- 有的教程中还引入spring-boot-starter-web,其实不用。因为eureka-server的依赖已经包含了它。在pom中点此依赖进去,一共点4次spring-cloud-netflix-eureka-server,发现web的依赖。 -->
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  12. </dependency>
  13. </dependencies>
  14. <dependencyManagement>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-dependencies</artifactId>
  19. <version>Hoxton.SR1</version>
  20. <type>pom</type>
  21. <scope>import</scope>
  22. </dependency>
  23. </dependencies>
  24. </dependencyManagement>

使用gradle构建

  1. plugins {
  2. id 'org.springframework.boot' version '2.3.4.RELEASE'
  3. id 'io.spring.dependency-management' version '1.0.10.RELEASE'
  4. id 'java'
  5. }
  6. group = 'com.springcloud'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '1.8'
  9. repositories {
  10. mavenCentral()
  11. }
  12. ext {
  13. springCloudVersion = "Hoxton.SR8"
  14. }
  15. dependencies {
  16. // 有的教程中还引入spring-boot-starter-web,其实不用。因为eureka-server的依赖已经包含了它。
  17. implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
  18. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  19. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  20. }
  21. }
  22. dependencyManagement {
  23. imports {
  24. mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  25. }
  26. }
  27. test {
  28. useJUnitPlatform()
  29. }

1.1.2 application.properties

  1. server.port=8080
  2. #eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
  3. eureka.server.eviction-interval-timer-in-ms=1000
  4. #是否将自己注册到Eureka Server,默认为true,由于当前就是server,故而设置成false,表明该服务不会向eureka注册自己的信息
  5. eureka.client.register-with-eureka=false
  6. #表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
  7. eureka.client.fetch-registry=true
  8. #eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
  9. eureka.instance.lease-expiration-duration-in-seconds=60
  10. #是否开启自我保护模式,默认为true。
  11. #默认情况下,如果Eureka Server在一定时间内没有接收到某个微服务实例的心跳,Eureka Server将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与Eureka Server之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。
  12. #Eureka通过“自我保护模式”来解决这个问题——当Eureka Server节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,Eureka Server就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。
  13. #综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。
  14. eureka.server.enable-self-preservation=true
  15. #Eureka Server的地址
  16. eureka.client.service-url.defaultZone=http://127.0.0.1:${server.port}/eureka/

1.1.3 启动类配置

启动类加上@EnableEurekaServer注解

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

1.2 配置客户端Eureka Client

1.2.1 构建项目

使用maven构建

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.2.3.RELEASE</version>
  5. <relativePath/>
  6. </parent>
  7. <dependencies>
  8. <!-- eureka客户端必须要有spring-boot-starter-web这个依赖. 不然注册不进去 -->
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  16. </dependency>
  17. </dependencies>
  18. <dependencyManagement>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-dependencies</artifactId>
  23. <version>Hoxton.SR1</version>
  24. <type>pom</type>
  25. <scope>import</scope>
  26. </dependency>
  27. </dependencies>
  28. </dependencyManagement>

使用gradle构建

  1. plugins {
  2. id 'org.springframework.boot' version '2.3.4.RELEASE'
  3. id 'io.spring.dependency-management' version '1.0.10.RELEASE'
  4. id 'java'
  5. }
  6. group = 'com.springcloud'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '1.8'
  9. repositories {
  10. mavenCentral()
  11. }
  12. ext {
  13. set('springCloudVersion', "Hoxton.SR8")
  14. }
  15. dependencies {
  16. implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
  17. //eureka客户端必须要有spring-boot-starter-web这个依赖. 不然注册不进去
  18. implementation 'org.springframework.boot:spring-boot-starter-web'
  19. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  20. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  21. }
  22. }
  23. dependencyManagement {
  24. imports {
  25. mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  26. }
  27. }
  28. test {
  29. useJUnitPlatform()
  30. }

1.2.2 application.properties

  1. spring.application.name=USER-CENTER
  2. #表示eureka client发送心跳给server端的频率
  3. #leaseRenewalIntervalInSeconds,表示eureka client发送心跳给server端的频率。
  4. #如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance。除此之外,如果该instance实现了HealthCheckCallback,并决定让自己unavailable的话,则该instance也不会接收到流量。
  5. eureka.instance.lease-renewal-interval-in-seconds=1
  6. #表示eureka client间隔多久去拉取服务注册信息,默认为30秒,对于api-gateway,如果要迅速获取服务注册状态,可以缩小该值,比如5秒
  7. eureka.client.registry-fetch-interval-seconds=1
  8. #Server的地址
  9. eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8080/eureka/

1.2.3 启动类配置.

启动类加上@EnableEurekaClient注解

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

1.2.4 查看服务注册后的控制台

微信图片_20200920133752.png

1.2.5 按照同样的方式再配置一个ORDER-CENTER

1.2.6 按照同样的方式再配置一个PRODUCT-CENTER

2. Eureka进阶

2.1 Eureka的自我保护机制

2.2 Eureka集群同步原理