logo
GBase 8s
适配迁移
文章

【Nacos适配】Nacos 2.2.3版本适配GBase8s数据库

snanjib
发表于2024-01-05 17:41:06771次浏览1个评论

1.获取nacos源码

Nacos 2.2.3源码地址

2.修改代码

①添加GBase8s JDBC驱动依赖

在父项目nacos-all与子项目nacos-configpom.xml中添加GBase8s驱动依赖,驱动版本需与使用的数据库版本对应。(需要自己安装到本地Maven仓库)

            <dependency>
                <groupId>com.gbasedbt</groupId>
                <artifactId>jdbc</artifactId>
                <version>3.5.0_2</version>
            </dependency>

②修改nacos-config项目

com.alibaba.nacos.config.server.constant.PropertiesConstant

添加以下代码:

public static final String GBASEDBT = "gbasedbt";

com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceProperties

添加变量并修改build方法:

private String jdbcDriverName;


List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback)方法:
...
            if (StringUtils.isEmpty(poolProperties.getDataSource().getDriverClassName())) {
                if (StringUtils.isNotEmpty(jdbcDriverName)) {
                    // 增加其他数据库驱动的支持
                    poolProperties.setDriverClassName(jdbcDriverName);
                } else {
                    //默认使用mysql驱动
                    poolProperties.setDriverClassName(JDBC_DRIVER_NAME);
                }
            }
...

③修改nacos-datasource-plugin项目

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

添加变量:

    public static final String GBASEDBT = "gbasedbt";

新建包com.alibaba.nacos.plugin.datasource.impl.gbasedbt,复制com.alibaba.nacos.plugin.datasource.impl.mysql包中所有类并重命名,后缀为相应的"ByGBasedbt"。分别编辑每个复制过来的类,将其中getDataSource()方法返回值改为:

    @Override
    public String getDataSource() {
        return DataSourceConstant.GBASEDBT;
    }

com.alibaba.nacos.plugin.datasource.impl.gbasedbt.ConfigInfoTagMapperByGBasedbt为例:

在文件META-INF/services/com.alibaba.nacos.plugin.datasource.mapper.Mapper中追加修改的类:

com.alibaba.nacos.plugin.datasource.impl.gbasedbt.ConfigInfoAggrMapperByGBasedbt
com.alibaba.nacos.plugin.datasource.impl.gbasedbt.ConfigInfoBetaMapperByGBasedbt
com.alibaba.nacos.plugin.datasource.impl.gbasedbt.ConfigInfoMapperByGbasedbt
com.alibaba.nacos.plugin.datasource.impl.gbasedbt.ConfigInfoTagMapperByGBasedbt
com.alibaba.nacos.plugin.datasource.impl.gbasedbt.ConfigTagsRelationMapperByGBasedbt
com.alibaba.nacos.plugin.datasource.impl.gbasedbt.GroupCapacityMapperByGBasedbt
com.alibaba.nacos.plugin.datasource.impl.gbasedbt.HistoryConfigInfoMapperByGBasedbt
com.alibaba.nacos.plugin.datasource.impl.gbasedbt.TenantCapacityMapperByGBasedbt
com.alibaba.nacos.plugin.datasource.impl.gbasedbt.TenantInfoMapperByGBasedbt

④修改``nacos-distribution``项目(可选)

distribution/conf添加GBase8s数据库SQL脚本,打包时会出现在conf目录中。脚本内容详见附录1

3.Maven打包

Maven命令:

mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U -Dcheckstyle.skip=true -Dpmd.skip=true

4.Nacos部署

解压后获得以下目录结构:

①修改配置文件:

修改conf目录中application.properties

(1)修改spring.datasource.platform

修改spring.datasource.platform参数,此处应与com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant#GBASEDBT的值一致:

spring.datasource.platform=gbasedbt

(2)修改连接串参数

连接串数据库应为nacos_db,与sql脚本中一致:

### Connect URL of DB:
db.url.0=jdbc:gbasedbt-sqli://ip:port/nacos_db:GBASEDBTSERVER=xxx;
db.user=xxx
db.password=xxx

(3)添加连接池参数

db.pool.config.driver-class-name指定了连接所使用的驱动类,如果不进行配置,默认会使用MySQL驱动。

db.pool.config.connection-test-query由于默认值为"select 1",使用GBase8s时会发生语法错误。

db.pool.config.driver-class-name=com.gbasedbt.jdbc.Driver
db.pool.config.connection-test-query=select 1 from dual

