修改nacos源码持久化到postgreSQL

修改nacos源码持久化到postgreSQL

项目使用 Nacos 作为注册中心和配置中心,要求使用 PostgreSQL,Nacos 官方仅支持 MySQL,需要对源码进行修改。

一、拉取源码

到nacos官网拉取对应版本的源码,我这里使用的2.0.1版本

https://github.com/alibaba/nacos/releases 

 

二、修改配置文件

修改console模块中的application.properties配置文件

代码如下(示例):

#* postgresql数据库的支持 #

spring.datasource.platform=postgresql

db.num=1

db.url.0=jdbc:postgresql://127.0.0.1:5432/nacos_config

db.user.0=postgres

db.password.0=postgres

# postgresql数据库的支持 *#

三、引入postgre驱动

1.修改pom

nacos-all中添加依赖

<dependency>

<groupId>org.postgresql</groupId>

<artifactId>postgresql</artifactId>

<version>42.2.14</version>

</dependency>

naming模块中添加依赖:

<dependency>

<groupId>org.postgresql</groupId>

<artifactId>postgresql</artifactId>

</dependency>

config模块中添加依赖:

<dependency>

<groupId>org.postgresql</groupId>

<artifactId>postgresql</artifactId>

</dependency>

2.修改驱动加载相关类

ExternalDataSourceProperties.java

poolProperties.setDriverClassName(JDBC_DRIVER_NAME);

//修改为postgresql加载

String driverClassName = JDBC_DRIVER_NAME;

if("postgresql".equals(EnvUtil.getProperty("spring.datasource.platform"))){

driverClassName = "org.postgresql.Driver";

}

poolProperties.setDriverClassName(driverClassName);

四、修改配置相关类

PropertyUtil.java

// External data sources are used by default in cluster mode

setUseExternalDB("mysql".equalsIgnoreCase(getString("spring.datasource.platform", "")));

// 修改为支持postgresql

String platfrom = getString("spring.datasource.platform", "");

setUseExternalDB("mysql".equalsIgnoreCase(platfrom) || "postgresql".equalsIgnoreCase(platfrom));

StartingApplicationListener.java

boolean useExternalStorage = ("mysql".equalsIgnoreCase(env.getProperty("spring.datasource.platform", "")));

// 修改为支持postgresql

String platform = env.getProperty("spring.datasource.platform", "");

boolean useExternalStorage = ("mysql".equalsIgnoreCase(platform) || "postgresql".equalsIgnoreCase(platform));

五、修改SQL

修改获取返回主键ID,GroupCapacityPersistService.java、TenantCapacityPersistService.java、ExternalStoragePersistServiceImpl.java

PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

// 修改为支持postgresql

PreparedStatement ps = connection.prepareStatement(sql, new String[]{"id"});

ExternalStoragePaginationHelperImpl.java

selectSql = sqlFetchRows + " limit " + startRow + "," + pageSize;

// 修改为支持postgresql

selectSql = sqlFetchRows + " limit " + pageSize + " offset " + startRow;

修改角色模糊查询ExternalRolePersistServiceImpl.java

@Override

public List<String> findRolesLikeRoleName(String role) {

String sql = "SELECT role FROM roles WHERE role like '%" + role + "%'";

List<String> users = this.jt.queryForList(sql, null, String.class);

return users;

}

修改用户模糊查询ExternalUserPersistServiceImpl.java

@Override

public List<String> findUserLikeUsername(String username) {

String sql = "SELECT username FROM users WHERE username like '%" + username + "%'";

List<String> users = this.jt.queryForList(sql, null, String.class);

return users;

}

六、利用工具全局搜索,将所有limit ?,? 替换为 offset ? limit ?

String sqlFetchRows = " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5 "

+ " FROM ( SELECT id FROM config_info WHERE tenant_id like ? ORDER BY id offset ? limit ? )"

+ " g, config_info t WHERE g.id = t.id ";

七、PostgreSQL脚本

数据库创建nacos_config库,并执行文末的sql脚本。

放到文章最后!!!!!!!!!!!

注意修改sql的时候,千万别修改到 EmbeddedStoragePersistServiceImpl 类中的sql。

八、修改后的源码打包

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

执行该命名,直接打包会有很多报错的地方。

九、源码编译的过程中如果报错,安装一个protobuf的插件

十、本地启动源码

可以通过压缩包的形式下载或是直接使用git clone指令下载:

 

# 通过git指令下载源码

git clone https://github.com/alibaba/nacos.git

下载后使用IDEA打开项目如下图:

IDEA打开Nacos源码项目

使用Maven进行编译

 

# clean & install & skip test

mvn clean install -Dmaven.test.skip=true

这之前可能会需要你配置一下JDK,如下图:

配置JDK

PS:JDK1.8或更高版本即可

安装完成

运行Nacos

找到console模块下的Nacos文件运行一下

直接运行

运行后发现会提示错误,如下图:

修改VM options项,

编辑配置

输入参数

 

# 以单机模式启动,-Dnacos.home为distribution模块所在目录

-Dnacos.standalone=true -Dnacos.home=C:\myworkspace\JcWorkSpace\nacos-2.0.1\distribution

重新运行,启动成功:

启动成功

访问管理页面:

管理页面

