nacos2.2.1集成达梦数据库

nacos2.2.1集成达梦数据库

1、下载源码

https://github.com/alibaba/nacos

2、新增达梦驱动依赖

父pom.xml

		8.1

        
            com.dm.driver
            jdbc18
            ${dm-driver.version}
        

如果编译失败可以降低以下版本
1.30.2
3.16.1

nacos-config模块pom.xml

        
            com.dm.driver
            jdbc18
        

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())) {
                //poolProperties.setDriverClassName(JDBC_DRIVER_NAME);
                System.out.println("===================================");
                System.out.println("jdbcDriverName=" + jdbcDriverName);
                if (StringUtils.isNotEmpty(jdbcDriverName)) {
                    // 增加其他数据库驱动的支持
                    poolProperties.setDriverClassName(jdbcDriverName);
                } else {
                    //默认使用mysql驱动
                    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
nacos2.2.1集成达梦数据库_第1张图片
com.alibaba.nacos.plugin.datasource.mapper.Mapper文件添加上图所加的达梦实现类路径
nacos2.2.1集成达梦数据库_第2张图片
nacos2.2.1集成达梦数据库_第3张图片

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;

你可能感兴趣的:(经验分享,数据库,java,nacos,达梦,dm)