②运行SQL脚本:

gbase8s-schema.sql会创建nacos_db数据库并准备nacos运行所需的表结构。sql脚本见附录1

③启停Nacos:

启动单机模式:

sh startup.sh -m standalone

关闭服务:

sh shutdown.sh

附录

附录1:GBase8s数据库SQL脚本

database sysmaster;

drop
	database if exists nacos_db;

create
	database nacos_db with log;

database nacos_db;

/******************************************/
/*   数据库全名 = nacos_db   */
/*   表名称 = config_info   */
/******************************************/
drop
	table
		if exists config_info;

create
	table
		config_info(
			id bigserial not null,
			data_id varchar(255) not null,
			group_id varchar(128) default null,
			content lvarchar(2000) not null,
			md5 varchar(32) default null,
			gmt_create datetime year to second default current year to second not null,
			gmt_modified datetime year to second default current year to second not null,
			src_user varchar(128) default null,
			src_ip varchar(50) default null,
			app_name varchar(128),
			tenant_id varchar(128) default '',
			c_desc varchar(256) default null,
			c_use varchar(64) default null,
			effect varchar(64) default null,
			type varchar(64) default null,
			c_schema lvarchar(2000),
			encrypted_data_key lvarchar(2000) default null
		);

comment on
table
	config_info is 'config_info';

comment on
column config_info.id is 'id';

comment on
column config_info.data_id is 'data_id';

comment on
column config_info.content is 'content';

comment on
column config_info.md5 is 'md5';

comment on
column config_info.gmt_create is '创建时间';

comment on
column config_info.gmt_modified is '修改时间';

comment on
column config_info.src_user is 'source user';

comment on
column config_info.src_ip is 'source ip';

comment on
column config_info.tenant_id is '租户字段';

comment on
column config_info.encrypted_data_key is '秘钥';

alter table
	config_info add constraint primary key(id) constraint configinfo_id_key;

alter table
	config_info add constraint unique(
		data_id,
		group_id,
		tenant_id
	) constraint uk_configinfo_datagrouptenant;

create
	index configinfo_dataid_key_idx on
	config_info(data_id);

create
	index configinfo_groupid_key_idx on
	config_info(group_id);

create
	index configinfo_dataid_group_key_idx on
	config_info(
		data_id,
		group_id
	);

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_info_aggr   */
/******************************************/
drop
	table
		if exists config_info_aggr;

create
	table
		config_info_aggr(
			id bigserial not null,
			data_id varchar(255) not null,
			group_id varchar(128) not null,
			datum_id varchar(255) not null,
			content lvarchar(2000) not null,
			gmt_modified datetime year to second default current year to second not null,
			app_name varchar(128) default null,
			tenant_id varchar(128) default ''
		);

comment on
table
	config_info_aggr is '增加租户字段';

comment on
column config_info_aggr.id is 'id';

comment on
column config_info_aggr.data_id is 'data_id';

comment on
column config_info_aggr.group_id is 'group_id';

comment on
column config_info_aggr.datum_id is 'datum_id';

comment on
column config_info_aggr.content is '内容';

comment on
column config_info_aggr.gmt_modified is '修改时间';

comment on
column config_info_aggr.tenant_id is '租户字段';

alter table
	config_info_aggr add constraint primary key(id) constraint configinfoaggr_id_key;

alter table
	config_info_aggr add constraint unique(
		data_id,
		group_id,
		tenant_id
	) constraint uk_configinfoaggr_datagrouptenantdatum;

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_info_beta   */
/******************************************/
drop
	table
		if exists config_info_beta;

create
	table
		config_info_beta(
			id bigserial not null,
			data_id varchar(255) not null,
			group_id varchar(128) not null,
			app_name varchar(128) default null,
			content lvarchar(2000) not null,
			beta_ips varchar(1024) default null,
			md5 varchar(32) default null,
			gmt_create datetime year to second default current year to second not null,
			gmt_modified datetime year to second default current year to second not null,
			src_user lvarchar(2000),
			src_ip varchar(50) default null,
			tenant_id varchar(128) default '',
			encrypted_data_key lvarchar(2000) default null
		);

alter table
	config_info_beta add constraint primary key(id) constraint configinfobeta_id_key;

alter table
	config_info_beta add constraint unique(
		data_id,
		group_id,
		tenant_id
	) constraint uk_configinfobeta_datagrouptenant;

