Spring Cloud之注册中心搭建

时间:2024-01-17 19:19:02

一、注册中心服务端搭建

1)引入相关Maven坐标

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2)加入相关注解

@EnableEurekaServer

3)相关配置属性,application.yml

server:
port: 8781 spring:
application:
name: eureka-server eureka:
instance:
# 开启除了主机名也可以通过ip来定义注册中心地址
prefer-ip-address: true
# 设置主机名,不配置的时候将根据操作系统的主机名来获取
hostname: localhost
# 定义服务续约任务的调用间隔时间,默认为30秒
lease-renewal-interval-in-seconds: 30
# 定义服务失效的时间,默认为90秒
lease-expiration-duration-in-seconds: 90
client:
# 1.单节点下可配置如下两个参数
## 不需要去检测其他服务
# fetch-registry: false
## 设置为false,代表不向注册中心注册自己
# register-with-eureka: false
# 2.配置高可用配置中心
serviceUrl:
defaultZone: http://localhost:8781/eureka,http://localhost:8782/eureka
server:
# wait-time-in-ms-when-sync-empty: 0
# 关闭保护机制,以确保注册中心可以将不可用的实例正确剔除
enable-self-preservation: false
#logging:
# level:
# root: info
# file: /data/home/logs/zbq-eureka/zbq-eureka.log
#logging:
logging:
level:
root: info
additivity: off
file: /data/home/logs/zbq-eureka/zbq-eureka.log

二、注册中心客户端搭建

1)引入相关Maven坐标

 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2)加入相关注解

@EnableDiscoveryClient

 

3)相关配置属性

##在配置文件中注明自己的服务注册中心的地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8781/eureka,http://localhost:8782/eureka spring:
# 需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
application.name: zbq-backend