Nacos默认只支持Derby与MySQL两种数据源,扩展支持国产数据库;查阅官网文档信息:Nacos从2.2.0版本开始,可通过SPI机制注入多数据源实现插件,并在引入对应数据源实现后,便可在Nacos启动时通过读取application.properties配置文件中spring.datasource.platform配置项选择加载对应多数据源插件;

本文主要记录此次扩展过程及插件使用过程。以下是此次扩展和使用过程。

一. 插件化实现

1. 引入nacos-datasource-plugin依赖

<dependency>
  <groupId>com.alibaba.nacos</groupId>
  <artifactId>nacos-datasource-plugin</artifactId>
  <version>${alibaba-nacos.version}</version>
  <scope>provided</scope><!--由使用者提供-->
</dependency>

2. 扩展DataSourceConstant类增加国产数据库标识

import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;

public class CustomDataSourceConstant extends DataSourceConstant {
    public static final String DM_8 = "dm8";
    public static final String KINGBASE_8 = "kingbase8";
}

3. 实现com.alibaba.nacos.plugin.datasource.mapper包下数据表对应Mapper接口;依照Mysql的Mapper实现扩展国产数据库对应Mapper接口(由于此次扩展的国产数据库支持mysql模式故直接扩展MysqlMapper)

3.1. 人大金仓数据库

public class ConfigInfoAggrMapperByKingbase extends ConfigInfoAggrMapperByMySql {
    @Override
    public String getDataSource(){
        return KingbaseDataSourceConstant.KINGBASE_8;
    }
}

public class ConfigInfoBetaMapperByKingbase extends ConfigInfoBetaMapperByMySql {
    @Override
    public String getDataSource(){
        return KingbaseDataSourceConstant.KINGBASE_8;
    }
}

public class ConfigInfoMapperByKingbase extends ConfigInfoMapperByMySql {
    @Override
    public String getDataSource(){
        return KingbaseDataSourceConstant.KINGBASE_8;
    }
}

public class ConfigInfoTagMapperByKingbase extends ConfigInfoTagMapperByMySql {
    @Override
    public String getDataSource(){
        return KingbaseDataSourceConstant.KINGBASE_8;
    }
}

public class ConfigTagsRelationMapperByKingbase extends ConfigTagsRelationMapperByMySql {
    @Override
    public String getDataSource(){
        return KingbaseDataSourceConstant.KINGBASE_8;
    }
}

public class GroupCapacityMapperByKingbase extends GroupCapacityMapperByMysql {
    @Override
    public String getDataSource(){
        return KingbaseDataSourceConstant.KINGBASE_8;
    }
}

public class HistoryConfigInfoMapperByKingbase extends HistoryConfigInfoMapperByMySql {
    @Override
    public String getDataSource(){
        return KingbaseDataSourceConstant.KINGBASE_8;
    }
}

public class TenantCapacityMapperByKingbase extends TenantCapacityMapperByMySql {
    @Override
    public String getDataSource(){
        return KingbaseDataSourceConstant.KINGBASE_8;
    }
}

public class TenantInfoMapperByKingbase extends TenantInfoMapperByMySql {
    @Override
    public String getDataSource(){
        return KingbaseDataSourceConstant.KINGBASE_8;
    }
}

3.2. 达梦数据库

public class ConfigInfoAggrMapperByDM extends ConfigInfoAggrMapperByMySql {
    @Override
    public String getDataSource() {
        return DMDataSourceConstant.DM_8;
    }
}

public class ConfigInfoBetaMapperByDM extends ConfigInfoBetaMapperByMySql {
    @Override
    public String getDataSource(){
        return DMDataSourceConstant.DM_8;
    }
}

public class ConfigInfoMapperByDM extends ConfigInfoMapperByMySql {
    @Override
    public String getDataSource(){
        return DMDataSourceConstant.DM_8;
    }
}

public class ConfigInfoTagMapperByDM extends ConfigInfoTagMapperByMySql {
    @Override
    public String getDataSource(){
        return DMDataSourceConstant.DM_8;
    }
}

public class ConfigTagsRelationMapperByDM extends ConfigTagsRelationMapperByMySql {
    @Override
    public String getDataSource(){
        return DMDataSourceConstant.DM_8;
    }
}

public class GroupCapacityMapperByDM extends GroupCapacityMapperByMysql {
    @Override
    public String getDataSource(){
        return DMDataSourceConstant.DM_8;
    }
}

public class HistoryConfigInfoMapperByDM extends HistoryConfigInfoMapperByMySql {
    @Override
    public String getDataSource(){
        return DMDataSourceConstant.DM_8;
    }
}

