ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
下面将 cloud-provider-payment8001 微服务注册到Eureka服务注册中心 cloud-eureka-server7001。 <br/> 步骤如下: **1. 构建模块:cloud-provider-payment8001** **2. 在 8001 模块的`pom.xml`中添加 eureka-client 与 actuator 依赖** ```xml <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ... </dependencies> ``` **3. 在 8001 模块的`resources/application.yml`中配置 eureka 相关** ```yml server: port: 8001 spring: application: name: cloud-payment-service eureka: client: service-url: #eureka提供的注册中心地址 defaultZone: http://www.eureka7001.com:7001/eureka/,http://www.eureka7002.com:7002/eureka/ instance: #显示当前模块的名称 instance-id: ${project.artifactId} #true:显示当前模块的ip地址 prefer-ip-address: true lease-renewal-interval-in-seconds: 1 lease-expiration-duration-in-seconds: 2 ####actuator#### info: app.name: ${spring.application.name} company.name: www.atguigu.com build.artifactId: ${project.artifactId} build.version: ${project.version} ``` **4. 在 8001 模块的启动类上添加注解`@EnableEurekaClient`** ```java @SpringBootApplication @EnableEurekaClient public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } } ``` **5. 验证** (1)先启动 7001 Eureka服务注册中心,再启动 8001 模块。 (2)访问 7001 Eureka服务注册中心 http://www.eureka7001.com:7001/ ,显示如下图,可以看到已经有一个微服务注册进来了。 ![](https://img.kancloud.cn/76/75/767558dba4f06f43224c5cf9805d0995_1608x177.jpg) 上图中的Application名字为 8001 模块中定义的名字,该名字非常重要,是该服务端向外提供服务时所使用的名字。 <br/> :-: ![](https://img.kancloud.cn/78/fb/78fb238520c49b4f13020c5441650b2f_1473x367.jpg) 显示当前模块的ip地址 <br/> ![](https://img.kancloud.cn/e5/db/e5dbee4cdabe2cb4c167c52b3f9a7984_1681x162.jpg) 点击Status的微服务,便可以看到actuator的配置。 ```json { "app":{ "name":"cloud-payment-service" }, "company":{ "name":"www.atguigu.com" }, "build":{ "artifactId":"cloud-provider-payment8001", "version":"1.0-SNAPSHOT" } } ```