文
章
目
录
章
目
录
基于之前的项目基础,我们现在需要在mscloud父工程下构建单机版的Eureka,下面我们已经遵循前面的6个步骤,实现搭建Eureka Server端和Eureka Client端服务注册中心的过程。
第1步:创建Module
在mscloud下新建名为cloud-eureka-server7001的子模块。
第2步:修改pom
修改该子模块的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mscloud</artifactId>
<groupId>com.panziye.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server7001</artifactId>
<dependencies>
<!-- eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引入自定义的api通用包,可以使用支付Payment等Entity -->
<dependency>
<groupId>com.panziye.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
注意:这里面的第一个依赖就是eureka服务端的依赖
第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);
}
}
注意:这里使用了@EnableEurekaServer 注解
到这里,Eureka Server端就创建结束了。
通过前面Eureka基础知识的学习,我们知道前面的两个子模块(订单80和支付8001)都是Eureka Client端,我们需要对其进行改造。
1、cloud-provider-payment8001模块改造
1)修改pom
修改该子模块的pom.xml,直接添加如下依赖即可:
<!-- eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
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);
}
}
注意:这里使用了@EnableEurekaClient注解,表明是Eureka客户端
4)测试
a)先启动Eureka Server,即我们这里的cloud-eureka-server7001
b)浏览器访问localhost:7001,结果如下:

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

注意:注册进来的Application名称与该服务对应得模块中application.yml中的spring.application.name配置的值一致
2、cloud-consumer-order80模块改造
1)修改pom
修改该子模块的pom.xml,直接添加如下依赖即可:
<!-- eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
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);
}
}
4)测试
1)启动EurekaServer、支付8001和订单80模块,会发现两个子模块都注册进来了

2)测试查询get






