Nacos篇

  • Nacos适配达梦数据库
    • 前期准备
    • 修改流程
    • 启动测试
    • 语法适配
    • 源码打包
    • 踩坑

Nacos适配达梦数据库

Nacos提供了两种存储方式文件存储、mysql存储,默认使用的是文件存储的方式,在实际开发中我们可能需要使用其它数据库,比如达梦、人大金仓等,就需要我们自己对Nacos源码进行修改适配,本文以达梦数据库展开,教你如何进行源码修改。

前期准备

首先我们需要Nacos源码,可以去github-nacos或者gitee-nacos进行下载,
本文以2.1.2版本为例,其它版本类似,稍有不同之处请参考其他博主教程。

修改流程

  1. 初始化项目
  2. 引入达梦数据库驱动;
  • 修改父工程pom文件,引入版本号
<dm-connector-java.version>8.1.1.193</dm-connector-java.version>
  • 修改三个地方的pom文件依赖(父工程、nacos-config、nacos-naming)注释mysql驱动,添加达梦数据库驱动
  • 子模块版本号可以不写
<!--<dependency>--><!-- <groupId>mysql</groupId>--><!-- <artifactId>mysql-connector-java</artifactId>--><!-- <version>${mysql-connector-java.version}</version>--><!-- </dependency>--><dependency><groupId>com.dameng</groupId><artifactId>Dm8JdbcDriver18</artifactId><version>${dm-connector-java.version}</version></dependency>
  1. 修改达梦数据库连接参数配置文件nacos-console模块的application.properties

  • 达梦数据库sql如下
