前言:上一章节我们介绍了如何将服务提供者注册进Eureka服务里,本章节则介绍如何将服务消费者Consumer注册进Eureka服务里
Eureka架构原理图
1、修改consumer-order80子模块的pom.xml文件,引入Eureka Clinet的依赖,然后reolad一下,下载依赖
例:
<?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>springcloud01</artifactId>
<groupId>com.ken.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer-order80</artifactId>
<dependencies>
<!--以下依赖都没写版本号,没写版本号的情况下会引用父项目的版本-->
<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>
<!--lombok插件-->
<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>
<!--引入自己定义的api调用包,可以使用Payment模块的entity-->
<dependency>
<groupId>com.ken.springcloud</groupId>
<artifactId>api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--Eureka Clinet-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2、修改consumer-order80子模块的application.yml文件配置的内容,将Eureka相关配置引入
server:
port: 80 #用80端口是因为浏览器网页服务默认的端口号都是80(80端口是为HTTP开放的)
spring:
application:
name: cloud-order-service
eureka:
client:
#表示是否将自己注册进Eureka Server里,默认为true
register-with-eureka: true
#是否从Eureka Server抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3、修改consumer-order80子模块的主启动类,将consumer-order80子模块标注为Eureka Client
package com.ken.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//标注为Eureka Client(服务注册中心)
@EnableEurekaClient
public class OrderMain {
public static void main(String[] args) {
SpringApplication.run(OrderMain.class,args);
}
}
效果图:
4、启动consumer-order80子模块的主启动类,测试consumer-order80子模块是否成功注册进Eureka
注意:要显启动Eureka Server,然后再启动服务提供者provider-payment8001子模块,最后才启动consumer-order80子模块
效果图:
若看到Instances currently registered with Eureka下面多出一行名为cloud-order-service的服务的相关信息,则证明consumer-order80子模块成功注册进了Eureka
注:我们在application.yml配置文件里的写的服务名称就是注册在Eureka Server里的名称