配置管理
在界面上添加配置

 
 我们在界面上新建了一个json类型的配置
使用SDK来获取配置
public String getConfig(String dataId, String group, long timeoutMs) throws NacosException
 
| 名称 | 类型 | 描述 | 
|---|---|---|
| dataId | string | 配置 ID,采用类似 package.class(如com.taobao.tc.refund.log.level)的命名规则保证全局唯一性,class 部分建议是配置的业务含义。全部字符小写。只允许英文字符和 4 种特殊字符(“.”、“:”、“-”、“_”),不超过 256 字节。 | 
| group | string | 配置分组,建议填写产品名:模块名(Nacos:Test)保证唯一性,只允许英文字符和4种特殊字符(“.”、“:”、“-”、“_”),不超过128字节。 | 
| timeout | long | 读取配置超时时间,单位 ms,推荐值 3000 | 
        // Nacos的地址(ip:port),端口是8848也可以不写
        String serverAddr = "localhost:8848";
        ConfigService configService = NacosFactory.createConfigService(serverAddr);
        System.out.println(configService);
        String dataId = "com.yyoo.nacos.sdk.CofingServiceTest";
        String group = "Nacos:Test";
        String content = configService.getConfig(dataId,group,3000);
        System.out.println(content);
 
运行结果
{"conf1":"test"}
 
添加配置监听
添加配置监听之后,Nacos 会推送配置变更
    @Test
    public void testConfig() throws NacosException, InterruptedException {
        // Nacos的地址(ip:port),端口是8848也可以不写
        String serverAddr = "localhost:8848";
        ConfigService configService = NacosFactory.createConfigService(serverAddr);
        System.out.println(configService);
        String dataId = "com.yyoo.nacos.sdk.CofingServiceTest";
        String group = "Nacos:Test";
        String content = configService.getConfig(dataId,group,1000);
        System.out.println(content);
        configService.addListener(dataId, group, new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }
            @Override
            public void receiveConfigInfo(String configInfo) {
                System.out.println("listener:"+configInfo);
            }
        });
        // 休眠线程60秒,因为订阅配置是守护线程,主线程退出守护线程就会退出(只是测试用,正式环境请勿使用)
        Thread.sleep(60000);
    }
 
60秒内,进入Nacos的系统界面。修改对应的配置:
修改发布后,我们的监听器的receiveConfigInfo方法即会执行
listener:{
    "conf1":"test",
    "conf2":"test3"
}
 
Listener接口的继承关系

AbstractConfigChangeListener如果我们要监听配置的变化,我们可以实现改监听来实现。其receiveConfigChange方法的ConfigChangeEvent对象可以获取ConfigChangeItem对象的列表,ConfigChangeItem对象属性就很丰富了。有需求的可以试试。这里不做详细讲解了。
PropertiesListener直接处理了Properties类型的配置,可以直接获取为Properties对象,我们可以照着此实现,来实现Json格式或者其他格式的监听。
删除监听
删除监听后Nacos不在推送配置信息
public void removeListener(String dataId, String group, Listener listener)
 
// 直接传入上面示例的监听对象即可
configService.removeListener(dataId, group, 上面的listener对象);
 
发布配置
public boolean publishConfig(String dataId, String group, String content) throws NacosException;
@Since 1.4.1
public boolean publishConfig(String dataId, String group, String content, String type) throws NacosException;
 
注:创建和修改配置时使用的同一个发布接口,当配置不存在时会创建配置,当配置已存在时会更新配置。
| 名称 | 类型 | 描述 | 
|---|---|---|
| content | string | 配置内容,不超过 100K 字节。 | 
| type | string | @Since 1.4.1. 配置类型,见 com.alibaba.nacos.api.config.ConfigType,默认为TEXT | 
服务管理
注册实例
    /**
     * register a instance to service.
     *
     * @param serviceName name of service
     * @param ip          instance ip
     * @param port        instance port
     * @throws NacosException nacos exception
     */
	void registerInstance(String serviceName, String ip, int port) throws NacosException;
    
    /**
     * register a instance to service.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @param ip          instance ip
     * @param port        instance port
     * @throws NacosException nacos exception
     */
    void registerInstance(String serviceName, String groupName, String ip, int port) throws NacosException;
    
    /**
     * register a instance to service with specified cluster name.
     *
     * @param serviceName name of service
     * @param ip          instance ip
     * @param port        instance port
     * @param clusterName instance cluster name
     * @throws NacosException nacos exception
     */
    void registerInstance(String serviceName, String ip, int port, String clusterName) throws NacosException;
    
    /**
     * register a instance to service with specified cluster name.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @param ip          instance ip
     * @param port        instance port
     * @param clusterName instance cluster name
     * @throws NacosException nacos exception
     */
    void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName)
            throws NacosException;
    
    /**
     * register a instance to service with specified instance properties.
     *
     * @param serviceName name of service
     * @param instance    instance to register
     * @throws NacosException nacos exception
     */
    void registerInstance(String serviceName, Instance instance) throws NacosException;
    
    /**
     * register a instance to service with specified instance properties.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @param instance    instance to register
     * @throws NacosException nacos exception
     */
    void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException;
 
