✨ 发现服务信息
This commit is contained in:
parent
7d4cd7ebcb
commit
89c14ddeeb
|
@ -237,3 +237,31 @@ networks: # 定义网络
|
||||||
driver: bridge # 使用 bridge 驱动(默认)
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
|
@ -48,12 +48,7 @@
|
||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- spring-cloud-alibaba -->
|
<!-- spring-cloud-alibaba -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
|
|
@ -37,6 +37,11 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- test -->
|
<!-- test -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
|
|
@ -2,8 +2,10 @@ package cn.bunny.service;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableDiscoveryClient
|
||||||
public class OrderServiceApplication {
|
public class OrderServiceApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(OrderServiceApplication.class, args);
|
SpringApplication.run(OrderServiceApplication.class, args);
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,8 +2,10 @@ package cn.bunny.service;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableDiscoveryClient
|
||||||
public class ProductServiceApplication {
|
public class ProductServiceApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(ProductServiceApplication.class, args);
|
SpringApplication.run(ProductServiceApplication.class, args);
|
||||||
|
|
Loading…
Reference in New Issue