nacos集群搭建

目录

一.版本

二.准备数据源

1.拉取mysql镜像

2.部署mysql实例

3.测试数据库连接

 4.创建数据库并初始化nacos所需数据

三.部署nacos节点

1.准备jdk,maven,nacos

2.制作nacos-node镜像

3.部署nacos-node实例

4.修改配置文件

5.启动与关闭节点

四.部署nginx

1.拉取nginx镜像

2.部署nginx实例

3.修改nginx配置并刷新

五.测试结果

六.日志

七.配置管理

1.配置列表

2.历史版本

3.监听查询

八.服务管理

1.服务列表

 2.订阅者列表


一.版本

CPU架构: aarch64

操作系统: Ubuntu 22.04.4 LTS

docker: 26.1.2

mysql: 8.4.0

nginx: 1.25.5

jdk: 1.8.0_371

maven: 3.9.6

nacos: 2.3.2

二.准备数据源

1.拉取mysql镜像
docker pull mysql:8.4.0
2.部署mysql实例
docker run -itd --name mysql4nacos -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:8.4.0
3.测试数据库连接

 4.创建数据库并初始化nacos所需数据
/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/******************************************/
/*   表名称 = config_info                  */
/******************************************/
CREATE TABLE `config_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) DEFAULT NULL COMMENT 'group_id',
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `c_desc` varchar(256) DEFAULT NULL COMMENT 'configuration description',
  `c_use` varchar(64) DEFAULT NULL COMMENT 'configuration usage',
  `effect` varchar(64) DEFAULT NULL COMMENT '配置生效的描述',
  `type` varchar(64) DEFAULT NULL COMMENT '配置的类型',
  `c_schema` text COMMENT '配置的模式',
  `encrypted_data_key` text NOT NULL COMMENT '密钥',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info';

/******************************************/
/*   表名称 = config_info_aggr             */
/******************************************/
CREATE TABLE `config_info_aggr` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `datum_id` varchar(255) NOT NULL COMMENT 'datum_id',
  `content` longtext NOT NULL COMMENT '内容',
  `gmt_modified` datetime NOT NULL COMMENT '修改时间',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='增加租户字段';


/******************************************/
/*   表名称 = config_info_beta             */
/******************************************/
CREATE TABLE `config_info_beta` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `beta_ips` varchar(1024) DEFAULT NULL COMMENT 'betaIps',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `encrypted_data_key` text NOT NULL COMMENT '密钥',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfobeta_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_beta';

/******************************************/
/*   表名称 = config_info_tag              */
/******************************************/
CREATE TABLE `config_info_tag` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
  `tag_id` varchar(128) NOT NULL COMMENT 'tag_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag';

/******************************************/
/*   表名称 = config_tags_relation         */
/******************************************/
CREATE TABLE `config_tags_relation` (
  `id` bigint(20) NOT NULL COMMENT 'id',
  `tag_name` varchar(128) NOT NULL COMMENT 'tag_name',
  `tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
  `nid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'nid, 自增长标识',
  PRIMARY KEY (`nid`),
  UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation';

/******************************************/
/*   表名称 = group_capacity               */
/******************************************/
CREATE TABLE `group_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID,空字符表示整个集群',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数,,0表示使用默认值',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='集群、各Group容量信息表';