帐密都是nacos,登入:

十一、PG初始化脚本

 

CREATE DATABASE IF NOT EXISTS mse_nacos encoding='UTF8' ;

use mse_nacos;

CREATE TABLE config_info (

id serial NOT NULL,

data_id varchar(255) NOT NULL ,

group_id varchar(255) DEFAULT NULL,

content text NOT NULL ,

md5 varchar(32) DEFAULT NULL ,

gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00' ,

gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00' ,

src_user text ,

src_ip varchar(20) DEFAULT NULL ,

app_name varchar(128) DEFAULT NULL,

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 text,

PRIMARY KEY (id),

constraint uk_configinfo_datagrouptenant unique(data_id,group_id,tenant_id)

) ;

comment on table config_info is 'config_info';

comment on column config_info.id is '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 '租户字段';

CREATE TABLE config_info_aggr (

id serial NOT NULL,

data_id varchar(255) NOT NULL ,

group_id varchar(255) NOT NULL ,

datum_id varchar(255) NOT NULL ,

content text NOT NULL ,

gmt_modified timestamp NOT NULL ,

app_name varchar(128) DEFAULT NULL,

tenant_id varchar(128) DEFAULT '' ,

PRIMARY KEY (id),

constraint uk_configinfoaggr_datagrouptenantdatum unique(data_id,group_id,tenant_id,datum_id)

) ;

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 '租户字段';

CREATE TABLE config_info_beta (

id serial NOT NULL,

data_id varchar(255) NOT NULL ,

group_id varchar(128) NOT NULL ,

app_name varchar(128) DEFAULT NULL ,

content text NOT NULL ,

beta_ips varchar(1024) DEFAULT NULL ,

md5 varchar(32) DEFAULT NULL ,

gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',

gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',

src_user text ,

src_ip varchar(20) DEFAULT NULL ,

tenant_id varchar(128) DEFAULT '',

PRIMARY KEY (id),

constraint uk_configinfobeta_datagrouptenant unique(data_id,group_id,tenant_id)

);

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 '租户字段';

CREATE TABLE config_info_tag (

id serial 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 text NOT NULL ,

md5 varchar(32) DEFAULT NULL ,

gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',

gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00' ,

src_user text ,

src_ip varchar(20) DEFAULT NULL ,

PRIMARY KEY (id),

constraint uk_configinfotag_datagrouptenanttag unique(data_id,group_id,tenant_id,tag_id)

) ;

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';

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

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 serial NOT NULL,

PRIMARY KEY (nid),

constraint uk_configtagrelation_configidtag unique(id,tag_name,tag_type)

) ;

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';

CREATE TABLE group_capacity (

id serial NOT NULL ,

group_id varchar(128) NOT NULL DEFAULT '',

quota int NOT NULL DEFAULT '0' CHECK (quota >= 0) ,

usage int NOT NULL DEFAULT '0' CHECK (usage >= 0),

max_size int NOT NULL DEFAULT '0' CHECK (max_size >= 0),

max_aggr_count int NOT NULL DEFAULT '0' CHECK (max_aggr_count >= 0),

max_aggr_size int NOT NULL DEFAULT '0' CHECK (max_aggr_size >= 0),

max_history_count int NOT NULL DEFAULT '0' CHECK (max_history_count >= 0),

gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00' ,

gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00' ,

PRIMARY KEY (id),

constraint uk_group_id unique(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 '修改时间';

CREATE TABLE his_config_info (

id bigint NOT NULL CHECK (id >= 0),

nid serial NOT NULL CHECK (nid >= 0),

data_id varchar(255) NOT NULL ,

group_id varchar(128) NOT NULL,

app_name varchar(128) DEFAULT NULL ,

content text NOT NULL,

md5 varchar(32) DEFAULT NULL,

gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',

gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',

src_user text,

src_ip varchar(20) DEFAULT NULL,

op_type char(10) DEFAULT NULL,

tenant_id varchar(128) DEFAULT '' ,

PRIMARY KEY (nid)

);

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 '租户字段';

CREATE TABLE tenant_capacity (

id serial NOT NULL CHECK (id >= 0),

tenant_id varchar(128) NOT NULL DEFAULT '',

quota int NOT NULL DEFAULT '0' CHECK (quota >= 0),

usage int NOT NULL DEFAULT '0' CHECK (usage >= 0),

max_size int NOT NULL DEFAULT '0' CHECK (max_size >= 0),

max_aggr_count int NOT NULL DEFAULT '0' CHECK (max_aggr_count >= 0),

max_aggr_size int NOT NULL DEFAULT '0' CHECK (max_aggr_size >= 0),

max_history_count int NOT NULL DEFAULT '0' CHECK (max_history_count >= 0),

gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',

gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00' ,

PRIMARY KEY (id),

constraint uk_tenant_id unique(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 '修改时间';

CREATE TABLE tenant_info (

id serial NOT NULL ,

kp varchar(128) NOT NULL,

tenant_id varchar(128) 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 ,

PRIMARY KEY (id),

constraint uk_tenant_info_kptenantid unique(kp,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 '修改时间';

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

);

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

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

参考: https://www.jianshu.com/p/8e5f876c81ea

https://blog.51cto.com/u_15257216/3019791

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值