public class TenantCapacityMapperByDM extends TenantCapacityMapperByMySql {
    @Override
    public String getDataSource(){
        return DMDataSourceConstant.DM_8;
    }
}

public class TenantInfoMapperByDM extends TenantInfoMapperByMySql {
    @Override
    public String getDataSource(){
        return DMDataSourceConstant.DM_8;
    }
}

4. 在/resource/META-INF/service/下添加com.alibaba.nacos.plugin.datasource.mapper.Mapper文件

com.alibaba.nacos.plugin.datasource.extend.kingbase.ConfigInfoAggrMapperByKingbase
com.alibaba.nacos.plugin.datasource.extend.kingbase.ConfigInfoBetaMapperByKingbase
com.alibaba.nacos.plugin.datasource.extend.kingbase.ConfigInfoMapperByKingbase
com.alibaba.nacos.plugin.datasource.extend.kingbase.ConfigInfoTagMapperByKingbase
com.alibaba.nacos.plugin.datasource.extend.kingbase.ConfigTagsRelationMapperByKingbase
com.alibaba.nacos.plugin.datasource.extend.kingbase.HistoryConfigInfoMapperByKingbase
com.alibaba.nacos.plugin.datasource.extend.kingbase.TenantInfoMapperByKingbase
com.alibaba.nacos.plugin.datasource.extend.kingbase.TenantCapacityMapperByKingbase
com.alibaba.nacos.plugin.datasource.extend.kingbase.GroupCapacityMapperByKingbase

com.alibaba.nacos.plugin.datasource.extend.dm.ConfigInfoAggrMapperByDM
com.alibaba.nacos.plugin.datasource.extend.dm.ConfigInfoBetaMapperByDM
com.alibaba.nacos.plugin.datasource.extend.dm.ConfigInfoMapperByDM
com.alibaba.nacos.plugin.datasource.extend.dm.ConfigInfoTagMapperByDM
com.alibaba.nacos.plugin.datasource.extend.dm.ConfigTagsRelationMapperByDM
com.alibaba.nacos.plugin.datasource.extend.dm.HistoryConfigInfoMapperByDM
com.alibaba.nacos.plugin.datasource.extend.dm.TenantInfoMapperByDM
com.alibaba.nacos.plugin.datasource.extend.dm.TenantCapacityMapperByDM
com.alibaba.nacos.plugin.datasource.extend.dm.GroupCapacityMapperByDM

5. 编译插件jar

二. 插件使用

1.  初始化数据库

1.1. 人大金仓数据库

CREATE TABLE "config_info" (
                               "id" bigserial,
                               "data_id" character varying(255 char) NOT NULL,
	"group_id" character varying(255 char) NULL,
	"content" text NOT NULL,
	"md5" character varying(32 char) NULL,
	"gmt_create" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"gmt_modified" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"src_user" text NULL,
	"src_ip" character varying(50 char) NULL,
	"app_name" character varying(128 char) NULL,
	"tenant_id" character varying(128 char) NULL DEFAULT NULL::varchar,
	"c_desc" character varying(256 char) NULL,
	"c_use" character varying(64 char) NULL,
	"effect" character varying(64 char) NULL,
	"type" character varying(64 char) NULL,
	"c_schema" text NULL,
	"encrypted_data_key" text  NULL,
	CONSTRAINT "PRIMARY_73F26186" PRIMARY KEY (id)
);
CREATE UNIQUE INDEX CONFIG_INFO_DATA_ID_IDX ON "config_info" ("data_id","group_id","tenant_id");


-- "pig_config"."config_info_aggr" definition

-- Drop table

-- DROP TABLE "config_info_aggr";

CREATE TABLE "config_info_aggr" (
                                    "id" bigserial,
                                    "data_id" character varying(255 char) NOT NULL,
	"group_id" character varying(255 char) NOT NULL,
	"datum_id" character varying(255 char) NOT NULL,
	"content" text NOT NULL,
	"gmt_modified" timestamp(0) without time zone NOT NULL,
	"app_name" character varying(128 char) NULL,
	"tenant_id" character varying(128 char) NULL DEFAULT NULL::varchar,
	CONSTRAINT "PRIMARY_F787E5C0" PRIMARY KEY (id)
);


-- "pig_config"."config_info_beta" definition

-- Drop table

-- DROP TABLE "config_info_beta";

CREATE TABLE "config_info_beta" (
                                    "id" bigserial,
                                    "data_id" character varying(255 char) NOT NULL,
	"group_id" character varying(128 char) NOT NULL,
	"app_name" character varying(128 char) NULL,
	"content" text NOT NULL,
	"beta_ips" character varying(1024 char) NULL,
	"md5" character varying(32 char) NULL,
	"gmt_create" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"gmt_modified" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"src_user" text NULL,
	"src_ip" character varying(50 char) NULL,
	"tenant_id" character varying(128 char) NULL DEFAULT NULL::varchar,
	"encrypted_data_key" text  NULL,
	CONSTRAINT "PRIMARY_E3F5B61F" PRIMARY KEY (id)
);


