当前位置: 首页 > news >正文

Nacos-服务发现

1.开启服务发现功能(@EnableDiscoveryClient)

@EnableDiscoveryClient	//开启服务发现功能
@SpringBootApplication
public class ProductMainApplication {public static void main(String[] args) {SpringApplication.run(ProductMainApplication.class, args);}
}

2.测试服务发现api(DiscoveryClient)

package com.cx.product;import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
import jakarta.annotation.Resource;
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;import java.util.List;/*** @author Jiang* @date 2025/11/7*/
@SpringBootTest
public class DiscoverTest {@AutowiredDiscoveryClient discoveryClient;@Testpublic void discoveryClientTest(){List<String> services = discoveryClient.getServices();for (String service : services) {System.out.println("service = " + service);List<ServiceInstance> instances = discoveryClient.getInstances(service);for (ServiceInstance instance : instances) {System.out.println("instance.getHost() = " + instance.getHost());System.out.println("instance.getPort() = " + instance.getPort());}}}
}

3.测试服务发现api(NacosDiscoveryClient)

package com.cx.product;import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
import jakarta.annotation.Resource;
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;import java.util.List;/*** @author Jiang* @date 2025/11/7*/
@SpringBootTest
public class DiscoverTest {@ResourceNacosDiscoveryClient nacosDiscoveryClient;@Testpublic void nacosDiscoveryClientTest(){List<String> services = nacosDiscoveryClient.getServices();for (String service : services) {System.out.println("service = " + service);List<ServiceInstance> instances = nacosDiscoveryClient.getInstances(service);for (ServiceInstance instance : instances) {System.out.println("instance.getHost() = " + instance.getHost());System.out.println("instance.getPort() = " + instance.getPort());}}}}

pom.xml(product)

<?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"><modelVersion>4.0.0</modelVersion><parent><groupId>com.cx</groupId><artifactId>services</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>services-product</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>//显式添加 我这里不显示添加会报错    //无法解析 org.junit.platform:junit-platform-launcher:1.10.3<junit-platform.version>1.10.3</junit-platform.version></properties><dependencies><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><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-launcher</artifactId><version>${junit-platform.version}</version><scope>test</scope></dependency></dependencies>
</project>
http://www.dtcms.com/a/582753.html

相关文章:

  • 西安网站建设有限公司上海网站建设的意义
  • 网站推广新手入门h5自己制作模板
  • 广西网站建设定制阿里云备案网站负责人
  • 做网站长沙如何去掉wordpress
  • Netty详解-01
  • 我公司让别人做网站了怎么办个人微信公共号可以做微网站么
  • 做网站 发现对方传销兴义 网站建设
  • 节点小宝免费版流量机制解析:点对点直连技术与备用流量设计
  • 扁平化网站源码企业网站的建立费用 作什么科目
  • 卖货网站平台互联网o2o是什么意思
  • 网站建设需要通过哪些审批大同住房和城乡和建设网站
  • 做个企业网站要多少钱网络的推广
  • 一套随访系统源码,医院随访管理系统源码,三级随访平台源码,技术框架:Java+Spring boot,Vue,Ant-Design+MySQL5
  • 响应式网站开发现状宁波高端网站建设推广
  • 摄影网站网页设计网络营销的特征包括
  • 潍坊模板建站定制网站优惠做网站
  • 共筑网络安全,守护绿色家园
  • 一枚指纹,开启工业IoT设备安全与权限分级实践
  • 设计电子商务网站建设方案住建房官网查询
  • 响应式网站做mip小程序什么样才能移到微信上
  • GIS-gdal-java.lang.NoSuchMethodError
  • 注册安全工程师考试科目南京seo顾问
  • 省品牌建设联合会网站关键词查询
  • PsSuspend(7.23):无损挂起与恢复指定进程——精准“冻住”故障现场
  • 台州网站设计公司网站推广公司官网
  • 【LLaVA-NeXT】请问,为什么“Stderr显示是N/A”的信息呢
  • 二级域名做网站好不好2024房地产彻底结束
  • 网站建设动态页面修改删除广州网站优化地址
  • “十五五规划”智慧养老新图景:科技如何让晚年更温暖
  • 好看云在线网站模板下载 迅雷下载 迅雷下载地址花都网络推广seo公司