示例1:
        // Nacos的地址(ip:port),端口是8848也可以不写
        String serverAddr = "localhost:8848";
        NamingService namingService = NamingFactory.createNamingService(serverAddr);
        namingService.registerInstance("nacos.test.t1","172.168.1.1",8888,"cluster1");
 
实例2:
        // Nacos的地址(ip:port),端口是8848也可以不写
        String serverAddr = "localhost:8848";
        NamingService namingService = NamingFactory.createNamingService(serverAddr);
        Instance instance = new Instance();
        instance.setIp("172.168.1.1");
        instance.setPort(8888);
        instance.setHealthy(false);
        instance.setWeight(2.0);
        Map<String, String> instanceMeta = new HashMap<>();
        instanceMeta.put("site", "et2");
        instance.setMetadata(instanceMeta);
        instance.setClusterName("cluster1");
        namingService.registerInstance("nacos.test.t1",instance);
 
注销实例
    
    /**
     * deregister instance from a service.
     *
     * @param serviceName name of service
     * @param ip          instance ip
     * @param port        instance port
     * @throws NacosException nacos exception
     */
    void deregisterInstance(String serviceName, String ip, int port) throws NacosException;
    
    /**
     * deregister instance from a service.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @param ip          instance ip
     * @param port        instance port
     * @throws NacosException nacos exception
     */
    void deregisterInstance(String serviceName, String groupName, String ip, int port) throws NacosException;
    
    /**
     * deregister instance with specified cluster name from a service.
     *
     * @param serviceName name of service
     * @param ip          instance ip
     * @param port        instance port
     * @param clusterName instance cluster name
     * @throws NacosException nacos exception
     */
    void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException;
    
    /**
     * deregister instance with specified cluster name from a service.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @param ip          instance ip
     * @param port        instance port
     * @param clusterName instance cluster name
     * @throws NacosException nacos exception
     */
    void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName)
            throws NacosException;
    
    /**
     * deregister instance with full instance information and default groupName.
     *
     * @param serviceName name of service
     * @param instance    instance
     * @throws NacosException nacos exception
     */
    void deregisterInstance(String serviceName, Instance instance) throws NacosException;
    
    /**
     * deregister instance with full instance information.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @param instance    instance information
     * @throws NacosException nacos exception
     */
    void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException;
 
示例:
namingService.deregisterInstance("nacos.test.t1", "172.168.1.1", 8888, "cluster1");
 
获取全部实例
  
    /**
     * get all instances of a service.
     *
     * @param serviceName name of service
     * @return A list of instance
     * @throws NacosException nacos exception
     */
    List<Instance> getAllInstances(String serviceName) throws NacosException;
    
    /**
     * get all instances of a service.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @return A list of instance
     * @throws NacosException nacos exception
     */
    List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException;
    
    /**
     * Get all instances of a service.
     *
     * @param serviceName name of service
     * @param subscribe   if subscribe the service
     * @return A list of instance
     * @throws NacosException nacos exception
     */
    List<Instance> getAllInstances(String serviceName, boolean subscribe) throws NacosException;
    
    /**
     * Get all instances of a service.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @param subscribe   if subscribe the service
     * @return A list of instance
     * @throws NacosException nacos exception
     */
    List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe) throws NacosException;
    
    /**
     * Get all instances within specified clusters of a service.
     *
     * @param serviceName name of service
     * @param clusters    list of cluster
     * @return A list of qualified instance
     * @throws NacosException nacos exception
     */
    List<Instance> getAllInstances(String serviceName, List<String> clusters) throws NacosException;
    
    /**
     * Get all instances within specified clusters of a service.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @param clusters    list of cluster
     * @return A list of qualified instance
     * @throws NacosException nacos exception
     */
    List<Instance> getAllInstances(String serviceName, String groupName, List<String> clusters) throws NacosException;
    
    /**
     * Get all instances within specified clusters of a service.
     *
     * @param serviceName name of service
     * @param clusters    list of cluster
     * @param subscribe   if subscribe the service
     * @return A list of qualified instance
     * @throws NacosException nacos exception
     */
    List<Instance> getAllInstances(String serviceName, List<String> clusters, boolean subscribe) throws NacosException;
    
    /**
     * Get all instances within specified clusters of a service.
     *
     * @param serviceName name of service
     * @param groupName   group of service
     * @param clusters    list of cluster
     * @param subscribe   if subscribe the service
     * @return A list of qualified instance
     * @throws NacosException nacos exception
     */
    List<Instance> getAllInstances(String serviceName, String groupName, List<String> clusters, boolean subscribe)
            throws NacosException;
 
获取健康或不健康实例列表
List<Instance> selectInstances(String serviceName, boolean healthy) throws NacosException;
List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy) throws NacosException;
 
获取一个健康实例
Instance selectOneHealthyInstance(String serviceName) throws NacosException;
Instance selectOneHealthyInstance(String serviceName, List<String> clusters) throws NacosException;
 
监听服务
监听服务下的实例列表变化
void subscribe(String serviceName, EventListener listener) throws NacosException;
void subscribe(String serviceName, List<String> clusters, EventListener listener) throws NacosException;
 
取消监听服务
void unsubscribe(String serviceName, EventListener listener) throws NacosException;
void unsubscribe(String serviceName, List<String> clusters, EventListener listener) throws NacosException;
                



















