Nacos2.1.0 adapted to Henkel database

Nacos2.1.0 adapts to Hangao database

1. Preparation

1.1.nacos official document

https://nacos.io/zh-cn/docs/quick-start.html

1.2. Download nacos source code

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

1.3. Import IDEA and compile

After downloading, import IDEA, configure the JDK and MAVEN environment, and wait for the project to load.

Compile the entire project after loading is complete.

2. Modify nacos source code

2.1. Introducing driver dependencies

Modify the pom.xml of nacos-all in the project root directory

<dependency>
    <groupId>com.highgo</groupId>
    <artifactId>HgdbJdbc</artifactId>
    <version>6.2.2</version>
</dependency>

2.2. Reference database

Directly refer to the driver library in the nacos-config module, and modify the pom.xml of nacos-config

<dependency>
     <groupId>com.highgo</groupId>
     <artifactId>HgdbJdbc</artifactId>
</dependency>
2.3. Modify configuration

application.properties of the nacos-console module

#Han Gao database
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. Add attributes

nacos-config module:

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

Increase the ability to specify the database driver through the configuration file

private String jdbcDriverName;

public String getJdbcDriverName() {<!-- -->
    return jdbcDriverName;
}

public void setJdbcDriverName(String jdbcDriverName) {<!-- -->
    this.jdbcDriverName = jdbcDriverName;
}

2.5. Specify the driver name

Modify the build method, specify the driver name for the HikariDataSource object, and call the setDriverClassName method

System.out.println("######################################## ####");
if (StringUtils.isNotEmpty(jdbcDriverName)) {<!-- -->
    System.out.println("jdbcDriverName --- [" + jdbcDriverName + "]");
    // set the schema name
    ds.setSchema("nacos");
    ds.setDriverClassName(jdbcDriverName);
} else {<!-- -->
    ds.setDriverClassName(JDBC_DRIVER_NAME);
}
 System.out.println("########################################### #");

The modified method is as follows:

 /**
     * 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));
            // Customize the support for Hangao database
            System.out.println("########################################### #");
            if (StringUtils.isNotEmpty(jdbcDriverName)) {<!-- -->
                System.out.println("jdbcDriverName --- [" + jdbcDriverName + "]");
                // set the schema name
                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. Define database type

Modify the PropertiesConstant under the nacos-config module

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

public static final String HIGHGO = "highgo";
2.7. Configuration takes effect

Modify the loadSetting method in PropertyUtil under the nacos-config module

com/alibaba/nacos/config/server/utils/PropertyUtil.java

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

3. Import Hangao script

-- create nacos user
CREATE USER nacos WITH PASSWORD 'Qwer@1234';
-- Create nacos database
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. Start nacos

#When starting up, we need to change the startup parameters to show that we are starting from a single machine
-Dnacos.standalone=true


5. Local access

http://localhost:8848/nacos/index.html

username nacos

Password nacos

6. Repackage

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

After running successfully, generate the corresponding package file in the nacos\distribution\target directory