-- "pig_config"."config_info_tag" definition

-- Drop table

-- DROP TABLE "config_info_tag";

CREATE TABLE "config_info_tag" (
                                   "id" bigserial,
                                   "data_id" character varying(255 char) NOT NULL,
	"group_id" character varying(128 char) NOT NULL,
	"tenant_id" character varying(128 char) NULL DEFAULT NULL::varchar,
	"tag_id" character varying(128 char) NOT NULL,
	"app_name" character varying(128 char) NULL,
	"content" text NOT NULL,
	"md5" character varying(32 char) NULL,
	"gmt_create" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"gmt_modified" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"src_user" text NULL,
	"src_ip" character varying(50 char) NULL,
	CONSTRAINT "PRIMARY_26447CE1" PRIMARY KEY (id)
);


-- "pig_config"."config_tags_relation" definition

-- Drop table

-- DROP TABLE "config_tags_relation";

CREATE TABLE "config_tags_relation" (
                                        "id" bigint NOT NULL,
                                        "tag_name" character varying(128 char) NOT NULL,
	"tag_type" character varying(64 char) NULL,
	"data_id" character varying(255 char) NOT NULL,
	"group_id" character varying(128 char) NOT NULL,
	"tenant_id" character varying(128 char) NULL DEFAULT NULL::varchar,
	"nid" bigserial,
	CONSTRAINT "PRIMARY_82226084" PRIMARY KEY (nid)
);
CREATE INDEX idx_tenant_id_4FB846F8 ON config_tags_relation USING btree (tenant_id);


-- "pig_config"."group_capacity" definition

-- Drop table

-- DROP TABLE "group_capacity";

CREATE TABLE "group_capacity" (
                                  "id" bigserial,
                                  "group_id" character varying(128 char) NOT NULL DEFAULT NULL::varchar,
	"quota" bigint NOT NULL DEFAULT 0,
	"usage" bigint NOT NULL DEFAULT 0,
	"max_size" bigint NOT NULL DEFAULT 0,
	"max_aggr_count" bigint NOT NULL DEFAULT 0,
	"max_aggr_size" bigint NOT NULL DEFAULT 0,
	"max_history_count" bigint NOT NULL DEFAULT 0,
	"gmt_create" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"gmt_modified" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	CONSTRAINT "PRIMARY_D72B7E75" PRIMARY KEY (id)
);


-- "pig_config"."his_config_info" definition

-- Drop table

-- DROP TABLE "his_config_info";

CREATE TABLE "his_config_info" (
                                   "id" numeric NOT NULL,
                                   "nid" bigserial,
                                   "data_id" character varying(255 char) NOT NULL,
	"group_id" character varying(128 char) NOT NULL,
	"app_name" character varying(128 char) NULL,
	"content" text NOT NULL,
	"md5" character varying(32 char) NULL,
	"gmt_create" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"gmt_modified" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"src_user" text NULL,
	"src_ip" character varying(50 char) NULL,
	"op_type" character(10 char) NULL,
	"tenant_id" character varying(128 char) NULL DEFAULT NULL::varchar,
	"encrypted_data_key" text NULL,
	CONSTRAINT "PRIMARY_93A4DC6B" PRIMARY KEY (nid)
);
CREATE INDEX idx_did_8DE97C10 ON his_config_info USING btree (data_id);
CREATE INDEX idx_gmt_create_F3B3E5A1 ON his_config_info USING btree (gmt_create);
CREATE INDEX idx_gmt_modified_42DBB1BB ON his_config_info USING btree (gmt_modified);


-- "pig_config"."permissions" definition

-- Drop table

-- DROP TABLE "permissions";

CREATE TABLE "permissions" (
    "role" character varying(50 char) NOT NULL,
	"resource" character varying(255 char) NOT NULL,
	"action" character varying(8 char) NOT NULL
);


-- "pig_config"."roles" definition

-- Drop table

-- DROP TABLE "roles";

CREATE TABLE "roles" (
    "username" character varying(50 char) NOT NULL,
	"role" character varying(50 char) NOT NULL
);


-- "pig_config"."tenant_capacity" definition

-- Drop table

-- DROP TABLE "tenant_capacity";

