前提:
服務至少要部署兩台機上,當其中一台下線后,由另一台提供對外服務
1,依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
application.yml 配置
management.endpoint.shutdown.enabled=true management.endpoint.health.show-details=always management.endpoints.web.exposure.include=*
#微服務上線請求
curl -H "Content-Type:application/vnd.spring-boot.actuator.v2+json;charset=UTF-8" -X POST http://localhost:2{{ port }}/actuator/service-registry?status=UP
#微服務下線請求
curl -H "Content-Type:application/vnd.spring-boot.actuator.v2+json;charset=UTF-8" -X POST http://localhost:2890/actuator/service-registry?status=DOWN
#eureka 小知識
>eureka 清理過期服務的時間間隔是0s,即不清理實例
eureka.server.eviction-interval-timer-in-ms
>eureka 客戶端配置
eureka 服務端如果發現客戶端在這個時間段內沒有收到心跳,會將客戶端下線
默認值90s
eureka.instance.lease-expiration-duration-in-seconds
>客戶端通過eureka.instance.lease-renewal-interval-in-seconds
每隔這個時間會主動心跳一次,默認值為30s,更新自己的狀態。