准备工作
1、下载nacos源码、编译,官网更详细
https://nacos.io/zh-cn/docs/quick-start.html
2、下载源码,按官网更详细
git地址:https://github.com/alibaba/nacos.git
3、下载达梦、人大金仓数据库驱动
我下载的是:Dm、Kingbase依赖
dm: https://download.csdn.net/download/qq_24101357/20677620
kingbase: https://download.csdn.net/download/qq_24101357/20677646
修改nacos源码
引入达梦数据库驱动依赖
nacos-all的pom.xml
<!--达梦、人大金仓-->
<dm-connector-java.version>1.8</dm-connector-java.version>
<kingbase-connector-java.version>8.2.0</kingbase-connector-java.version>
<dependency>
<groupId>com.dameng</groupId>
<artifactId>Dm8JdbcDriver18</artifactId>
<version>${dm-connector-java.version}</version>
</dependency>
<dependency>
<groupId>com.kingbase</groupId>
<artifactId>kingbase8</artifactId>
<version>${kingbase-connector-java.version}</version>
</dependency>
nacos-config的pom.xml
<dependency>
<groupId>com.dameng</groupId>
<artifactId>Dm8JdbcDriver18</artifactId>
</dependency>
<dependency>
<groupId>com.kingbase</groupId>
<artifactId>kingbase8</artifactId>
</dependency>
修改数据库配置
nacos-console模块的application.properties:
主要修改了:
1、增加了驱动db.jdbcDriverName;
2、spring.datasource.platform=mysql,现在的意思其实是是否使用外置数据库,直接mysql就等于是使用外置数据库,使用什么数据库则由db.jdbcDriverName决定;
#*************** Config Module Related Configurations ***************#
#达梦
### If user MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
db.jdbcDriverName=dm.jdbc.driver.DmDriver
### Connect URL of DB:
db.url.0=jdbc:dm://127.0.0.1:5236/NACOS?STU&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
db.user=root
db.password=root
#人大金仓
#spring.datasource.platform=mysql
#db.num=1
#db.jdbcDriverName=com.kingbase8.Driver
#db.url.0=jdbc:kingbase8://127.0.0.1:54321/nacos
#db.user.0=root
#db.password.0=root
#mysql
#spring.datasource.platform=mysql
#db.jdbcDriverName=com.mysql.cj.jdbc.Driver
#db.num=1
#db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
#db.user=root
#db.password=root
修改的程序
nacos-config模块:
com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceProperties
其实就改了可以通过配置文件指定数据库驱动
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.alibaba.nacos.config.server.service.datasource;
import static com.alibaba.nacos.common.utils.CollectionUtils.getOrDefault;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.Environment;
import com.google.common.base.Preconditions;
import com.zaxxer.hikari.HikariDataSource;
/**
* Properties of external DataSource
*
* @author Nacos
*/
public class ExternalDataSourceProperties{
public static final long CONNECTION_TIMEOUT_MS = 3000L;
public static final long VALIDATION_TIMEOUT = 10L;
public static final String TEST_QUERY = "SELECT 1 FROM dual";
public static final int DEFAULT_MAX_POOL_SIZE = 20;
public static final int DEFAULT_MINIMUM_IDLE = 50;
private Integer num;
private List<String> url = new ArrayList<>();
private List<String> user = new ArrayList<>();
private List<String> password = new ArrayList<>();
private List<Integer> maxPoolSize = new ArrayList<>();
private List<Integer> minIdle = new ArrayList<>();
/**
* 数据库驱动
* 增加对达梦、人大金仓数据库的支持
*/
private String jdbcDriverName;
public String getJdbcDriverName() {
return jdbcDriverName;
}
public void setJdbcDriverName(String jdbcDriverName) {
this.jdbcDriverName = jdbcDriverName;
}
public void setNum(Integer num) {
this.num = num;
}
public void setUrl(List<String> url) {
this.url = url;
}
public void setUser(List<String> user) {
this.user = user;
}
public void setPassword(List<String> password) {
this.password = password;
}
public void setMaxPoolSize(List<Integer> maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
public void setMinIdle(List<Integer> minIdle) {
this.minIdle = minIdle;
}
/**
*
* @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);
HikariDataSource ds = new HikariDataSource();
// ds.setDriverClassName(JDBC_DRIVER_NAME)
// 增加对达梦、人大金仓数据库的支持
ds.setDriverClassName(jdbcDriverName);
if(StringUtils.isNotEmpty(jdbcDriverName)){
// 增加对达梦、人大金仓数据库的支持
ds.setDriverClassName(jdbcDriverName);
}else{
//默认使用mysql驱动
ds.setDriverClassName(JDBC_DRIVER_NAME);
}
ds.setJdbcUrl(url.get(index).trim());
ds.setUsername(getOrDefault(user, index, user.get(0)).trim());
ds.setPassword(getOrDefault(password, index, password.get(0)).trim());
ds.setConnectionTimeout(CONNECTION_TIMEOUT_MS);
ds.setMaximumPoolSize(getOrDefault(maxPoolSize, index, DEFAULT_MAX_POOL_SIZE));
ds.setMinimumIdle(getOrDefault(minIdle, index, DEFAULT_MINIMUM_IDLE));
// Check the connection pool every 10 minutes
ds.setValidationTimeout(TimeUnit.MINUTES.toMillis(VALIDATION_TIMEOUT));
ds.setConnectionTestQuery(TEST_QUERY);
dataSources.add(ds);
callback.accept(ds);
}
Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available");
return dataSources;
}
interface Callback<DataSource> {
/**
* Perform custom logic
* @param dataSource
*/
void accept(DataSource dataSource);
}
}
重新编译、运行nacos
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
或:mvn -Prelease-nacos -Dmaven.test.skip=true -Drat.skip=true clean install -U
或:mvn -Prelease-nacos -Dmaven.test.skip=true -Dcheckstyle.skip=true clean install -U
成功后,编译好的在这里
distribution\target\nacos-server-1.3.0\nacos\bin\startup.cmd
此时,nacos已支持达梦数据库,其实也支持oracle数据库,只要更换驱动就可以
(注:可以查看nacos文件夹,目录里面有一个BUILDING文件中说明)
关于nacos集群
集群和数据库没有关系,业务系统也不需要作任何改变
主要是以下步骤:
1、修改conf/cluster.conf文件,指定参与集群的3个nacos
192.168.4.128:8845
192.168.4.128:8846
192.168.4.128:8847
2、通过nginx代理nacos
upstream nacos {
server 192.168.4.128:8845;
server 192.168.4.128:8846;
server 192.168.4.128:8847;
}
server {
listen 8848;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /nacos {
proxy_pass http://nacos;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}