Eureka:
1: Eureka提供了一系列REST的API,供Eureka Client来调用,实现服务注册,注销,心跳,状态更新等等操作
2: Eureka属于客户端发现, 如果希望用服务端发现,则用nginx.
服务端配置:
1: 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2: 配置application.yml
spring:
application:
name: eureka #定义服务器名字
server:
port: 8761 #为了不占用应用的8080, 修改为Eureka的默认端口
eureka:
client: #自己注册为自己的客户端
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: false
server:
enable-self-preservation: false #开发环境可以关掉, 生产环境不要关闭
3: 在项目的application.java中启用EurekaServer
@EnableEurekaServer
说明:
1: 参考文档:https://yq.aliyun.com/articles/138261
2: register-with-eureka: 是否把自己显示在Eureka上, 作为客户端
enable-self-preservation: 关闭自我保护模式, 保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务).
开发环境可以关掉, 生产环境不要关闭
客户端配置:
1: 添加依赖
<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>
2: 配置application.yml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3: 在项目的application.java中启用Client
@EnableEurekaClient
实现Eureka高可用性:
通过互相注册
1: 在Eureka服务端, 增加N个(例如eureka1, eureka2, eureka3),
2: 在eureka1的service-url的defaultZone: http://localhost:8761/eureka2/,http://localhost:8761/eureka3/
3: 在eureka客户端中, 把所有的服务注册中心都填进去
如何在注册中心关闭eureka
1: pom中增加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2: yml中增加
management:
endpoints:
web:
exposure:
include: info, health, shutdown
或者
management:
endpoint:
shutdown:
enabled:
3: 执行post命令
curl -X POST localhost:8080/actuator/shutdown
网友评论