文章目录
1、Nacos 注册信息
以源码中自带的ManingTest 测试类距离举例,
这是客户端注册的一个测试类,他模仿了一个真实的服务注册进Naocos 的过程,包括NacosServer连接、实例的创建、实力属性的赋值、注册实例,所以在这个其中包含了服务注册的核心代码,可以看出,Nacos注册服务实例时,包含了两大类信息: Nacos Server 连接信息 和 实例信息。
内容如下:
@Ignore
public class NamingTest {
@Test
public void testServiceList() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
properties.put(PropertyKeyConst.USERNAME, "nacos");
properties.put(PropertyKeyConst.PASSWORD, "nacos");
Instance instance = new Instance();
instance.setIp("1.1.1.1");
instance.setPort(800);
instance.setWeight(2);
Map<String, String> map = new HashMap<String, String>();
map.put("netType", "external");
map.put("version", "2.0");
instance.setMetadata(map);
NamingService namingService = NacosFactory.createNamingService(properties);
namingService.registerInstance("nacos.test.1", instance);
ThreadUtils.sleep(5000L);
List<Instance> list = namingService.getAllInstances("nacos.test.1");
System.out.println