Nacos2.x 适配达梦数据源,2.x版本都可以用这套,只是拿2.3.2举例

下载nacos源码

https://github.com/alibaba/nacos/tree/2.3.2

选择对应的版本,下载对应的zip即可
在这里插入图片描述
在这里插入图片描述

相关配置

在父POM下加入依赖

<dm.version>8.1.1.49</dm.version>

<dependency>
     <groupId>com.dameng</groupId>
     <artifactId>Dm8JdbcDriver18</artifactId>
     <version>${dm.version}</version>
 </dependency>

nacos-consolenacos-naming 下引用依赖

        <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>Dm8JdbcDriver18</artifactId>
        </dependency>

修改 nacos-console 下的 application.properties 配置文件, 把下面的配置沾到最后方即可,记得修改,换成你自己的数据库信息

spring.sql.init.platform=mysql  #这里写mysql就可以,代表使用外部的数据源,不用非得写dm
db.num=1
db.jdbcDriverName=dm.jdbc.driver.DmDriver
db.url.0=jdbc:dm://达梦数据库所在服务器ip:5236?schema=dm数据库名称&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&clobAsString=1   # schema=dm数据库名称  这里记得这样写,代表使用这个库
db.user.0=SYSDBA  # 账号
db.password.0=SYSDBA # 密码

修改代码

ExternalDataSourceProperties.java

    private String jdbcDriverName;

    public String getJdbcDriverName() {
        return jdbcDriverName;
    }

    public void setJdbcDriverName(String jdbcDriverName) {
        this.jdbcDriverName = jdbcDriverName;
    }



   if (StringUtils.isEmpty(jdbcDriverName)) {
       poolProperties.setDriverClassName(JDBC_DRIVER_NAME);
   } else {
       poolProperties.setDriverClassName(jdbcDriverName);
   }

最终这个类的代码如下

/*
 * Copyright 1999-2023 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.persistence.datasource;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.Preconditions;
import com.alibaba.nacos.common.utils.StringUtils;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.Environment;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.alibaba.nacos.common.utils.CollectionUtils.getOrDefault;

/**
 * Properties of external DataSource.
 *
 * @author Nacos
 */
public class ExternalDataSourceProperties {
    
    private static final String JDBC_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    
    private static final String TEST_QUERY = "SELECT 1";
    
    private Integer num;

    private String jdbcDriverName;

    public String getJdbcDriverName() {
        return jdbcDriverName;
    }

    public void setJdbcDriverName(String jdbcDriverName) {
        this.jdbcDriverName = jdbcDriverName;
    }
    
    private List<String> url = new ArrayList<>();
    
    private List<String> user = new ArrayList<>();
    
    private List<String> password = new ArrayList<>();
    
    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;
    }
    
    /**
     * 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);
            if (StringUtils.isEmpty(jdbcDriverName)) {
                poolProperties.setDriverClassName(JDBC_DRIVER_NAME);
            } else {
                poolProperties.setDriverClassName(jdbcDriverName);
            }
            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;
    }
    
    interface Callback<D> {
        
        /**
         * Perform custom logic.
         *
         * @param datasource dataSource.
         */
        void accept(D datasource);
    }
}

IDEA启动调试

  • 进入nacos-console 模块运行,即可

  • 记得在默认是集群启动,tomcat增加配置如下

    -Dnacos.standalone=true
    

    在这里插入图片描述

  • 如果启动报错

    • java: 程序包com.alibaba.nacos.consistency.entity不存在

    • istio.mcp.v1alpha1.MetadataOuterClass不存在
      在这里插入图片描述
      在这里插入图片描述

      如果出现上面的问题,找到类虽在的模块
      找到maven 点击 compile 即可
      或者 找到总工程 点击 compile
      或者 右键选中对应的模块,打开控制台执行 mvn compile
      或者双击 ctrl 执行 mvn clean compile -Dmaven.test.skip=true
      以上几种方式多试试,我试了好多次终于成功了

    另外说一下,nacos 父pom 指定的是8版本的 java 环境,如果你的jdk版本不是8,需要修改
    我使用的是13 这里改成13就好了,否则报错 无效发行版在这里插入图片描述

    如果需要开启nacos密码, 记得修改 nacos-console模块下的 application.properties 如下配置
    是修改,修改不是复制粘贴

    nacos.core.auth.enabled=true
    nacos.core.auth.caching.enabled=true
    
    nacos.core.auth.server.identity.key=nacos
    nacos.core.auth.server.identity.value=nacos
    nacos.core.auth.plugin.nacos.token.secret.key=VGhpc0lzTXlDdXN0b21TZWNyZXRLZXkwMTIzNDU2Nzg=
    

    完结

  • 23
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值