From 89c14ddeebc2e777855df5d66d21fa35df5b757f Mon Sep 17 00:00:00 2001
From: bunny <1319900154@qq.com>
Date: Tue, 20 May 2025 21:38:52 +0800
Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=8F=91=E7=8E=B0=E6=9C=8D?=
=?UTF-8?q?=E5=8A=A1=E4=BF=A1=E6=81=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
cloud-demo/ReadMe.md | 28 +++++++++++++
cloud-demo/pom.xml | 7 +---
cloud-demo/services/pom.xml | 5 +++
.../service/OrderServiceApplication.java | 2 +
.../java/cn/bunny/service/DiscoveryTest.java | 42 +++++++++++++++++++
.../service/ProductServiceApplication.java | 2 +
6 files changed, 80 insertions(+), 6 deletions(-)
create mode 100644 cloud-demo/services/service-order/src/test/java/cn/bunny/service/DiscoveryTest.java
diff --git a/cloud-demo/ReadMe.md b/cloud-demo/ReadMe.md
index 40c529f..b4c39c8 100644
--- a/cloud-demo/ReadMe.md
+++ b/cloud-demo/ReadMe.md
@@ -237,3 +237,31 @@ networks: # 定义网络
driver: bridge # 使用 bridge 驱动(默认)
```
+## 注册中心
+
+### 服务发现
+
+发现服务信息。
+
+```java
+for (String service : discoveryClient.getServices()) {
+ System.out.println(service);
+
+ for (ServiceInstance instance : discoveryClient.getInstances(service)) {
+ System.out.println("IP地址:" + instance.getHost());
+ System.out.println("端口号" + instance.getPort());
+ }
+}
+
+System.out.println("----------------------------------------------");
+
+// 两个方式一样,DiscoveryClient 是 Spring自带的 NacosDiscoveryClient是 Nacos
+for (String service : nacosDiscoveryClient.getServices()) {
+ System.out.println(service);
+
+ for (ServiceInstance instance : nacosDiscoveryClient.getInstances(service)) {
+ System.out.println("IP地址:" + instance.getHost());
+ System.out.println("端口号" + instance.getPort());
+ }
+}
+```
diff --git a/cloud-demo/pom.xml b/cloud-demo/pom.xml
index 37d08ff..f205c17 100644
--- a/cloud-demo/pom.xml
+++ b/cloud-demo/pom.xml
@@ -48,12 +48,7 @@
${junit.version}
test
-
- org.springframework.boot
- spring-boot-starter-test
- test
-
-
+
com.alibaba.cloud
diff --git a/cloud-demo/services/pom.xml b/cloud-demo/services/pom.xml
index da83074..2523ad0 100644
--- a/cloud-demo/services/pom.xml
+++ b/cloud-demo/services/pom.xml
@@ -37,6 +37,11 @@
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
junit
junit
diff --git a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java
index 5022a12..19e7969 100644
--- a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java
+++ b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java
@@ -2,8 +2,10 @@ package cn.bunny.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
+@EnableDiscoveryClient
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
diff --git a/cloud-demo/services/service-order/src/test/java/cn/bunny/service/DiscoveryTest.java b/cloud-demo/services/service-order/src/test/java/cn/bunny/service/DiscoveryTest.java
new file mode 100644
index 0000000..3c5d22a
--- /dev/null
+++ b/cloud-demo/services/service-order/src/test/java/cn/bunny/service/DiscoveryTest.java
@@ -0,0 +1,42 @@
+package cn.bunny.service;
+
+import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.cloud.client.ServiceInstance;
+import org.springframework.cloud.client.discovery.DiscoveryClient;
+
+@SpringBootTest()
+public class DiscoveryTest {
+
+ @Autowired
+ private DiscoveryClient discoveryClient;
+
+ @Autowired
+ private NacosDiscoveryClient nacosDiscoveryClient;
+
+ @Test
+ void discoveryClientTest() {
+ for (String service : discoveryClient.getServices()) {
+ System.out.println(service);
+
+ for (ServiceInstance instance : discoveryClient.getInstances(service)) {
+ System.out.println("IP地址:" + instance.getHost());
+ System.out.println("端口号" + instance.getPort());
+ }
+ }
+
+ System.out.println("----------------------------------------------");
+
+ // 两个方式一样,DiscoveryClient 是 Spring自带的 NacosDiscoveryClient是 Nacos
+ for (String service : nacosDiscoveryClient.getServices()) {
+ System.out.println(service);
+
+ for (ServiceInstance instance : nacosDiscoveryClient.getInstances(service)) {
+ System.out.println("IP地址:" + instance.getHost());
+ System.out.println("端口号" + instance.getPort());
+ }
+ }
+ }
+}
diff --git a/cloud-demo/services/service-product/src/main/java/cn/bunny/service/ProductServiceApplication.java b/cloud-demo/services/service-product/src/main/java/cn/bunny/service/ProductServiceApplication.java
index 689905a..ef092ea 100644
--- a/cloud-demo/services/service-product/src/main/java/cn/bunny/service/ProductServiceApplication.java
+++ b/cloud-demo/services/service-product/src/main/java/cn/bunny/service/ProductServiceApplication.java
@@ -2,8 +2,10 @@ package cn.bunny.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
+@EnableDiscoveryClient
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);