nacos–扩展–1.2–SKD–服务发现
代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/master/nacos-learn/nocas-sdk
1、准备
1.1、工具类
/**
* 工具类
*/
public class NacosTools {
/**
* 获取配置服务
*/
public static ConfigService getconfigService() {
String serverAddr = "192.168.187.171:8848";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
ConfigService configService = null;
try {
configService = NacosFactory.createConfigService(properties);
} catch (NacosException e) {
e.printStackTrace();
}
return configService;
}
/**
* 获取名称服务
*/
public static NamingService getNamingService() {
String serverAddr = "192.168.187.171:8848";
NamingService naming = null;
try {
naming = NamingFactory.createNamingService(serverAddr);
} catch (NacosException e) {
e.printStackTrace();
}
return naming;
}
}
2、注册实例
2.1、接口说明
2.2、代码
public class Test_registerInstance {
public static void main(String[] args) throws Exception {
NamingService naming = NacosTools.getNamingService();
System.out.println("======================方式1======================");
//注册
naming.registerInstance("xcoa", "192.168.187.1", 8080, "DEFAULT");
System.out.println("======================方式2======================");
Instance instance = new Instance();
instance.setIp("55.55.55.55");
instance.setPort(9999);
instance.setHealthy(false);
instance.setWeight(2.0);
Map<String, String> instanceMeta = new HashMap<>();
instanceMeta.put("site", "et2");
instance.setMetadata(instanceMeta);
//注册
naming.registerInstance("xcoa2", "oa", instance);
System.out.println("======================方式3======================");
// 测试让主线程不退出,因为订阅配置是守护线程,主线程退出守护线程就会退出。 正式代码中无需下面代码
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3、注销实例
3.1、接口说明
3.2、代码
public class Test_deregisterInstance {
public static void main(String[] args) throws Exception {
NamingService naming = NacosTools.getNamingService();
//注销
naming.deregisterInstance("xcoa", "192.168.187.1", 8080, "DEFAULT");
}
}
4、获取全部实例
4.1、接口说明
4.2、代码
public class Test_getAllInstances {
public static void main(String[] args) throws Exception {
NamingService naming = NacosTools.getNamingService();
List<Instance> xcoa = naming.getAllInstances("xcoa");
System.out.println("======================实例列表======================");
System.out.println(xcoa);
}
}
5、获取健康或不健康实例列表
5.1、接口说明
5.2、代码
public class Test_selectInstances {
public static void main(String[] args) throws Exception {
NamingService naming = NacosTools.getNamingService();
//获取健康实例
List<Instance> xcoa = naming.selectInstances("xcoa", true);
//获取不健康实例
List<Instance> xcoa2 = naming.selectInstances("xcoa2", "oa", false);
System.out.println("======================健康实例列表======================");
System.out.println(xcoa);
System.out.println("======================不健康实例列表======================");
System.out.println(xcoa2);
}
}
6、获取一个健康实例
6.1、接口说明
6.2、代码
public class Test_selectOneHealthyInstance {
public static void main(String[] args) throws Exception {
NamingService naming = NacosTools.getNamingService();
//获取健康实例
Instance instance = naming.selectOneHealthyInstance("xcoa");
System.out.println("======================获取健康实例======================");
System.out.println(instance);
}
}
7、监听服务
7.1、接口说明
7.2、代码
public class Test_subscribe {
public static void main(String[] args) throws Exception {
NamingService naming = NacosTools.getNamingService();
System.out.println("======================监听服务下的实例列表变化======================");
naming.subscribe("xcoa", event -> {
if (event instanceof NamingEvent) {
System.out.println(((NamingEvent) event).getServiceName());
System.out.println(((NamingEvent) event).getInstances());
}
});
}
}
8、取消监听服务
8.1、接口说明
8.2、代码
public class Test_unsubscribe {
public static void main(String[] args) throws Exception {
NamingService naming = NacosTools.getNamingService();
System.out.println("======================监听服务下的实例列表变化======================");
naming.subscribe("xcoa", event -> {
if (event instanceof NamingEvent) {
System.out.println(((NamingEvent) event).getServiceName());
System.out.println(((NamingEvent) event).getInstances());
}
});
System.out.println("======================取消监听服务下的实例列表变化======================");
naming.unsubscribe("xcoa", event -> {
if (event instanceof NamingEvent) {
System.out.println(((NamingEvent) event).getServiceName());
System.out.println(((NamingEvent) event).getInstances());
}
});
// 测试让主线程不退出,因为订阅配置是守护线程,主线程退出守护线程就会退出。 正式代码中无需下面代码
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}