CREATE TABLE "tenant_capacity" (
                                   "id" bigserial,
                                   "tenant_id" character varying(128 char) NOT NULL DEFAULT NULL::varchar,
	"quota" bigint NOT NULL DEFAULT 0,
	"usage" bigint NOT NULL DEFAULT 0,
	"max_size" bigint NOT NULL DEFAULT 0,
	"max_aggr_count" bigint NOT NULL DEFAULT 0,
	"max_aggr_size" bigint NOT NULL DEFAULT 0,
	"max_history_count" bigint NOT NULL DEFAULT 0,
	"gmt_create" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	"gmt_modified" timestamp(0) without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
	CONSTRAINT "PRIMARY_F4CCC2AA" PRIMARY KEY (id)
);


-- "pig_config"."tenant_info" definition

-- Drop table

-- DROP TABLE "tenant_info";

CREATE TABLE "tenant_info" (
                               "id" bigserial,
                               "kp" character varying(128 char) NOT NULL,
	"tenant_id" character varying(128 char) NULL DEFAULT NULL::varchar,
	"tenant_name" character varying(128 char) NULL DEFAULT NULL::varchar,
	"tenant_desc" character varying(256 char) NULL,
	"create_source" character varying(32 char) NULL,
	"gmt_create" bigint NOT NULL,
	"gmt_modified" bigint NOT NULL,
	CONSTRAINT "PRIMARY_8B91ED1E" PRIMARY KEY (id)
);
CREATE INDEX idx_tenant_id_ABB71DBA ON tenant_info USING btree (tenant_id);


-- "pig_config"."users" definition

-- Drop table

-- DROP TABLE "users";

CREATE TABLE "users" (
    "username" character varying(50 char) NOT NULL,
	"password" character varying(500 char) NOT NULL,
	"enabled" tinyint NOT NULL,
	CONSTRAINT "PRIMARY_1A6B23BE" PRIMARY KEY (username)
);

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

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

1.2. 达梦数据库

CREATE TABLE CONFIG_INFO
(
    ID BIGINT IDENTITY(1, 1) NOT NULL,
    DATA_ID VARCHAR(2040),
    GROUP_ID VARCHAR(1024),
    CONTENT CLOB,
    MD5 VARCHAR(256),
    GMT_CREATE TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00',
    GMT_MODIFIED TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00',
    SRC_USER TEXT,
    SRC_IP VARCHAR(160),
    APP_NAME VARCHAR(1024),
    TENANT_ID VARCHAR(1024) DEFAULT '',
    C_DESC VARCHAR(2048),
    C_USE VARCHAR(512),
    EFFECT VARCHAR(512),
    TYPE VARCHAR(512),
    C_SCHEMA TEXT,
    ENCRYPTED_DATA_KEY TEXT,
    NOT CLUSTER PRIMARY KEY(ID),
    CONSTRAINT UK_CONFIGINFO_DATAGROUPTENANT UNIQUE(DATA_ID, GROUP_ID, TENANT_ID)) STORAGE(ON NACOS, CLUSTERBTR) ;

COMMENT ON TABLE CONFIG_INFO IS 'config_info';
COMMENT ON COLUMN CONFIG_INFO.CONTENT IS 'content';
COMMENT ON COLUMN CONFIG_INFO.DATA_ID IS 'data_id';
COMMENT ON COLUMN CONFIG_INFO.ENCRYPTED_DATA_KEY IS '秘钥';
COMMENT ON COLUMN CONFIG_INFO.GMT_CREATE IS '创建时间';
COMMENT ON COLUMN CONFIG_INFO.GMT_MODIFIED IS '修改时间';
COMMENT ON COLUMN CONFIG_INFO.ID IS 'id';
COMMENT ON COLUMN CONFIG_INFO.MD5 IS 'md5';
COMMENT ON COLUMN CONFIG_INFO.SRC_IP IS 'source ip';
COMMENT ON COLUMN CONFIG_INFO.SRC_USER IS 'source user';
COMMENT ON COLUMN CONFIG_INFO.TENANT_ID IS '租户字段';


CREATE TABLE CONFIG_INFO_AGGR
(
    ID BIGINT IDENTITY(1, 1) NOT NULL,
    DATA_ID VARCHAR(2040) NOT NULL,
    GROUP_ID VARCHAR(1024) NOT NULL,
    DATUM_ID VARCHAR(2040) NOT NULL,
    CONTENT CLOB NOT NULL,
    GMT_MODIFIED TIMESTAMP(0) NOT NULL,
    APP_NAME VARCHAR(1024),
    TENANT_ID VARCHAR(1024) DEFAULT '',
    NOT CLUSTER PRIMARY KEY(ID),
    CONSTRAINT UK_CONFIGINFOAGGR_DATAGROUPTENANTDATUM UNIQUE(DATA_ID, GROUP_ID, TENANT_ID, DATUM_ID)) STORAGE(ON NACOS, CLUSTERBTR) ;

