修改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数据库的支持 *#
nacos-all中添加依赖
naming模块中添加依赖:
config模块中添加依赖:
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));
修改获取返回主键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 sql = "SELECT role FROM roles WHERE role like '%" + role + "%'";
List
return users;
}
修改用户模糊查询ExternalUserPersistServiceImpl.java
@Override
public List
String sql = "SELECT username FROM users WHERE username like '%" + username + "%'";
List
return users;
}
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 ";
数据库创建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
执行该命名,直接打包会有很多报错的地方。
十、本地启动源码
可以通过压缩包的形式下载或是直接使用git clone指令下载:
# 通过git指令下载源码
git clone https://github.com/alibaba/nacos.git
下载后使用IDEA打开项目如下图:
IDEA打开Nacos源码项目
# clean & install & skip test
mvn clean install -Dmaven.test.skip=true
这之前可能会需要你配置一下JDK,如下图:
配置JDK
PS:JDK1.8或更高版本即可
安装完成
找到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