基于之前的项目基础,我们现在需要在mscloud父工程下构建单机版的Eureka,下面我们已经遵循前面的6个步骤,实现搭建Eureka Server端和Eureka Client端服务注册中心的过程。
第1步:创建Module
在mscloud下新建名为cloud-eureka-server7001的子模块。
第2步:修改pom
修改该子模块的pom.xml如下:

程序员导航
优网导航旗下整合全网优质开发资源,一站式IT编程学习与工具大全网站
mscloud com.panziye.springcloud 1.0-SNAPSHOT 4.0.0 cloud-eureka-server7001 org.springframework.cloud spring-cloud-starter-netflix-eureka-server com.panziye.springcloud cloud-api-commons ${project.version} org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
[v_blue]注意:这里面的第一个依赖就是eureka服务端的依赖[/v_blue]
第3步:写yml
我们在该子模块的resources下新建application.yml,配置如下:
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端实例名称
client:
#false表示不像注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置eureka server交互的地址查询服务和注册都需要依赖此地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
第4步:主启动类
在com.panziye.springcloud包新建名为EurekaMain7001的主启动类如下:
package com.panziye.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer //表明是服务端 public class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class,args); } }
[v_blue]注意:这里使用了@EnableEurekaServer 注解[/v_blue]
到这里,Eureka Server端就创建结束了。

AI 工具导航
优网导航旗下AI工具导航,精选全球千款优质 AI 工具集
通过前面Eureka基础知识的学习,我们知道前面的两个子模块(订单80和支付8001)都是Eureka Client端,我们需要对其进行改造。
1、cloud-provider-payment8001模块改造
1)修改pom
修改该子模块的pom.xml,直接添加如下依赖即可:
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
2)写yml
在application.yml新增Eureka相关配置,如下:
eureka:
client:
#表示是否将自己注册进eureka服务中心,默认true
register-with-eureka: true
#表示是否从EurekaServer抓取已有注册信息,默认true。单节点无所谓,集群必须设置true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3)主启动类
在主启动会类上新增Eureka相关注解,结果如下:
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
[v_blue]注意:这里使用了@EnableEurekaClient注解,表明是Eureka客户端[/v_blue]
4)测试
a)先启动Eureka Server,即我们这里的cloud-eureka-server7001
b)浏览器访问localhost:7001,结果如下:

c)再启动cloud-provider-payment8001模块,然后刷新浏览器如下:

[v_blue]注意:注册进来的Application名称与该服务对应得模块中application.yml中的spring.application.name配置的值一致[/v_blue]

免费在线工具导航
优网导航旗下整合全网优质免费、免注册的在线工具导航大全
2、cloud-consumer-order80模块改造
1)修改pom
修改该子模块的pom.xml,直接添加如下依赖即可:
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
2)写yml
在application.yml新增Eureka相关配置,如下:
eureka:
client:
#表示是否将自己注册进eureka服务中心,默认true
register-with-eureka: true
#表示是否从EurekaServer抓取已有注册信息,默认true。单节点无所谓,集群必须设置true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3)主启动类
在主启动会类上新增Eureka相关注解,结果如下:
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}





