本文章简单介绍如何实现服务注册与发现
1.eureka server
添加所需依赖<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
2.配置server
# 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己 eureka.client.fetch-registry=false # 由于注册中心的职责就是维护服务实例,他并不需要去检索服务,所以也设置为false eureka.client.register-with-eureka=false eureka.client.service-url.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/ eureka.instance.hostname=localhost #配置属性,但由于 Eureka 自我保护模式以及心跳周期长的原因,经常会遇到 Eureka Server 不剔除已关停的节点的问题 # 关闭自我保护 eureka.server.enable-self-preservation=false #清理间隔(单位毫秒,默认是60*1000) eureka.server.eviction-interval-timer-in-ms=5000
3.集成security
#启用身份认证 security.basic.enabled=true #自定义用户名,密码 security.user.name=snjx security.user.password=snjx
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency>
4.添加注解
5.eureka client使用
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
6.访问地址
http://localhost:8761/eureka/