Spring Cloud版本管理及eureka

Spring Cloud学习笔记

依赖管理

spring cloud引入依赖管理进行spring cloud及相关组件依赖管理
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>2020.0.4</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

服务注册发现组件

eureka目前2.0版本已经停止维护,自我保护机制为15分钟内有15%及以上心跳正常则心跳检测不过时不会移除实例。(防止网络波动问题)
  • 服务端需要引入以下依赖
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency>
  • 在启动类上增加@EnableEurekaServer注解
    image

  • 示例配置文件如下

server:
  port: 31002
spring:
  application:
    name: illriver-bbs-eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:31002/eureka/ #eureka服务默认分区地址
    fetch-registry: false #启动立即注册 默认true
    register-with-eureka: false # 是否注册到eureka 默认true
  server:
    enable-self-preservation: true #是否开启自我保护机制 默认true
    eviction-interval-timer-in-ms: 0 #移除自我保护机制之后心跳注册失败之后多少秒被注册中心剔除 默认0s
  instance:
    lease-renewal-interval-in-seconds: 30 #客户端多久向eureka发送一次心跳 默认30s
    lease-expiration-duration-in-seconds: 90 #eureka默认接受心跳的最大时间 默认90s
  • 服务发现端需要引入以下依赖
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  • 在启动类上增加@EnableEurekaClient注解
    image

  • 示例配置文件如下

server:
 port: 31001
spring:
 application:
  name: illriver-bbs-board
eureka:
 client:
  service-url:
   defaultZone: http://localhost:31002/eureka/ #eureka服务默认分区地址
consul本地启动无需启动类上配置,需要增加以下依赖
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  </dependency>
 <!--consul安全检查必要依赖-->
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
posted @ 2021-10-30 00:11  illriver  阅读(708)  评论(0)    收藏  举报