一、配置Eureka服务端
导包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
配置 application.yml 内容
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
# 是否注册到Eureka服务 默认是true
registerWithEureka: false
# 是否获取注册列表 默认是true
fetchRegistry: false
# 设置Eureka服务的网络路径
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
再启动类加注释开启并申明是一个 eureka 服务
package com.wt.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
// 开启并申明是一个 eureka 服务
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
二、配置客户端
导包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
配置 application.yml 内容
server:
port: 8770
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: product-service
三、关闭Eureka自我保护机制
Eureka 服务端application.yml 修改内容
eureka:
server:
# 检查不活跃的节点 并剔除掉
enable-self-preservation: false
# 每5秒清理一次失效的服务
eviction-interval-timer-in-ms: 5000
Eureka 客户端application.yml 修改内容
eureka:
instance:
# 告诉服务端,如果我20s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
lease-expiration-duration-in-seconds: 10
# 每间隔10s,向服务端发送一次心跳,证明自己依然”存活“
lease-renewal-interval-in-seconds: 10
网友评论