原文链接:nacos2.2.1集成达梦数据库_nacos2.2.1适配达梦数据库-CSDN博客
nacos2.2.1集成达梦数据库,达梦数据库驱动版本:8.1.1.193
1、下载源码
nacos2.2.1集成达梦数据库_nacos2.2.1适配达梦数据库-CSDN博客
2、新增达梦驱动依赖
父pom.xml <dm-driver.version>8.1.1.193</dm-driver.version> <dependency> <groupId>com.dameng</groupId> <artifactId>DmJdbcDriver18</artifactId> <version>${dm-driver.version}</version> </dependency>
nacos-config模块pom.xml
<dm-driver.version>8.1.1.193</dm-driver.version> <dependency> <groupId>com.dameng</groupId> <artifactId>DmJdbcDriver18</artifactId> <version>${dm-driver.version}</version> </dependency>
3、修改nacos-console模块配置文件application.properties
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=dm
### Count of DB:
db.num=1
### Connect URL of DB:
db.jdbcDriverName=dm.jdbc.driver.DmDriver
db.url.0=jdbc:dm://127.0.0.1:5236/MAIN?STU&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
db.user.0=NACOS
db.password.0=NACOS!@#
4、 nacos-config模块修改java类
修改ExternalDataSourceProperties.java
private String jdbcDriverName;
public String getJdbcDriverName() {
return jdbcDriverName;
}
public void setJdbcDriverName(String jdbcDriverName) {
this.jdbcDriverName = jdbcDriverName;
}
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);
if (StringUtils.isEmpty(poolProperties.getDataSource().getDriverClassName())) {
System.out.println("===================================");
System.out.println("jdbcDriverName=" + jdbcDriverName);
if (StringUtils.isNotEmpty(jdbcDriverName)) {
poolProperties.setDriverClassName(jdbcDriverName);
} else {
poolProperties.setDriverClassName(JDBC_DRIVER_NAME);
}
System.out.println("===================================");
System.out.println("dataSources=" + dataSources);
}
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();
if (StringUtils.isEmpty(ds.getConnectionTestQuery())) {
ds.setConnectionTestQuery(TEST_QUERY);
}
dataSources.add(ds);
callback.accept(ds);
}
Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available");
return dataSources;
}
修改DataSourceConstant.java
//新增达梦常量
public static final String DM = "dm";
修改ExternalConfigInfoPersistServiceImpl.java
176行、194行异常DuplicateKeyException改成DataIntegrityViolationException(不然编辑yml文件时会报)
try {
addConfigInfo(srcIp, srcUser, configInfo, time, configAdvanceInfo, notify);
} catch (DataIntegrityViolationException ive) { // Unique constraint conflict
updateConfigInfo(configInfo, srcIp, srcUser, time, configAdvanceInfo, notify);
}
Map<String, Object> configAdvanceInfo, boolean notify) {
try {
addConfigInfo(srcIp, srcUser, configInfo, time, configAdvanceInfo, notify);
return true;
} catch (DataIntegrityViolationException ignore) { // Unique constraint conflict
return updateConfigInfoCas(configInfo, srcIp, srcUser, time, configAdvanceInfo, notify);
}
5、nacos-datasource-plugin模块添加dm实现类
DataSourceConstant类添加常量
内容拷贝了mysql包下类,改了类名,每个类里面 DataSourceConstant.DM
com.alibaba.nacos.plugin.datasource.mapper.Mapper文件添加上图所加的达梦实现类路径
6、运行
idea运行单机版需要添加VM options配置:-Dnacos.standalone=true,nacos-console直接springboot run就可以,打包部署到服务器:
配了maven环境变量,打开nacos项目根目录,执行命令:mvn -Prelease-nacos ‘-Dmaven.test.skip=true’ ‘-Dcheckstyle.skip=true’ clean install -U 在nacos-distribution/target/目录下有nacos-server-2.1.1.tar.gz文件,单机启动需要添加参数用-m standalone,用start.sh启动命令:./startup.sh -m standalone 用jar启动命令:java ‘-Dnacos.standalone=true’ -jar ./nacos-server.jar;
问题:
如遇见异常
更新配置文件违反唯一约束异常
处理方式为:ExternalConfigInfoPersistServiceImpl方法insertOrUpdate与方法insertOrUpdateCas捕获异常用DataIntegrityViolationException即可