COMMENT ON TABLE CONFIG_INFO_AGGR IS '增加租户字段';
COMMENT ON COLUMN CONFIG_INFO_AGGR.CONTENT IS '内容';
COMMENT ON COLUMN CONFIG_INFO_AGGR.DATA_ID IS 'data_id';
COMMENT ON COLUMN CONFIG_INFO_AGGR.DATUM_ID IS 'datum_id';
COMMENT ON COLUMN CONFIG_INFO_AGGR.GMT_MODIFIED IS '修改时间';
COMMENT ON COLUMN CONFIG_INFO_AGGR.GROUP_ID IS 'group_id';
COMMENT ON COLUMN CONFIG_INFO_AGGR.ID IS 'id';
COMMENT ON COLUMN CONFIG_INFO_AGGR.TENANT_ID IS '租户字段';


CREATE TABLE CONFIG_INFO_BETA
(
    ID BIGINT IDENTITY(1, 1) NOT NULL,
    DATA_ID VARCHAR(2040) NOT NULL,
    GROUP_ID VARCHAR(1024) NOT NULL,
    APP_NAME VARCHAR(1024),
    CONTENT CLOB NOT NULL,
    BETA_IPS VARCHAR(8192),
    MD5 VARCHAR(256),
    GMT_CREATE TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    GMT_MODIFIED TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    SRC_USER TEXT,
    SRC_IP VARCHAR(160),
    TENANT_ID VARCHAR(1024) DEFAULT '',
    ENCRYPTED_DATA_KEY TEXT NOT NULL,
    NOT CLUSTER PRIMARY KEY(ID),
    CONSTRAINT UK_CONFIGINFOBETA_DATAGROUPTENANT UNIQUE(DATA_ID, GROUP_ID, TENANT_ID)) STORAGE(ON NACOS, CLUSTERBTR) ;

COMMENT ON TABLE CONFIG_INFO_BETA IS 'config_info_beta';
COMMENT ON COLUMN CONFIG_INFO_BETA.APP_NAME IS 'app_name';
COMMENT ON COLUMN CONFIG_INFO_BETA.BETA_IPS IS 'betaIps';
COMMENT ON COLUMN CONFIG_INFO_BETA.CONTENT IS 'content';
COMMENT ON COLUMN CONFIG_INFO_BETA.DATA_ID IS 'data_id';
COMMENT ON COLUMN CONFIG_INFO_BETA.ENCRYPTED_DATA_KEY IS '秘钥';
COMMENT ON COLUMN CONFIG_INFO_BETA.GMT_CREATE IS '创建时间';
COMMENT ON COLUMN CONFIG_INFO_BETA.GMT_MODIFIED IS '修改时间';
COMMENT ON COLUMN CONFIG_INFO_BETA.GROUP_ID IS 'group_id';
COMMENT ON COLUMN CONFIG_INFO_BETA.ID IS 'id';
COMMENT ON COLUMN CONFIG_INFO_BETA.MD5 IS 'md5';
COMMENT ON COLUMN CONFIG_INFO_BETA.SRC_IP IS 'source ip';
COMMENT ON COLUMN CONFIG_INFO_BETA.SRC_USER IS 'source user';
COMMENT ON COLUMN CONFIG_INFO_BETA.TENANT_ID IS '租户字段';


CREATE TABLE CONFIG_INFO_TAG
(
    ID BIGINT IDENTITY(1, 1) NOT NULL,
    DATA_ID VARCHAR(2040) NOT NULL,
    GROUP_ID VARCHAR(1024) NOT NULL,
    TENANT_ID VARCHAR(1024) DEFAULT '',
    TAG_ID VARCHAR(1024) NOT NULL,
    APP_NAME VARCHAR(1024),
    CONTENT CLOB NOT NULL,
    MD5 VARCHAR(256),
    GMT_CREATE TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    GMT_MODIFIED TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    SRC_USER TEXT,
    SRC_IP VARCHAR(160),
    NOT CLUSTER PRIMARY KEY(ID),
    CONSTRAINT UK_CONFIGINFOTAG_DATAGROUPTENANTTAG UNIQUE(DATA_ID, GROUP_ID, TENANT_ID, TAG_ID)) STORAGE(ON NACOS, CLUSTERBTR) ;

