Nacos源码阅读开篇之Nacos客户端-服务注册信息和NamingService接口


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
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值