--创建表空间CREATE TABLESPACE"NACOS" DATAFILE 'NACOS.DBF' SIZE 300 autoextend on maxsize 67108863 CACHE = NORMAL;--创建用户CREATE USER "NACOS" IDENTIFIED BY "NACOS!123" LIMIT PASSWORD_LOCK_TIME 1, PASSWORD_GRACE_TIME 10 DEFAULT TABLESPACE "NACOS";--给用户授权GRANT "RESOURCE","PUBLIC","DBA","VTI" TO "NACOS";
CREATE TABLE "NACOS"."CONFIG_INFO"("ID" BIGINT IDENTITY(1, 1) NOT NULL,"DATA_ID" VARCHAR(255) NOT NULL,"GROUP_ID" VARCHAR(255),"CONTENT" CLOB NOT NULL,"MD5" VARCHAR(32),"GMT_CREATE" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() NOT NULL,"GMT_MODIFIED" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() NOT NULL,"SRC_USER" TEXT,"SRC_IP" VARCHAR(50),"APP_NAME" VARCHAR(128),"TENANT_ID" VARCHAR(128) DEFAULT '',"C_DESC" VARCHAR(256),"C_USE" VARCHAR(64),"EFFECT" VARCHAR(64),"TYPE" VARCHAR(64),"C_SCHEMA" TEXT,"ENCRYPTED_DATA_KEY" TEXT NOT NULL,NOT CLUSTER PRIMARY KEY("ID"),CONSTRAINT "UK_CONFIGINFO_DATAGROUPTENANT" UNIQUE("DATA_ID", "GROUP_ID", "TENANT_ID")) STORAGE(ON "nacos_db", CLUSTERBTR) ; COMMENT ON TABLE "NACOS"."CONFIG_INFO" IS 'config_info';COMMENT ON COLUMN "NACOS"."CONFIG_INFO"."ID" IS 'id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO"."DATA_ID" IS 'data_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO"."CONTENT" IS 'content';COMMENT ON COLUMN "NACOS"."CONFIG_INFO"."MD5" IS 'md5';COMMENT ON COLUMN "NACOS"."CONFIG_INFO"."GMT_CREATE" IS '创建时间';COMMENT ON COLUMN "NACOS"."CONFIG_INFO"."GMT_MODIFIED" IS '修改时间';COMMENT ON COLUMN "NACOS"."CONFIG_INFO"."SRC_USER" IS 'source user';COMMENT ON COLUMN "NACOS"."CONFIG_INFO"."SRC_IP" IS 'source ip';COMMENT ON COLUMN "NACOS"."CONFIG_INFO"."TENANT_ID" IS '租户字段';CREATE UNIQUEINDEX "INDEX16097400477400" ON "NACOS"."CONFIG_INFO"("ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."CONFIG_INFO_AGGR"("ID" BIGINT IDENTITY(1, 1) NOT NULL,"DATA_ID" VARCHAR(255) NOT NULL,"GROUP_ID" VARCHAR(255) NOT NULL,"DATUM_ID" VARCHAR(255) NOT NULL,"CONTENT" CLOB NOT NULL,"GMT_MODIFIED" TIMESTAMP(0) NOT NULL,"APP_NAME" VARCHAR(128),"TENANT_ID" VARCHAR(128) DEFAULT '',NOT CLUSTER PRIMARY KEY("ID"),CONSTRAINT "UK_CONFIGINFOAGGR_DATAGROUPTENANTDATUM" UNIQUE("DATA_ID", "GROUP_ID", "TENANT_ID", "DATUM_ID")) STORAGE(ON "nacos_db", CLUSTERBTR) ; COMMENT ON TABLE "NACOS"."CONFIG_INFO_AGGR" IS '增加租户字段';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_AGGR"."ID" IS 'id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_AGGR"."DATA_ID" IS 'data_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_AGGR"."GROUP_ID" IS 'group_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_AGGR"."DATUM_ID" IS 'datum_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_AGGR"."CONTENT" IS '内容';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_AGGR"."GMT_MODIFIED" IS '修改时间';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_AGGR"."TENANT_ID" IS '租户字段';CREATE UNIQUEINDEX "INDEX16097305729100" ON "NACOS"."CONFIG_INFO_AGGR"("ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."CONFIG_INFO_BETA"("ID" BIGINT IDENTITY(1, 1) NOT NULL,"DATA_ID" VARCHAR(255) NOT NULL,"GROUP_ID" VARCHAR(128) NOT NULL,"APP_NAME" VARCHAR(128),"CONTENT" CLOB NOT NULL,"BETA_IPS" VARCHAR(1024),"MD5" VARCHAR(32),"GMT_CREATE" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() NOT NULL,"GMT_MODIFIED" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() NOT NULL,"SRC_USER" TEXT,"SRC_IP" VARCHAR(50),"TENANT_ID" VARCHAR(128) 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_db", CLUSTERBTR) ; COMMENT ON TABLE "NACOS"."CONFIG_INFO_BETA" IS 'config_info_beta';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."ID" IS 'id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."DATA_ID" IS 'data_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."GROUP_ID" IS 'group_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."APP_NAME" IS 'app_name';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."CONTENT" IS 'content';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."BETA_IPS" IS 'betaIps';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."MD5" IS 'md5';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."GMT_CREATE" IS '创建时间';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."GMT_MODIFIED" IS '修改时间';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."SRC_USER" IS 'source user';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."SRC_IP" IS 'source ip';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_BETA"."TENANT_ID" IS '租户字段';CREATE UNIQUEINDEX "INDEX16097203229000" ON "NACOS"."CONFIG_INFO_BETA"("ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."CONFIG_INFO_TAG"("ID" BIGINT IDENTITY(1, 1) NOT NULL,"DATA_ID" VARCHAR(255) NOT NULL,"GROUP_ID" VARCHAR(128) NOT NULL,"TENANT_ID" VARCHAR(128) DEFAULT '',"TAG_ID" VARCHAR(128) NOT NULL,"APP_NAME" VARCHAR(128),"CONTENT" CLOB NOT NULL,"MD5" VARCHAR(32),"GMT_CREATE" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() NOT NULL,"GMT_MODIFIED" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() NOT NULL,"SRC_USER" TEXT,"SRC_IP" VARCHAR(50),NOT CLUSTER PRIMARY KEY("ID"),CONSTRAINT "UK_CONFIGINFOTAG_DATAGROUPTENANTTAG" UNIQUE("DATA_ID", "GROUP_ID", "TENANT_ID", "TAG_ID")) STORAGE(ON "nacos_db", CLUSTERBTR) ; COMMENT ON TABLE "NACOS"."CONFIG_INFO_TAG" IS 'config_info_tag';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."ID" IS 'id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."DATA_ID" IS 'data_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."GROUP_ID" IS 'group_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."TENANT_ID" IS 'tenant_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."TAG_ID" IS 'tag_id';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."APP_NAME" IS 'app_name';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."CONTENT" IS 'content';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."MD5" IS 'md5';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."GMT_CREATE" IS '创建时间';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."GMT_MODIFIED" IS '修改时间';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."SRC_USER" IS 'source user';COMMENT ON COLUMN "NACOS"."CONFIG_INFO_TAG"."SRC_IP" IS 'source ip';CREATE UNIQUEINDEX "INDEX16096985471400" ON "NACOS"."CONFIG_INFO_TAG"("ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."CONFIG_TAGS_RELATION"("ID" BIGINT NOT NULL,"TAG_NAME" VARCHAR(128) NOT NULL,"TAG_TYPE" VARCHAR(64),"DATA_ID" VARCHAR(255) NOT NULL,"GROUP_ID" VARCHAR(128) NOT NULL,"TENANT_ID" VARCHAR(128) 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_db", CLUSTERBTR) ; COMMENT ON TABLE "NACOS"."CONFIG_TAGS_RELATION" IS 'config_tag_relation';COMMENT ON COLUMN "NACOS"."CONFIG_TAGS_RELATION"."ID" IS 'id';COMMENT ON COLUMN "NACOS"."CONFIG_TAGS_RELATION"."TAG_NAME" IS 'tag_name';COMMENT ON COLUMN "NACOS"."CONFIG_TAGS_RELATION"."TAG_TYPE" IS 'tag_type';COMMENT ON COLUMN "NACOS"."CONFIG_TAGS_RELATION"."DATA_ID" IS 'data_id';COMMENT ON COLUMN "NACOS"."CONFIG_TAGS_RELATION"."GROUP_ID" IS 'group_id';COMMENT ON COLUMN "NACOS"."CONFIG_TAGS_RELATION"."TENANT_ID" IS 'tenant_id';CREATEINDEX "INDEX16096890650200" ON "NACOS"."CONFIG_TAGS_RELATION"("TENANT_ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ;CREATE UNIQUEINDEX "INDEX16096909541100" ON "NACOS"."CONFIG_TAGS_RELATION"("NID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."GROUP_CAPACITY"("ID" BIGINT IDENTITY(1, 1) NOT NULL,"GROUP_ID" VARCHAR(128) 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 CURRENT_TIMESTAMP() NOT NULL,"GMT_MODIFIED" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() 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_db", CLUSTERBTR) ; COMMENT ON TABLE "NACOS"."GROUP_CAPACITY" IS '集群、各Group容量信息表';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."ID" IS '主键ID';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."GROUP_ID" IS 'Group ID,空字符表示整个集群';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."QUOTA" IS '配额,0表示使用默认值';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."USAGE" IS '使用量';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."MAX_SIZE" IS '单个配置大小上限,单位为字节,0表示使用默认值';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."MAX_AGGR_COUNT" IS '聚合子配置最大个数,,0表示使用默认值';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."MAX_AGGR_SIZE" IS '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."MAX_HISTORY_COUNT" IS '最大变更历史数量';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."GMT_CREATE" IS '创建时间';COMMENT ON COLUMN "NACOS"."GROUP_CAPACITY"."GMT_MODIFIED" IS '修改时间';CREATE UNIQUEINDEX "INDEX16096746763900" ON "NACOS"."GROUP_CAPACITY"("ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."HIS_CONFIG_INFO"("ID" BIGINT NOT NULL,"NID" BIGINT IDENTITY(1, 1) NOT NULL,"DATA_ID" VARCHAR(255) NOT NULL,"GROUP_ID" VARCHAR(128) NOT NULL,"APP_NAME" VARCHAR(128),"CONTENT" CLOB NOT NULL,"MD5" VARCHAR(32),"GMT_CREATE" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() NOT NULL,"GMT_MODIFIED" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() NOT NULL,"SRC_USER" TEXT,"SRC_IP" VARCHAR(50),"OP_TYPE" CHAR(10),"TENANT_ID" VARCHAR(128) DEFAULT '',"ENCRYPTED_DATA_KEY" TEXT NOT NULL,NOT CLUSTER PRIMARY KEY("NID")) STORAGE(ON "nacos_db", CLUSTERBTR) ; COMMENT ON TABLE "NACOS"."HIS_CONFIG_INFO" IS '多租户改造';COMMENT ON COLUMN "NACOS"."HIS_CONFIG_INFO"."APP_NAME" IS 'app_name';COMMENT ON COLUMN "NACOS"."HIS_CONFIG_INFO"."TENANT_ID" IS '租户字段';CREATEINDEX "IDX_GMT_CREATE" ON "NACOS"."HIS_CONFIG_INFO"("GMT_CREATE" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ;CREATEINDEX "IDX_DID" ON "NACOS"."HIS_CONFIG_INFO"("DATA_ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ;CREATEINDEX "IDX_GMT_MODIFIED" ON "NACOS"."HIS_CONFIG_INFO"("GMT_MODIFIED" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ;CREATE UNIQUEINDEX "INDEX16096449256900" ON "NACOS"."HIS_CONFIG_INFO"("NID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."PERMISSIONS"("ROLE" VARCHAR(50) NOT NULL,"RESOURCE" VARCHAR(255) NOT NULL,"ACTION" VARCHAR(8) NOT NULL,CONSTRAINT "UK_ROLE_PERMISSION" UNIQUE("ROLE", "RESOURCE", "ACTION")) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."ROLES"("USERNAME" VARCHAR(50) NOT NULL,"ROLE" VARCHAR(50) NOT NULL,CONSTRAINT "IDX_USER_ROLE" UNIQUE("USERNAME", "ROLE")) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."TENANT_CAPACITY"("ID" BIGINT IDENTITY(1, 1) NOT NULL,"TENANT_ID" VARCHAR(128) 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 CURRENT_TIMESTAMP() NOT NULL,"GMT_MODIFIED" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP() 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_db", CLUSTERBTR) ; COMMENT ON TABLE "NACOS"."TENANT_CAPACITY" IS '租户容量信息表';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."ID" IS '主键ID';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."TENANT_ID" IS 'Tenant ID';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."QUOTA" IS '配额,0表示使用默认值';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."USAGE" IS '使用量';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."MAX_SIZE" IS '单个配置大小上限,单位为字节,0表示使用默认值';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."MAX_AGGR_COUNT" IS '聚合子配置最大个数';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."MAX_AGGR_SIZE" IS '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."MAX_HISTORY_COUNT" IS '最大变更历史数量';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."GMT_CREATE" IS '创建时间';COMMENT ON COLUMN "NACOS"."TENANT_CAPACITY"."GMT_MODIFIED" IS '修改时间';CREATE UNIQUEINDEX "INDEX16096046055000" ON "NACOS"."TENANT_CAPACITY"("ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."TENANT_INFO"("ID" BIGINT IDENTITY(1, 1) NOT NULL,"KP" VARCHAR(128) NOT NULL,"TENANT_ID" VARCHAR(128) DEFAULT '',"TENANT_NAME" VARCHAR(128) DEFAULT '',"TENANT_DESC" VARCHAR(256),"CREATE_SOURCE" VARCHAR(32),"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_db", CLUSTERBTR) ; COMMENT ON TABLE "NACOS"."TENANT_INFO" IS 'tenant_info';COMMENT ON COLUMN "NACOS"."TENANT_INFO"."ID" IS 'id';COMMENT ON COLUMN "NACOS"."TENANT_INFO"."KP" IS 'kp';COMMENT ON COLUMN "NACOS"."TENANT_INFO"."TENANT_ID" IS 'tenant_id';COMMENT ON COLUMN "NACOS"."TENANT_INFO"."TENANT_NAME" IS 'tenant_name';COMMENT ON COLUMN "NACOS"."TENANT_INFO"."TENANT_DESC" IS 'tenant_desc';COMMENT ON COLUMN "NACOS"."TENANT_INFO"."CREATE_SOURCE" IS 'create_source';COMMENT ON COLUMN "NACOS"."TENANT_INFO"."GMT_CREATE" IS '创建时间';COMMENT ON COLUMN "NACOS"."TENANT_INFO"."GMT_MODIFIED" IS '修改时间';CREATEINDEX "IDX_TENANT_ID" ON "NACOS"."TENANT_INFO"("TENANT_ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ;CREATE UNIQUEINDEX "INDEX16095846853300" ON "NACOS"."TENANT_INFO"("ID" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE TABLE "NACOS"."USERS"("USERNAME" VARCHAR(50) NOT NULL,"PASSWORD" VARCHAR(500) NOT NULL,"ENABLED" TINYINT NOT NULL,NOT CLUSTER PRIMARY KEY("USERNAME")) STORAGE(ON "nacos_db", CLUSTERBTR) ; CREATE UNIQUEINDEX "PRIMARY" ON "NACOS"."USERS"("USERNAME" ASC) STORAGE(ON "nacos_db", CLUSTERBTR) ;INSERT INTO nacos.users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE); INSERT INTO nacos.roles (username, role) VALUES ('nacos', 'ROLE_ADMIN'); commit;
  1. 修改nacos-config模块下的ExternalDataSourceProperties.java
  • 注释掉private static final String JDBC_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
  • 添加驱动属性private String jdbcDriverName;生成了get、set方法
  • 为DataSourcePoolProperties指定驱动名称poolProperties.setDriverClassName(jdbcDriverName);
    全部代码如下:
public class ExternalDataSourceProperties {//private static final String JDBC_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";private String jdbcDriverName;private static final String TEST_QUERY = "SELECT 1";private Integer num;private List<String> url = new ArrayList<>();private List<String> user = new ArrayList<>();private List<String> password = new ArrayList<>();public void setNum(Integer num) {this.num = num;}public void setUrl(List<String> url) {this.url = url;}public void setUser(List<String> user) {this.user = user;}public void setPassword(List<String> password) {this.password = password;}public String getJdbcDriverName() {return jdbcDriverName;}public void setJdbcDriverName(String jdbcDriverName) {this.jdbcDriverName = jdbcDriverName;}/** * Build serveral HikariDataSource. * * @param environment {@link Environment} * @param callbackCallback function when constructing data source * @return List of {@link HikariDataSource} */List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback) {List<HikariDataSource> dataSources = new ArrayList<>();Binder.get(environment).bind("db", Bindable.ofInstance(this));Preconditions.checkArgument(Objects.nonNull(num), "db.num is null");Preconditions.checkArgument(CollectionUtils.isNotEmpty(user), "db.user or db.user.[index] is null");Preconditions.checkArgument(CollectionUtils.isNotEmpty(password), "db.password or db.password.[index] is null");for (int index = 0; index < num; index++) {int currentSize = index + 1;Preconditions.checkArgument(url.size() >= currentSize, "db.url.%s is null", index);DataSourcePoolProperties poolProperties = DataSourcePoolProperties.build(environment);poolProperties.setDriverClassName(jdbcDriverName);poolProperties.setJdbcUrl(url.get(index).trim());poolProperties.setUsername(getOrDefault(user, index, user.get(0)).trim());poolProperties.setPassword(getOrDefault(password, index, password.get(0)).trim());HikariDataSource ds = poolProperties.getDataSource();ds.setConnectionTestQuery(TEST_QUERY);dataSources.add(ds);callback.accept(ds);}Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available");return dataSources;}interface Callback<D> {/** * Perform custom logic. * * @param datasource dataSource. */void accept(D datasource);}}
  1. 修改nacos-config模块下PropertiesConstant.java类

    修改public static final String MYSQL= "mysql";public static final String DM = "dm";

  2. 修改nacos-config模块下PropertyUtil.java类

setUseExternalDB(PropertiesConstant.DM.equalsIgnoreCase(getString(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "")));

  1. 修改nacos-core模块下StartingApplicationListener.java类
    private static final String DEFAULT_DATABASE = "dm";

启动测试

  1. 启动主类加入-Dnacos.standalone=true参数,以单机模式启动

    如果出现use external storage说明你全部配置正确,如果没有,请仔细检查操作步骤。

访问http://localhost:8848/nacos
测试新增数据

语法适配

  1. 测试发现:角色管理-绑定角色-动态查询用户名、权限管理-添加权限-动态查询角色名接口sql报错

    修改下列类中sql语句

  • ExternalRolePersistServiceImpl
@Overridepublic List<String> findRolesLikeRoleName(String role) {String sql = "SELECT role FROM roles WHERE role LIKE '%"+role+"%'";List<String> users = this.jt.queryForList(sql, String.class);return users;}

  • ExternalUserPersistServiceImpl
 @Overridepublic List<String> findUserLikeUsername(String username) {String sql = "SELECT username FROM users WHERE username LIKE '%"+username+"%'";List<String> users = this.jt.queryForList(sql, String.class);return users;}

  • 修改后测试结果如下,可以动态匹配成功。

    源码打包

    打包前一定要修改distribution模块下的application.properties文件,与console模块配置一样一样。这个配置就是打包完成后的配置文件。

    1. 打包项目,跳过test、pmd、checkstyle
mvn -Prelease-nacos -Dmaven.test.skip=true -Dpmd.skip=true -Dcheckstyle.skip=true clean install -U

打包成功后,压缩包在nacos-distribution模块下target目录中

踩坑

  • 可能会出现的错误,找不到Dm8JdbcDriver18jar包,原因是需要我们手动将Dm8JdbcDriver18添加到我们的本地maven库中,可以使用如下命令进行添加
mvn install:install-file -Dfile=D:\***\dm\drivers\jdbc\DmJdbcDriver18.jar -DgroupId=com.dameng -DartifactId=Dm8JdbcDriver18 -Dversion=8.1.1.193 -Dpackaging=jar

file后面改成自己的达梦安装目录下的驱动jar包,groupIdartifactIdversion请与pom中引入内容保持一致

  • No DataSource set
    一般是数据库连接配置文件出错或者是dm数据库表有问题,请从配置文件中数据库名,ip、账号、密码开始排查,然后排查dm数据库表及数据是否正确创建。