Nacos2.1.0适配瀚高数据库
1.准备工作
1.1.nacos官方文档
https://nacos.io/zh-cn/docs/quick-start.html
1.2.下载nacos源码
https://github.com/alibaba/nacos.git
1.3.导入IDEA并编译
下载后,导入IDEA,配置好JDK和MAVEN环境,等待项目加载完成。
加载完成后编译整个项目。
2.修改nacos源码
2.1.引入驱动依赖
修改项目根目录下nacos-all的pom.xml
<dependency>
<groupId>com.highgo</groupId>
<artifactId>HgdbJdbc</artifactId>
<version>6.2.2</version>
</dependency>
2.2.引用数据库
在 nacos-config 模块直接引用驱动库,修改 nacos-config的pom.xml
<dependency>
<groupId>com.highgo</groupId>
<artifactId>HgdbJdbc</artifactId>
</dependency>
2.3.修改配置
nacos-console模块的application.properties
#瀚高数据库
spring.datasource.platform=highgo
db.jdbcDriverName=com.highgo.jdbc.Driver
db.num=1
db.url.0=jdbc:highgo://192.168.248.109:5866/nacos
db.user.0=nacos
db.password.0=Qwer@1234
2.4.添加属性
nacos-config模块:
com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceProperties.java
增加可以通过配置文件指定数据库驱动
private String jdbcDriverName;
public String getJdbcDriverName() {
return jdbcDriverName;
}
public void setJdbcDriverName(String jdbcDriverName) {
this.jdbcDriverName = jdbcDriverName;
}
2.5.指定驱动名称
修改build方法,为 HikariDataSource 对象指定驱动名称,调动 setDriverClassName 方法
System.out.println("###########################################");
if (StringUtils.isNotEmpty(jdbcDriverName)) {
System.out.println("jdbcDriverName --- [" + jdbcDriverName+"]");
// 设置模式名
ds.setSchema("nacos");
ds.setDriverClassName(jdbcDriverName);
} else {
ds.setDriverClassName(JDBC_DRIVER_NAME);
}
System.out.println("###########################################");
修改后的方法如下:
/**
* Build serveral HikariDataSource.
*
* @param environment {@link Environment}
* @param callback Callback 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(JDBC_DRIVER_NAME);
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);
ds.setIdleTimeout(TimeUnit.MINUTES.toMillis(10L));
ds.setConnectionTimeout(TimeUnit.SECONDS.toMillis(3L));
//自定义对瀚高数据库的支持
System.out.println("###########################################");
if (StringUtils.isNotEmpty(jdbcDriverName)) {
System.out.println("jdbcDriverName --- [" + jdbcDriverName+"]");
// 设置模式名
ds.setSchema("nacos");
ds.setDriverClassName(jdbcDriverName);
} else {
ds.setDriverClassName(JDBC_DRIVER_NAME);
}
System.out.println("###########################################");
dataSources.add(ds);
callback.accept(ds);
}
Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available");
return dataSources;
}
2.6.定义数据库类型
修改 nacos-config模块下的PropertiesConstant
com/alibaba/nacos/config/server/constant/PropertiesConstant.java
public static final String HIGHGO = "highgo";
2.7.配置生效
修改 nacos-config模块下的PropertyUtil中的loadSetting方法
com/alibaba/nacos/config/server/utils/PropertyUtil.java
setUseExternalDB(PropertiesConstant.HIGHGO
.equalsIgnoreCase(getString(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "")));
3.导入瀚高脚本
-- 创建nacos用户
CREATE USER nacos WITH PASSWORD 'Qwer@1234';
-- 创建nacos数据库
CREATE DATABASE nacos OWNER 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(50) 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,
encrypted_data_key text NOT NULL,
PRIMARY KEY (id),
constraint uk_configinfo_datagrouptenant unique(data_id,group_id,tenant_id)
);
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)
);
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(50) DEFAULT NULL,
tenant_id varchar(128) DEFAULT '',
encrypted_data_key text NOT NULL,
PRIMARY KEY (id),
constraint uk_configinfobeta_datagrouptenant unique(data_id,group_id,tenant_id)
);
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(50) DEFAULT NULL,
PRIMARY KEY (id),
constraint uk_configinfotag_datagrouptenanttag unique(data_id,group_id,tenant_id,tag_id)
);
CREATE TABLE config_tags_relation (
id serial 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)
);
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)
);
CREATE TABLE his_config_info (
id serial NOT NULL,
nid 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,
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(50) DEFAULT NULL,
op_type char(10) DEFAULT NULL,
tenant_id varchar(128) DEFAULT '',
encrypted_data_key text NOT NULL,
PRIMARY KEY (nid)
);
CREATE TABLE tenant_capacity (
id serial NOT NULL,
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)
);
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)
);
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
);
CREATE TABLE permissions (
role varchar(50) NOT NULL,
resource varchar(255) NOT NULL,
action varchar(8) NOT NULL
);
INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);
INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');
4.启动nacos
#启动的时候我们需要改下启动参数来表明我们是单机启动的
-Dnacos.standalone=true
5.本地访问
http://localhost:8848/nacos/index.html
用户名nacos
密码nacos
6.重新打包
mvn -Prelease-nacos -Dmaven.test.skip=true -Dpmd.skip=true -Dcheckstyle.skip=true clean install -U
运行成功后在nacos\distribution\target目录下生成对应的打包文件