COMMENT ON TABLE CONFIG_INFO_TAG IS 'config_info_tag';
COMMENT ON COLUMN CONFIG_INFO_TAG.APP_NAME IS 'app_name';
COMMENT ON COLUMN CONFIG_INFO_TAG.CONTENT IS 'content';
COMMENT ON COLUMN CONFIG_INFO_TAG.DATA_ID IS 'data_id';
COMMENT ON COLUMN CONFIG_INFO_TAG.GMT_CREATE IS '创建时间';
COMMENT ON COLUMN CONFIG_INFO_TAG.GMT_MODIFIED IS '修改时间';
COMMENT ON COLUMN CONFIG_INFO_TAG.GROUP_ID IS 'group_id';
COMMENT ON COLUMN CONFIG_INFO_TAG.ID IS 'id';
COMMENT ON COLUMN CONFIG_INFO_TAG.MD5 IS 'md5';
COMMENT ON COLUMN CONFIG_INFO_TAG.SRC_IP IS 'source ip';
COMMENT ON COLUMN CONFIG_INFO_TAG.SRC_USER IS 'source user';
COMMENT ON COLUMN CONFIG_INFO_TAG.TAG_ID IS 'tag_id';
COMMENT ON COLUMN CONFIG_INFO_TAG.TENANT_ID IS 'tenant_id';


CREATE TABLE CONFIG_TAGS_RELATION
(
    ID BIGINT NOT NULL,
    TAG_NAME VARCHAR(1024) NOT NULL,
    TAG_TYPE VARCHAR(512),
    DATA_ID VARCHAR(2040) NOT NULL,
    GROUP_ID VARCHAR(1024) NOT NULL,
    TENANT_ID VARCHAR(1024) DEFAULT '',
    NID BIGINT IDENTITY(1, 1) NOT NULL,
    NOT CLUSTER PRIMARY KEY(NID),
    CONSTRAINT UK_CONFIGTAGRELATION_CONFIGIDTAG UNIQUE(ID, TAG_NAME, TAG_TYPE)) STORAGE(ON NACOS, CLUSTERBTR) ;

COMMENT ON TABLE CONFIG_TAGS_RELATION IS 'config_tag_relation';
COMMENT ON COLUMN CONFIG_TAGS_RELATION.DATA_ID IS 'data_id';
COMMENT ON COLUMN CONFIG_TAGS_RELATION.GROUP_ID IS 'group_id';
COMMENT ON COLUMN CONFIG_TAGS_RELATION.ID IS 'id';
COMMENT ON COLUMN CONFIG_TAGS_RELATION.TAG_NAME IS 'tag_name';
COMMENT ON COLUMN CONFIG_TAGS_RELATION.TAG_TYPE IS 'tag_type';
COMMENT ON COLUMN CONFIG_TAGS_RELATION.TENANT_ID IS 'tenant_id';


CREATE  INDEX IDX_TENANT_ID ON CONFIG_TAGS_RELATION(TENANT_ID ASC) STORAGE(ON NACOS, CLUSTERBTR) ;

CREATE TABLE GROUP_CAPACITY
(
    ID BIGINT IDENTITY(1, 1) NOT NULL,
    GROUP_ID VARCHAR(1024) DEFAULT '' NOT NULL,
    QUOTA BIGINT DEFAULT 0 NOT NULL,
    USAGE BIGINT DEFAULT 0 NOT NULL,
    MAX_SIZE BIGINT DEFAULT 0 NOT NULL,
    MAX_AGGR_COUNT BIGINT DEFAULT 0 NOT NULL,
    MAX_AGGR_SIZE BIGINT DEFAULT 0 NOT NULL,
    MAX_HISTORY_COUNT BIGINT DEFAULT 0 NOT NULL,
    GMT_CREATE TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    GMT_MODIFIED TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    NOT CLUSTER PRIMARY KEY(ID),
    CONSTRAINT UK_GROUP_ID UNIQUE(GROUP_ID),
    CHECK(QUOTA >= 0)
    ,CHECK(USAGE >= 0)
    ,CHECK(MAX_SIZE >= 0)
    ,CHECK(MAX_AGGR_COUNT >= 0)
    ,CHECK(MAX_AGGR_SIZE >= 0)
    ,CHECK(MAX_HISTORY_COUNT >= 0)) STORAGE(ON NACOS, CLUSTERBTR) ;

