SpringCloud之搭建Eureka注册中心
1、版本说明
springboot:2.5.5
springcloud:2020.0.4
其他版本对应参考Spring Cloud官网:
2、搭建Eureka服务端
2.1添加Maven依赖
在pom.xml中添加以下依赖:
<!-- 引入的Eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 自动匹配当前springboot与Eureka的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.2配置application.yml文件
server:
port: 7887 #服务注册中心端口号
eureka:
instance:
hostname: 127.0.0.1 #服务注册中心IP地址
client:
registerWithEureka: false #是否向服务注册中心注册自己
fetchRegistry: false #是否检索服务
serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
![]()
2.3启动类引入@EnableEurekaServer
在启动类上添加注解@EnableEurekaServer标明自己是一个注册中心
![]()
2.4启动服务访问http://127.0.0.1:7887/

如上图所示,说明注册中心已经搭建成功,此时还没有服务注册进来。
3、搭建Eureka客户端
3.1添加Maven依赖
在pom.xml中添加以下依赖:
<!-- 引入的Eureka-client-->
<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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 自动匹配当前springboot与Eureka的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.2配置application.yml文件
server:
port: 8989
eureka:
instance:
lease-renewal-interval-in-seconds: 5 #心跳时间,即服务续约间隔时间(默认为30s)
lease-expiration-duration-in-seconds: 15 #发呆时间,即服务续约到期时间(默认为90s)
client:
registry-fetch-interval-seconds: 10 #拉取服务注册信息间隔(默认为30s)
service-url:
defaultZone: http://127.0.0.1:7887/eureka/
healthcheck:
enabled: true #开启健康检查(依赖spring-boot-starter-actuator)

3.3启动类引入@EnableEurekaClient
在启动类上添加注解@EnableEurekaClient标明自己是Eureka客户端-消费者
3.4启动服务访问http://127.0.0.1:7887/
此时服务已经注册进来了,但是Application显示UNKNOWN,说明没有配置服务应用名称。
在application.yml文件中添加配置:
spring:
application:
name: study1
再次重启服务刷新页面,此时服务成功注册进来了。
注:
1、客户端与服务端版本需要保持一致,否则服务无法注册
2、D版本及一下的springcloud,Eureka依赖引入方式为:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>







浙公网安备 33010602011771号