nacos--扩展--1.2--SKD--服务发现

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();
            }
        }

    }
}
 
nacos-server-1.2.0.zip 包是从github上下载的官方源包 Nacos 1.2 踩坑记录 坑1: nacos-mysql.sql 文件中的 permissions 表, 创建的联合唯一键超长, sql文件里是512长度, 导致报 Specified key was too long; max key length is 767 bytes 解决方案1: 直接缩短到255长度 解决方案2: 修改数据库配置, set innodb_large_prefix=on 吐槽一下, 1.2.0版本比1.1.4版本多创建一个数据表, 为甚么前面的sql有引擎,有编码,有注释, 1.2.0新增的表没有这些? 发布之前不做sql的兼容? 强迫症患者表示很难受 坑2: windows版集群部署, 官方文档只交代了简单的配置, 集群模式启动命令中需要加上 -m, 这是在修改startup.cmd看到脚本发现的, 不加-m永远是单机模式... 单机模式: -m standalone 或不配置 -m 集群模式: -m 参数值不等于standalone皆可 压缩包提供了已经修改好的启动文件 startup.cmd, 并支持启动时可附带ip和端口的设置, 支持跨网段集群 下载学习的同学可以对照一下改动位置 linux环境对应修改startup.sh文件, 这个包本身是windows版, 所以我没做sh文件的改动 补充: 集群配置 1、修改 application.properties 末尾追加数据库配置, 用于集群环境下存储 Nacos 配置文件 spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://ip:port/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=xxxx db.password=xxxxx 2、复制 cluster.conf.example 并重命名为 cluster.conf, 写入配置, 最少3个节点构成集群 192.168.1.2:8848 192.168.1.2:8849 192.168.1.2:8850 3、nginx反向代理配置, 因为我这里是单台机器 upstream nacos-server { server 127.0.0.1:8848; server 127.0.0.1:8849; server 127.0.0.1:8850; } server { listen 80; server_name localhost; location / { proxy_pass http://nacos-server; } } 4、启动集群, 写个脚本批量启动 startup_all.bat, 脚本代码如下 start startup.cmd -m -i 192.168.1.2 -p 8848 start startup.cmd -m -i 192.168.1.2 -p 8849 start startup.cmd -m -i 192.168.1.2 -p 8850 需要注意启动命令的 -i 和 -p 是我们自己加入的, 配置的ip和端口需要跟 cluster.conf 对应, 缺省127.0.0.1:8848 此时打开Nacos后台,能看到3个节点已构成集群模式: 节点Ip 节点状态 集群任期 Leader止时(ms) 心跳止时(ms) 192.168.1.2:8849 LEADER 1 16736 2500 192.168.1.2:8848 FOLLOWER 1 12510 2500 192.168.1.2:8850 FOLLOWER 1 16397 2500
nacos-sdk-go是一个用于服务发现的开源软件包。它提供了一种简单且可靠的方式来实现服务发现功能,可以帮助开发人员更方便地构建分布式应用程序。 nacos-sdk-go基于Nacos开源项目开发,Nacos是阿里巴巴开源的一个服务发现和配置管理平台。nacos-sdk-go提供了一系列的API和函数,可以用于注册、发现和管理服务。它支持HTTP和GRPC协议,能够与不同编程语言和框架进行集成。 使用nacos-sdk-go进行服务发现非常简单。首先,我们需要在应用程序中导入nacos-sdk-go的包,并初始化一个Nacos客户端。然后,我们可以使用该客户端注册服务、获取服务列表以及注销服务。例如,我们可以使用RegisterInstance函数将一个实例注册到Nacos服务注册表中。 当其他应用程序需要使用我们的服务时,它们可以使用nacos-sdk-go的DiscoverInstances函数来获取可用的服务实例列表。这样,我们的服务就可以被其他应用程序发现和使用了。 除了服务发现功能,nacos-sdk-go还提供了一些其他功能,如配置管理、动态配置刷新等。它可以帮助我们更好地管理和维护分布式应用程序的配置和服务。 总结来说,nacos-sdk-go是一个功能强大的服务发现工具,它可以帮助开发人员更方便地构建分布式应用程序。通过使用nacos-sdk-go,我们可以实现服务的注册、发现和管理,并能够与其他应用程序进行无缝集成,提高应用程序的可用性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值