/******************************************/
/*   表名称 = his_config_info              */
/******************************************/
CREATE TABLE `his_config_info` (
  `id` bigint(20) unsigned NOT NULL COMMENT 'id',
  `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'nid, 自增标识',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `op_type` char(10) DEFAULT NULL COMMENT 'operation type',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `encrypted_data_key` text NOT NULL COMMENT '密钥',
  PRIMARY KEY (`nid`),
  KEY `idx_gmt_create` (`gmt_create`),
  KEY `idx_gmt_modified` (`gmt_modified`),
  KEY `idx_did` (`data_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='多租户改造';


/******************************************/
/*   表名称 = tenant_capacity              */
/******************************************/
CREATE TABLE `tenant_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='租户容量信息表';


CREATE TABLE `tenant_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `kp` varchar(128) NOT NULL COMMENT 'kp',
  `tenant_id` varchar(128) default '' COMMENT 'tenant_id',
  `tenant_name` varchar(128) default '' COMMENT 'tenant_name',
  `tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc',
  `create_source` varchar(32) DEFAULT NULL COMMENT 'create_source',
  `gmt_create` bigint(20) NOT NULL COMMENT '创建时间',
  `gmt_modified` bigint(20) NOT NULL COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info';

CREATE TABLE `users` (
	`username` varchar(50) NOT NULL PRIMARY KEY COMMENT 'username',
	`password` varchar(500) NOT NULL COMMENT 'password',
	`enabled` boolean NOT NULL COMMENT 'enabled'
);

CREATE TABLE `roles` (
	`username` varchar(50) NOT NULL COMMENT 'username',
	`role` varchar(50) NOT NULL COMMENT 'role',
	UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE
);

CREATE TABLE `permissions` (
    `role` varchar(50) NOT NULL COMMENT 'role',
    `resource` varchar(128) NOT NULL COMMENT 'resource',
    `action` varchar(8) NOT NULL COMMENT 'action',
    UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE
);

INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);

INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');

三.部署nacos节点

1.准备jdk,maven,nacos
drwxr-xr-x 5 root root 4096 May 17 09:16 ./
drwxr-xr-x 3 root root 4096 May 17 09:18 ../
drwxr-xr-x 8 root root 4096 May 17 08:23 jdk/
drwxr-xr-x 6 root root 4096 May 17 08:23 maven/
drwxr-xr-x 5 root root 4096 May 17 08:24 nacos/
2.制作nacos-node镜像
FROM ubuntu:22.04
COPY nacos-node/ /home/
ENV JAVA_HOME=/home/jdk
ENV PATH=${JAVA_HOME}/bin:/home/maven/bin:$PATH
CMD /bin/bash
docker build -t nacos-node:1.0.0 .
docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nacos-node   1.0.0     49d86ad9509d   2 days ago     423MB
3.部署nacos-node实例
docker run -itd --name nacos-1 nacos-node:1.0.0
# or
# docker run -itd --name nacos-1 -p 8858:8848 -p 9858:9848 -p 9859:9849 -p 7858:7848 nacos-node:1.0.0

# 集群部署在同一docker中时,节点通讯走内部路由,无需将端口映射出来
4.修改配置文件
vim /home/nacos/conf/application.properties
# 数据源
spring.sql.init.platform=mysql
db.num=1
db.url.0=jdbc:mysql://10.0.2.5:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
db.user.0=root
db.password.0=123456

# 鉴权
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=true
nacos.core.auth.plugin.nacos.token.secret.key=bmFjb3MuY29yZS4hdXRoLnBsdWdpbi5uYWNvcy50b2tlbi5zZWNyZXQua2V5
nacos.core.auth.server.identity.key=example
nacos.core.auth.server.identity.value=example
vim /home/nacos/conf/cluster.conf

172.17.0.3:8848
172.17.0.4:8848
172.17.0.5:8848

# 修改为集群实际的ip与端口
5.启动与关闭节点
# 启动
/home/nacos/bin/startup.sh

# 关闭
/home/nacos/bin/shutdown.sh

四.部署nginx

1.拉取nginx镜像
docker pull nginx:1.25.5
2.部署nginx实例
docker run -itd -p 8848:8848 --name nginx4nacos nginx:1.25.5
3.修改nginx配置并刷新

配置文件内容


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;

    upstream nacos-cluster {
        server 172.17.0.3:8848;
        server 172.17.0.4:8848;
        server 172.17.0.5:8848;
    }

    server {
        listen 8848;
        server_name 10.0.2.5;
        access_log  /var/log/nginx/nacos.access.log  main;
        location / {
            proxy_pass http://nacos-cluster;
        }
    }

    #include /etc/nginx/conf.d/*.conf;
}
# 覆盖配置文件
docker cp ./nginx.conf ba941e7e13ce:/etc/nginx/

# 重新加载配置文件
docker exec ba941e7e13ce /usr/sbin/nginx -s reload

五.测试结果

访问8848端口,对应nginx

六.日志

  • alipay-jraft.log 记录Nacos使用的JRaft库的日志,nacos节点崩溃时,可以在此日志中体现,jraft通过7848端口进行相互通讯
  • cmdb-main.log 记录Nacos的CMDB模块的主要日志
  • config-client-request.log 记录客户端向Nacos请求配置信息的日志
  • config-dump.log 记录Nacos配置信息的dump信息,用于排查配置信息的变更情况
  • config-fatal.log 记录Nacos配置信息加载失败或者出现致命错误的日志
  • config-memory.log 记录Nacos配置信息在内存中的情况,用于排查内存使用情况
  • config-notify.log
  • config-pull-check.log 记录Nacos配置信息拉取的校验日志
  • config-pull.log
  • config-server.log 记录Nacos配置信息的服务端的主要日志
  • config-trace.log
  • core-auth.log 记录Nacos核心模块的认证和授权相关日志
  • istio-main.log 记录Nacos的Istio集成模块的主要日志
  • nacos-address.log 记录Nacos地址相关的日志
  • nacos-cluster.log 记录Nacos的集群相关的日志
  • nacos-persistence.log 记录Nacos的持久化相关的日志
  • nacos.log 记录Nacos的主要日志
  • nacos_gc.log.0.current 记录Nacos的GC日志
  • naming-distro.log 记录Nacos命名模块的分布式日志,节点失联会在此日志体现
  • naming-event.log 记录Nacos命名模块的事件日志
  • naming-performance.log 记录Nacos命名模块的性能日志
  • naming-push.log 记录Nacos命名模块的推送日志,配置变更会体现在此日志
  • naming-raft.log 记录Nacos命名模块的JRaft日志,集群leader节点
  • naming-rt.log 记录Nacos命名模块的实时日志
  • naming-server.log 记录Nacos命名模块的服务端日志,节点健康情况和客户端连接情况体现在此日志,节点失联崩溃会在这个日志里抛出异常
  • protocol-distro.log 记录Nacos协议模块的分布式日志
  • protocol-raft.log 记录Nacos协议模块的JRaft日志
  • remote-digest.log 记录Nacos远程模块的摘要日志
  • remote-push.log 记录Nacos远程模块的推送日志
  • remote.log 记录Nacos远程模块的主要日志,一些gRpc服务的启动日志,9848/9849端口
  • start.out 记录Nacos启动时的输出信息
  • tps-control-detail.log 记录Nacos流量控制模块的详细日志
  • tps-control-digest.log 记录Nacos流量控制模块的摘要日志
  • tps-control.log 记录Nacos流量控制模块的主要日志
  • /logs/access_log 外部http请求访问的日志,不会自动清理

七.配置管理

1.配置列表

maven工程pom.xml相关项

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>

bootstrap.yml

spring:
  profiles:
    active: dev

bootstrap-dev.yml

spring:
  application:
    name: hello-service
  cloud:
    nacos:
      discovery:
        server-addr: 10.0.2.5:8848
        username: nacos
        password: 123456
        namespace: 91607462-a444-4b66-ac45-ef62dc4ce7cf
        group: cxp
      config:
        server-addr: 10.0.2.5:8848
        username: nacos
        password: 123456
        namespace: 91607462-a444-4b66-ac45-ef62dc4ce7cf
        group: cxp
        data-id: hello-service-dev.yaml
        file-extension: yaml

开启服务

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

获取并使用配置

@RestController
@RefreshScope
public class HelloController {

    @Value("${name}")
    private String name;

    @RequestMapping("hello")
    public String hello() {
        return "hello, " + name;
    }
}

nacos上的配置

 调用接口结果

配置文件存在默认项

${spring.application.name}-${spring.profiles.active}.${spring-cloud-nacos-config.file-extension}

listening config: dataId=hello-service-dev.yaml, group=cxp
listening config: dataId=hello-service.yaml, group=cxp
listening config: dataId=hello-service, group=cxp
2.历史版本

配置文件的修改变更可以通过历史版本功能来查看或回滚

3.监听查询

maven工程pom.xml相关项

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>2.0.3</version>
        </dependency>

维度:配置

        Properties properties = new Properties();
        properties.put("serverAddr", "10.0.2.5:8878");
        properties.put("namespace", "91607462-a444-4b66-ac45-ef62dc4ce7cf");
        properties.put("username", "nacos");
        properties.put("password", "123456");

        ConfigService configService = NacosFactory.createConfigService(properties);
        System.out.println("server status: " + configService.getServerStatus());
        String config = configService.getConfig("hello-service-dev.yaml", "cxp", 5000);
        System.out.println("config = " + config);

        configService.addListener("hello-service-dev.yaml", "cxp", new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String configInfo) {
                System.out.println(configInfo);
            }
        });

 配置变更前后,nacos client的输出

config = name: cxp-dev-2024052214
name: cxp-dev-2024052215
name: cxp-dev-20240522

八.服务管理

1.服务列表

 注意区分永久实例与临时实例

 2.订阅者列表
        Properties properties = new Properties();
        properties.put("serverAddr", "10.0.2.5:8878");
        properties.put("namespace", "91607462-a444-4b66-ac45-ef62dc4ce7cf");
        properties.put("username", "nacos");
        properties.put("password", "123456");

        NamingService namingService = NacosFactory.createNamingService(properties);
        System.out.println("server status: " + namingService.getServerStatus());
        List<Instance> instances = namingService.getAllInstances("hello-service", "cxp");
        for (Instance instance : instances) {
            String ip = instance.getIp();
            System.out.println("ip = " + ip);
        }

        namingService.subscribe("hello-service", "cxp", event -> {
            NamingEvent namingEvent = (NamingEvent) event;
            List<Instance> list = namingEvent.getInstances();
            for (Instance instance : list) {
                String ip = instance.getIp();
                System.out.println("ip = " + ip);
            }
        });

  第一个是hello-service对自己的订阅,第二个是通过nacos client的订阅

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值