• Nacos学习笔记


    视频学习指路:
    【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】

    Nacos

    nacos注册中心的搭建

    1.下载nacos的安装包,github地址:https://github.com/alibaba/nacos,解压缩到英文目录下,打开bin文件下cmd窗口,startup.cmd -m standalone命令启动(Nacos的单机启动)
    2、在对应工程的父级pom.xml下添加依赖

               
                <dependency>
                    <groupId>com.alibaba.cloudgroupId>
                    <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                    <version>2.2.5.RELEASEversion>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3、在子模块对应的pom.xml下注释eureka依赖,添加nacos依赖。这里以orde-rservice模块为例,user-service模块同理。

            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4、修改order-service对应的application.yml配置文件,注释eureka配置,在spring配置下添加nacos配置

    server:
      port: 8080
    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/cloud_order?useSSL=false 
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
      application:
          name: orderservice
      cloud:
        nacos:
          server-addr: localhost:8848
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    启动三个服务,在网页http://localhost:8080/order/101 查询数据,正常情况下可以查到,如果报:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed的错误,则在模块对应的application.yml的数据库配置处将

     url: jdbc:mysql://localhost:3306/cloud_order?useSSL=false 
    
    • 1

    改为

     url: jdbc:mysql://localhost:3306/cloud_order?useSSL=false&allowPublicKeyRetrieval=true
    
    • 1

    即可。
    在cmd下启动nacos服务后
    在这里插入图片描述将Console对应的网址输入到浏览器中,启动相应的服务可以观察到
    在这里插入图片描述说明注册成功。

    Nacos服务分级存储模型

    在这里插入图片描述

    • Nacos服务分级存储模型
      一级是服务,例如user-service
      二级是集群,例如杭州或者上海
      三级是实例,例如杭州机房的某台部署了user-service的服务器

    服务跨集群调用问题:服务调用尽可能选择本地集群的服务,跨集群调用延迟较高。本地集群不可访问的时候,再去访问其它集群。

    • 设置实例的集群属性
      修改application.yml文件,添加spring.cloud.nacos.discovery.cluster-name(服务名称)属性即可。
      在这里插入图片描述
      在这里插入图片描述

    上述步骤只是针对user-service进行了集群的部署,但是目的是让order-service在调用的时候优先本地调用,本地集群不可访问的时候,再去访问其它集群的user-service。
    根据集群进行负载均衡:
    1、修改order-service中的application.yml,这只集群为HZ

    spring:
      cloud:
        nacos:
          server-addr: localhost:8848
          discovery:
            cluster-name: HZ #集群名字
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、在order-service中设置负载均衡的IRule为NacosRule,这个规则优先会寻找与自己同集群的服务:

    userservice:
        ribbon:
          NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule #负载均衡规则
    
    • 1
    • 2
    • 3
    • 在实际的部署当中会出现这样的场景:服务器设备性能有差异,部分实例所在机器的性能较好,另一些较差,希望性能好的机器可以承担更多的用户请求。在Nacos中,提供了权重配置来控制访问的频率,权重越大则访问频率越高。
      –在Nacos控制台可以设置实例的权重值。权重为0则不会被访问。

    环境隔离–namespace
    1、在nacos控制台,新建一个命名空间。
    在这里插入图片描述

    2、修改order-service的application.yml,添加namespace:

    spring:
      cloud:
        nacos:
          server-addr: localhost:8848
          discovery:
            cluster-name: HZ #集群名字
            namespace: 4af5ff2c-b175-40dc-baa7-c2b340d7a5f9 #这里填写命名空间的id,而不是命名空间的名字
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • nacos环境隔离:
      1.namespace用来做环境隔离
      2.每个namespace都有唯一的id
      3.不同namespace下的服务不可见也就是不存在不同命名空间下的服务实例
      在这里插入图片描述

    临时实例和非临时实例:
    服务注册到nacos时候,可以选择成为临时或者非临时实例,默认是临时实例。临时实例宕机时,会从nacos的服务列表中剔除,而非临时实例不会

    spring:
      cloud:
        nacos:
            ephemeral: false #设置为非临时实例
    
    • 1
    • 2
    • 3
    • 4
    • Nacos与eureka的共同点:
      1、都支持服务注册和服务拉取
      2、都支持服务提供者心跳方式做健康检测
    • Nacos与eureka的区别:
      1、Nacos支持服务端主动检测提供者状态:临时实例采用心跳模式,非临时实例采用主动检测模式。
      2、临时实例心跳不正常会被剔除,非临时实例则不会被剔除。—非临时实例会给服务器端造成较大的压力
      3、Nacos支持服务列表变更的消息推送模式,服务列表更新更及时。(nacos是push和pull两种方式结合,eureka是pull方式)
      4、Nacos集群默认采用AP方式,当集群中存在非临时实例时,采用CP模式;Eureka采用AP模式。

    Nacos配置管理

    在这里插入图片描述步骤一:在Nacos控制台点击配置管理,新增配置

    在这里插入图片描述
    在这里插入图片描述
    步骤二:在代码部分配置
    1、引入Nacos的配置管理客户端依赖:哪个服务需要在nacos中需要新增配管理,就在哪个服务的pom文件中添加依赖。这里以userservice服务为例。

            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、在该服务所在的resources资源文件中添加bootstrap.yml文件。(记得注释掉原来服务application中对于服务名称、nacos地址等的配置)

    spring:
      application:
        name: userservice
      profiles:
        active: dev #环境
      cloud:
        nacos:
          server-addr: localhost:8848 #nacos地址
          config:
            file-extension: yaml #文件后缀名
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.测试
    在UserController中

    package com.titi.user.web;
    
    import com.titi.user.pojo.User;
    import com.titi.user.service.UserService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.*;
    
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    @Slf4j
    @RefreshScope
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        //读取配置的注解
        @Value("${pattern.dateformat}")
        private String dateformat;
    
        @GetMapping("now")
        public String now(){
            return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
        }
    
    
        /**
         * 路径: /user/110
         *
         * @param id 用户id
         * @return 用户
         */
        @GetMapping("/{id}")
        public User queryById(@PathVariable("id") Long id) {
            return userService.queryById(id);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    页面输入http://localhost:8081/user/now
    步骤三:配置自动刷新
    Nacos中的配置文件变更后,微服务无需重启就可以感知。通过两种配置实现。
    方式一:在@Value注入的变量所在的类上添加注解@RefreshScope
    方式二:使用@ConfigurationProperties注解

    在该服务的config包下添加PatternProperties类

    package com.titi.user.config;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * @description: ${description}
     * @Title: PatternProperties
     * @Package com.titi.user.config
     * @Author TiTi
     * @Date 2022/11/3 16:05
     */
    
    @Data
    @Component
    @ConfigurationProperties(prefix = "pattern")
    public class PatternProperties {
      private   String dateformat;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    package com.titi.user.web;
    
    import com.titi.user.config.PatternProperties;
    import com.titi.user.pojo.User;
    import com.titi.user.service.UserService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.*;
    
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    @Slf4j
    //@RefreshScope
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        //读取配置的注解
    //    @Value("${pattern.dateformat}")
    //    private String dateformat;
    
        @Autowired
        private PatternProperties properties
    
        @GetMapping("now")
        public String now(){
    
            //return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
            return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
        }
    
    
        /**
         * 路径: /user/110
         *
         * @param id 用户id
         * @return 用户
         */
        @GetMapping("/{id}")
        public User queryById(@PathVariable("id") Long id) {
            return userService.queryById(id);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    多环境配置共享

    • 微服务启动的时候会从nacos读取多个配置文件:
      1、[spring.application.name]-[spring.profiles.active].yaml,例如:userservice-dev.yaml
      2、[spring.application.name].yaml,例如:userservice.yaml
      无论profile如何变化,[spring.application.name].yaml文件一定会加载,因此可以将多环境的配置写入到该文件当中。
      测试
      在这里插入图片描述

      在这里插入图片描述

    package com.titi.user.config;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    @Data
    @Component
    @ConfigurationProperties(prefix = "pattern")
    public class PatternProperties {
      private String dateformat;
      private String envSharedValue;//添加nacos中的共享变量
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在usercontroller类中添加

        @Autowired
        private PatternProperties properties;
    
        @GetMapping("prop")
        public PatternProperties properties(){
            return properties;
        }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    开启userservice和userservice2,由于userservice的.yaml

    spring:
      application:
        name: userservice
      profiles:
        active: dev #环境
      cloud:
        nacos:
          server-addr: localhost:8848 #nacos地址
          config:
            file-extension: yaml #文件后缀名
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    而userservice2的.yaml

    spring:
      application:
        name: userservice2
      profiles:
        active: test #环境
      cloud:
        nacos:
          server-addr: localhost:8848 #nacos地址
          config:
            file-extension: yaml #文件后缀名
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    所以userservice可以读到nacos的userservice-dev.yaml和userservice.yaml配置,而userservice2可以读到nacos的userservice.yaml配置
    测试结果:
    在这里插入图片描述
    在这里插入图片描述多种配置文件的优先级:服务名-profile.yaml>服务名称.yaml>本地配置
    在这里插入图片描述

    Nacos集群搭建

    集群结构图:
    在这里插入图片描述
    集群搭建:
    步骤:

    • 1、搭建数据库,初始化数据库表结构
    • 首先新建一个数据库,命名为nacos,而后导入下面的SQL:
    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(255) DEFAULT NULL,
      `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,
      `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
      `c_desc` varchar(256) DEFAULT NULL,
      `c_use` varchar(64) DEFAULT NULL,
      `effect` varchar(64) DEFAULT NULL,
      `type` varchar(64) DEFAULT NULL,
      `c_schema` text,
      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';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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(255) 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,
      `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='增加租户字段';
    
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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 '租户字段',
      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';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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,
      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';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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容量信息表';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = his_config_info   */
    /******************************************/
    CREATE TABLE `his_config_info` (
      `id` bigint(64) unsigned NOT NULL,
      `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `data_id` varchar(255) NOT NULL,
      `group_id` varchar(128) NOT NULL,
      `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
      `content` longtext NOT NULL,
      `md5` varchar(32) DEFAULT NULL,
      `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `src_user` text,
      `src_ip` varchar(50) DEFAULT NULL,
      `op_type` char(10) DEFAULT NULL,
      `tenant_id` varchar(128) DEFAULT '' 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='多租户改造';
    
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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,
    	`password` varchar(500) NOT NULL,
    	`enabled` boolean NOT NULL
    );
    
    CREATE TABLE `roles` (
    	`username` varchar(50) NOT NULL,
    	`role` varchar(50) NOT NULL,
    	UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE
    );
    
    CREATE TABLE `permissions` (
        `role` varchar(50) NOT NULL,
        `resource` varchar(255) NOT NULL,
        `action` varchar(8) NOT NULL,
        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');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 2、下载nacos安装包
    • 3、配置nacos(一个nacos节点配置一份)
      将压缩包解压到非中文目录下,进入到conf目录,修改配置文件cluster.conf.example,重命名为cluster.conf,然后添加内容:三个节点都在本机,所以是127.0.0.1,实际中按照开发环境来配置
      在这里插入图片描述在这里插入图片描述

    复制三份nacos文件,分别重命名为nacos1、nacos2、nacos3,修改nacos1/conf/application.properties、nacos2/conf/application.properties、nacos3/conf/application.properties下的server.port分别为8845、8846、8847

    • 4、启动nacos集群
      分别从nascos1、nacos2、nacos3下的bin中进入cmd,通过命令startup.cmd命令启动。
    • 5、nginx反向代理
      将nginx安装包解压到非中文目录,打开nginx/conf/nginx.conf配置文件,修改配置(粘贴到http的内容中)
      在这里插入图片描述

    在nginx的目录下进入到cmd,启动nginx,start nginx.exe,在浏览器输入localhost/nacos/
    在本地代码中修改bootstrap.yml中的nacos:server-addr:localhost:80

  • 相关阅读:
    FPGA - 7系列 FPGA内部结构之Memory Resources -02- FIFO资源
    【Linux网络】搭建内外网的网关服务器,实现DNS分离解析与DHCP自动分配
    Vue3 - 组件通信(子传父)
    【Java】医院智能导诊系统源码:解决患者盲目就诊问题、降低患者挂错号比例
    Codeforces Round #771 (Div. 2) D. Big Brush
    机器学习周记(第三十八周:语义分割)2024.5.6~2024.5.12
    2. HarmonyOS工程结构
    R 语言画图中英文字体解决方案
    Arduino从零开始(2)——控制舵机与步进电机
    服务器上运行comsol闪退,无法正常运行
  • 原文地址:https://blog.csdn.net/Apikaqiu/article/details/127102657