本文共 2691 字,大约阅读时间需要 8 分钟。
上一章搭建了eureka注册中心,这章就写搭建一个client服务注册到注册中心上去。
pom文件:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.1.RELEASE com.zsy.cloud eureka-client 1.0.0 jar eureka-client 1.8 Greenwich.M3 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
yml文件:
###服务启动端口号server: port: 8082###服务名称(服务注册到eureka名称)spring: application: name: zsy-app###服务注册到eureka地址eureka: client: service-url: defaultZone: http://localhost:8081/eureka ###册自己到注册中心 register-with-eureka: true ###是否需要从eureka上获取注册信息 fetch-registry: true # 心跳检测检测与续约时间 # 测试时将值设置设置小些,保证服务关闭后注册中心能及时踢出服务 instance: ###Eureka客户端向服务端发送心跳的时间间隔,单位为秒(客户端告诉服务端自己会按照该规则) lease-renewal-interval-in-seconds: 1 ####Eureka服务端在收到最后一次心跳之后等待的时间上限,单位为秒,超过则剔除(客户端告诉服务端按照此规则等待自己) lease-expiration-duration-in-seconds: 2
启动类:
package com.zsy.cloud.eureka_client;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); }}
加入@EnableEurekaClient注解使其成为eureka的client
再次访问eureka的管理后台:
发现client已成功注册进来。
转载地址:http://gpbai.baihongyu.com/