COMMENT ON TABLE GROUP_CAPACITY IS '集群、各Group容量信息表';
COMMENT ON COLUMN GROUP_CAPACITY.GMT_CREATE IS '创建时间';
COMMENT ON COLUMN GROUP_CAPACITY.GMT_MODIFIED IS '修改时间';
COMMENT ON COLUMN GROUP_CAPACITY.GROUP_ID IS 'Group ID,空字符表示整个集群';
COMMENT ON COLUMN GROUP_CAPACITY.ID IS '主键ID';
COMMENT ON COLUMN GROUP_CAPACITY.MAX_AGGR_COUNT IS '聚合子配置最大个数,,0表示使用默认值';
COMMENT ON COLUMN GROUP_CAPACITY.MAX_AGGR_SIZE IS '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值';
COMMENT ON COLUMN GROUP_CAPACITY.MAX_HISTORY_COUNT IS '最大变更历史数量';
COMMENT ON COLUMN GROUP_CAPACITY.MAX_SIZE IS '单个配置大小上限,单位为字节,0表示使用默认值';
COMMENT ON COLUMN GROUP_CAPACITY.QUOTA IS '配额,0表示使用默认值';
COMMENT ON COLUMN GROUP_CAPACITY.USAGE IS '使用量';


CREATE TABLE HIS_CONFIG_INFO
(
    ID DECIMAL(20,0) NOT NULL,
    NID BIGINT IDENTITY(1, 1) NOT NULL,
    DATA_ID VARCHAR(2040) NOT NULL,
    GROUP_ID VARCHAR(1024) NOT NULL,
    APP_NAME VARCHAR(1024),
    CONTENT CLOB NOT NULL,
    MD5 VARCHAR(256),
    GMT_CREATE TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    GMT_MODIFIED TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    SRC_USER TEXT,
    SRC_IP VARCHAR(160),
    OP_TYPE CHAR(80),
    TENANT_ID VARCHAR(1024) DEFAULT '',
    ENCRYPTED_DATA_KEY TEXT NOT NULL,
    NOT CLUSTER PRIMARY KEY(NID),
    CHECK(ID >= 0)) STORAGE(ON NACOS, CLUSTERBTR) ;

COMMENT ON TABLE HIS_CONFIG_INFO IS '多租户改造';
COMMENT ON COLUMN HIS_CONFIG_INFO.APP_NAME IS 'app_name';
COMMENT ON COLUMN HIS_CONFIG_INFO.ENCRYPTED_DATA_KEY IS '秘钥';
COMMENT ON COLUMN HIS_CONFIG_INFO.TENANT_ID IS '租户字段';


CREATE  INDEX IDX_GMT_CREATE ON HIS_CONFIG_INFO(GMT_CREATE ASC) STORAGE(ON NACOS, CLUSTERBTR) ;
CREATE  INDEX IDX_DID ON HIS_CONFIG_INFO(DATA_ID ASC) STORAGE(ON NACOS, CLUSTERBTR) ;
CREATE  INDEX IDX_GMT_MODIFIED ON HIS_CONFIG_INFO(GMT_MODIFIED ASC) STORAGE(ON NACOS, CLUSTERBTR) ;

CREATE TABLE PERMISSIONS
(
    ROLE VARCHAR(400) NOT NULL,
    RESOURCE VARCHAR(4096) NOT NULL,
    ACTION VARCHAR(64) NOT NULL,
    CONSTRAINT UK_ROLE_PERMISSION UNIQUE(ROLE, RESOURCE, ACTION)) STORAGE(ON NACOS, CLUSTERBTR) ;

CREATE TABLE ROLES
(
    USERNAME VARCHAR(400) NOT NULL,
    ROLE VARCHAR(400) NOT NULL,
    CONSTRAINT UK_USERNAME_ROLE UNIQUE(USERNAME, ROLE)) STORAGE(ON NACOS, CLUSTERBTR) ;

CREATE TABLE TENANT_CAPACITY
(
    ID BIGINT IDENTITY(1, 1) NOT NULL,
    TENANT_ID VARCHAR(1024) DEFAULT '' NOT NULL,
    QUOTA BIGINT DEFAULT 0 NOT NULL,
    USAGE BIGINT DEFAULT 0 NOT NULL,
    MAX_SIZE BIGINT DEFAULT 0 NOT NULL,
    MAX_AGGR_COUNT BIGINT DEFAULT 0 NOT NULL,
    MAX_AGGR_SIZE BIGINT DEFAULT 0 NOT NULL,
    MAX_HISTORY_COUNT BIGINT DEFAULT 0 NOT NULL,
    GMT_CREATE TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    GMT_MODIFIED TIMESTAMP(0) DEFAULT '2010-05-05 00:00:00' NOT NULL,
    NOT CLUSTER PRIMARY KEY(ID),
    CONSTRAINT UK_TENANT_ID UNIQUE(TENANT_ID),
    CHECK(QUOTA >= 0)
    ,CHECK(USAGE >= 0)
    ,CHECK(MAX_SIZE >= 0)
    ,CHECK(MAX_AGGR_COUNT >= 0)
    ,CHECK(MAX_AGGR_SIZE >= 0)
    ,CHECK(MAX_HISTORY_COUNT >= 0)) STORAGE(ON NACOS, CLUSTERBTR) ;

