本文共 2404 字,大约阅读时间需要 8 分钟。
这篇文章为大家搭建一个eureka项目,主要展示其服务发现的功能:
首先添加pom依赖,如果使用idea创建maven项目时可以直接选择相关依赖,这里使用eclipse手把手搭建:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.1.RELEASE com.zsy.cloud cloud-eureka 0.0.1-SNAPSHOT jar zsy-eureka 1.8 Greenwich.M3 org.springframework.cloud spring-cloud-starter-netflix-eureka-server 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: 8081 #服务端口###服务名称(服务注册到eureka名称,如果需要注册自己,必须声明服务名称)spring: application: name: eureka-servereureka: instance: ###注册到eurekaip地址 hostname: 127.0.0.1 client: serviceUrl: # 注册中心服务接口,别的服务需要注册时使用这个接口即可(最好一个斜杠后面的服务名称必须是eureka) defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka ###因为自己是注册中心,不需要自己注册自己 register-with-eureka: false ###因为自己是注册中心,不需要检索服务 fetch-registry: false server: # 测试时关闭自我保护机制,保证不可用服务及时踢出 enable-self-preservation: false eviction-interval-timer-in-ms: 2000
启动类:
package com.zsy.cloud.eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApp { public static void main(String[] args) { SpringApplication.run(EurekaServerApp.class, args); }}
eureka管理页面:
页面显示没有服务注册,下一章写个client进行注册。
转载地址:http://tpbai.baihongyu.com/