comment on
table
	config_info_beta is 'config_info_beta';

comment on
column config_info_beta.id is 'id';

comment on
column config_info_beta.data_id is 'data_id';

comment on
column config_info_beta.group_id is 'group_id';

comment on
column config_info_beta.app_name is 'app_name';

comment on
column config_info_beta.content is 'content';

comment on
column config_info_beta.beta_ips is 'betaIps';

comment on
column config_info_beta.md5 is 'md5';

comment on
column config_info_beta.gmt_create is '创建时间';

comment on
column config_info_beta.gmt_modified is '修改时间';

comment on
column config_info_beta.src_user is 'source user';

comment on
column config_info_beta.src_ip is 'source ip';

comment on
column config_info_beta.tenant_id is '租户字段';

comment on
column config_info_beta.encrypted_data_key is '秘钥';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_info_tag   */
/******************************************/
drop
	table
		if exists config_info_tag;

create
	table
		config_info_tag(
			id bigserial 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) default null,
			content lvarchar(2000) not null,
			md5 varchar(32) default null,
			gmt_create datetime year to second default current year to second not null,
			gmt_modified datetime year to second default current year to second not null,
			src_user lvarchar(2000),
			src_ip varchar(50) default null
		);

alter table
	config_info_tag add constraint primary key(id) constraint configinfotag_id_key;

alter table
	config_info_tag add constraint unique(
		data_id,
		group_id,
		tenant_id
	) constraint uk_configinfotag_datagrouptenanttag;

comment on
table
	config_info_tag is 'config_info_tag';

comment on
column config_info_tag.id is 'id';

comment on
column config_info_tag.data_id is 'data_id';

comment on
column config_info_tag.group_id is 'group_id';

comment on
column config_info_tag.tenant_id is 'tenant_id';

comment on
column config_info_tag.tag_id is 'tag_id';

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.md5 is 'md5';

comment on
column config_info_tag.gmt_create is '创建时间';

comment on
column config_info_tag.gmt_modified is '修改时间';

comment on
column config_info_tag.src_user is 'source user';

comment on
column config_info_tag.src_ip is 'source ip';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_tags_relation   */
/******************************************/
drop
	table
		if exists config_tags_relation;

create
	table
		config_tags_relation(
			id bigint not null,
			tag_name varchar(128) not null,
			tag_type varchar(64) default null,
			data_id varchar(255) not null,
			group_id varchar(128) not null,
			tenant_id varchar(128) default '',
			nid bigserial not null
		);

alter table
	config_tags_relation add constraint primary key(nid) constraint config_tags_relation_nid_key;

alter table
	config_tags_relation add constraint unique(
		id,
		tag_name,
		tag_type
	) constraint uk_configtagrelation_configidtag;

create
	index config_tags_tenant_id_idx on
	config_tags_relation(tenant_id);

comment on
table
	config_tags_relation is 'config_tag_relation';

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.data_id is 'data_id';

comment on
column config_tags_relation.group_id is 'group_id';

comment on
column config_tags_relation.tenant_id is 'tenant_id';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = group_capacity   */
/******************************************/
drop
	table
		if exists group_capacity;

create
	table
		group_capacity(
			id bigserial not null,
			group_id varchar(128) not null default '',
			quota int not null default 0,
			usage int not null default 0,
			max_size int not null default 0,
			max_aggr_count int not null default 0,
			max_aggr_size int not null default 0,
			max_history_count int not null default 0,
			gmt_create datetime year to second not null default current year to second,
			gmt_modified datetime year to second not null default current year to second
		);

alter table
	group_capacity add constraint primary key(id) constraint group_capacity_id_key;

alter table
	group_capacity add constraint unique(group_id) constraint uk_group_id;

comment on
table
	group_capacity is '集群、各Group容量信息表';

comment on
column group_capacity.id is '主键ID';

comment on
column group_capacity.group_id is 'Group ID,空字符表示整个集群';

comment on
column group_capacity.quota is '配额,0表示使用默认值';

comment on
column group_capacity.usage is '使用量';

comment on
column group_capacity.max_size is '单个配置大小上限,单位为字节,0表示使用默认值';

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.gmt_create is '创建时间';

comment on
column group_capacity.gmt_modified is '修改时间';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = his_config_info   */
/******************************************/
drop
	table
		if exists his_config_info;