COMMENT ON TABLE TENANT_CAPACITY IS '租户容量信息表';
COMMENT ON COLUMN TENANT_CAPACITY.GMT_CREATE IS '创建时间';
COMMENT ON COLUMN TENANT_CAPACITY.GMT_MODIFIED IS '修改时间';
COMMENT ON COLUMN TENANT_CAPACITY.ID IS '主键ID';
COMMENT ON COLUMN TENANT_CAPACITY.MAX_AGGR_COUNT IS '聚合子配置最大个数';
COMMENT ON COLUMN TENANT_CAPACITY.MAX_AGGR_SIZE IS '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值';
COMMENT ON COLUMN TENANT_CAPACITY.MAX_HISTORY_COUNT IS '最大变更历史数量';
COMMENT ON COLUMN TENANT_CAPACITY.MAX_SIZE IS '单个配置大小上限,单位为字节,0表示使用默认值';
COMMENT ON COLUMN TENANT_CAPACITY.QUOTA IS '配额,0表示使用默认值';
COMMENT ON COLUMN TENANT_CAPACITY.TENANT_ID IS 'Tenant ID';
COMMENT ON COLUMN TENANT_CAPACITY.USAGE IS '使用量';


CREATE TABLE TENANT_INFO
(
    ID BIGINT IDENTITY(1, 1) NOT NULL,
    KP VARCHAR(1024) NOT NULL,
    TENANT_ID VARCHAR(1024) DEFAULT '',
    TENANT_NAME VARCHAR(1024) DEFAULT '',
    TENANT_DESC VARCHAR(2048),
    CREATE_SOURCE VARCHAR(256),
    GMT_CREATE BIGINT NOT NULL,
    GMT_MODIFIED BIGINT NOT NULL,
    NOT CLUSTER PRIMARY KEY(ID),
    CONSTRAINT UK_TENANT_INFO_KPTENANTID UNIQUE(KP, TENANT_ID)) STORAGE(ON NACOS, CLUSTERBTR) ;

COMMENT ON TABLE TENANT_INFO IS 'tenant_info';
COMMENT ON COLUMN TENANT_INFO.CREATE_SOURCE IS 'create_source';
COMMENT ON COLUMN TENANT_INFO.GMT_CREATE IS '创建时间';
COMMENT ON COLUMN TENANT_INFO.GMT_MODIFIED IS '修改时间';
COMMENT ON COLUMN TENANT_INFO.ID IS 'id';
COMMENT ON COLUMN TENANT_INFO.KP IS 'kp';
COMMENT ON COLUMN TENANT_INFO.TENANT_DESC IS 'tenant_desc';
COMMENT ON COLUMN TENANT_INFO.TENANT_ID IS 'tenant_id';
COMMENT ON COLUMN TENANT_INFO.TENANT_NAME IS 'tenant_name';


CREATE  INDEX INDEX23489292270800 ON TENANT_INFO(TENANT_ID ASC) STORAGE(ON NACOS, CLUSTERBTR) ;

CREATE TABLE USERS
(
    USERNAME VARCHAR(400) NOT NULL,
    PASSWORD VARCHAR(4000) NOT NULL,
    ENABLED TINYINT NOT NULL,
    NOT CLUSTER PRIMARY KEY(USERNAME)) STORAGE(ON NACOS, CLUSTERBTR) ;

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

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

2. 在Nacos安装目录下的plugins文件夹中添加我们实现的插件(custom-plugin.jar);如果nacos目录下没有plugins文件夹,则创建plugins文件夹。注意:需添加对应数据库的驱动jar包

3. 在application.properties配置文件中将spring.datasource.platform修改为对应的数据源名称,并配置数据源相关参数。spring.datasource.platform的内容为扩展的数据库标识,如人大金仓数据库定义为:kingbase8

spring.datasource.platform=kingbase8
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:kingbase://127.0.0.1:3306/nacos
db.user=nacos
db.password=nacos

4. 重启Nacos

三. 源码

Gitee:nacos-datasource-plugins-extend: nacos数据源插件扩展 - Gitee.com

四. 参考

Nacos官网文档: 多数据源

GitHub 加速计划 / na / nacos
29.84 K
12.75 K
下载
Nacos是由阿里巴巴开源的服务治理中间件,集成了动态服务发现、配置管理和服务元数据管理功能,广泛应用于微服务架构中,简化服务治理过程。
最近提交(Master分支:3 个月前 )
4334cd16 * Support custom client configuration timeout.(#12748) * Add UT.(#12748) 24 天前
b04d2266 28 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