create
	table
		his_config_info(
			id bigint not null,
			nid bigserial not null,
			data_id varchar(255) not null,
			group_id varchar(128) not null,
			app_name varchar(128) default null,
			content lvarchar(2000) not null,
			md5 varchar(32) default null,
			gmt_create datetime year to second default current year to second not null,
			gmt_modified datetime year to second default current year to second not null,
			src_user lvarchar(2000),
			src_ip varchar(50) default null,
			op_type char(10) default null,
			tenant_id varchar(128) default '',
			encrypted_data_key lvarchar(2000) default null
		);

alter table
	his_config_info add constraint primary key(nid) constraint hisconfiginfo_nid_key;

create
	index hisconfiginfo_dataid_key_idx on
	his_config_info(data_id);

create
	index hisconfiginfo_gmt_create_idx on
	his_config_info(gmt_create);

create
	index hisconfiginfo_gmt_modified_idx on
	his_config_info(gmt_modified);

comment on
table
	his_config_info is '多租户改造';

comment on
column his_config_info.app_name is 'app_name';

comment on
column his_config_info.tenant_id is '租户字段';

comment on
column his_config_info.encrypted_data_key is '秘钥';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = tenant_capacity   */
/******************************************/
drop
	table
		if exists tenant_capacity;

create
	table
		tenant_capacity(
			id bigserial not null,
			tenant_id varchar(128) not null default '',
			quota int not null default 0,
			usage int not null default 0,
			max_size int not null default 0,
			max_aggr_count int not null default 0,
			max_aggr_size int not null default 0,
			max_history_count int not null default 0,
			gmt_create datetime year to second default current year to second not null,
			gmt_modified datetime year to second default current year to second not null
		);

alter table
	tenant_capacity add constraint primary key(id) constraint tenant_capacity_id_key;

alter table
	tenant_capacity add constraint unique(tenant_id) constraint uk_tenant_id;

comment on
table
	tenant_capacity is '租户容量信息表';

comment on
column tenant_capacity.id is '主键ID';

comment on
column tenant_capacity.tenant_id is 'Tenant ID';

comment on
column tenant_capacity.quota is '配额,0表示使用默认值';

comment on
column tenant_capacity.usage is '使用量';

comment on
column tenant_capacity.max_size is '单个配置大小上限,单位为字节,0表示使用默认值';

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.gmt_create is '创建时间';

comment on
column tenant_capacity.gmt_modified is '修改时间';

drop
	table
		if exists tenant_info;

create
	table
		tenant_info(
			id bigserial not null,
			kp varchar(128) not null,
			tenant_id varchar(128) not null default '',
			tenant_name varchar(128) default '',
			tenant_desc varchar(256) default null,
			create_source varchar(32) default null,
			gmt_create bigint not null,
			gmt_modified bigint not null
		);

alter table
	tenant_info add constraint primary key(id) constraint tenant_info_id_key;

alter table
	tenant_info add constraint unique(
		kp,
		tenant_id
	) constraint uk_tenant_info_kptenantid;

create
	index tenant_info_tenant_id_idx on
	tenant_info(tenant_id);

comment on
table
	tenant_info is 'tenant_info';

comment on
column tenant_info.id is 'id';

comment on
column tenant_info.kp is 'kp';

comment on
column tenant_info.tenant_id is 'tenant_id';

comment on
column tenant_info.tenant_name is 'tenant_name';

comment on
column tenant_info.tenant_desc is 'tenant_desc';

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 '修改时间';

drop
	table
		if exists users;

create
	table
		users(
			username varchar(50) not null,
			password varchar(500) not null,
			enabled boolean not null
		);

alter table
	users add constraint primary key(username) constraint users_username_key;

drop
	table
		if exists roles;

create
	table
		roles(
			username varchar(50) not null,
			role varchar(50) not null
		);

alter table
	roles add constraint unique(
		username,
		role
	) constraint uk_username_role;

drop
	table
		if exists permissions;

create
	table
		permissions(
			role varchar(50) not null,
			resource varchar(512) not null,
			action varchar(8) not null
		);

alter table
	permissions add constraint unique(
		role,
		resource,
		action
	) constraint uk_role_permission;

insert
	into
		users(
			username,
			password,
			enabled
		)
	values(
		'nacos',
		'$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu',
		't'
	);

insert
	into
		roles(
			username,
			role
		)
	values(
		'nacos',
		'ROLE_ADMIN'
	);

 

 

 

评论

登录后才可以发表评论
GBase用户26794发表于 2 个月前
这个表结构 文件 我的怎么不自动生成