Nacos2.2.0适配金仓Kingbase8数据源

本文详细介绍了Nacos2.2.0如何通过源码修改来适配金仓Kingbase8数据源,包括源码下载、驱动包引入、多个模块的修改,如nacos-config、nacos-datasource-plugin,以及建表DDL语句和打包过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nacos2.2.0适配金仓Kingbase8数据源

Nacos 从 2.2.0 版本开始,可通过 SPI 机制注入多数据源实现插件,并在引入对应数据源实现后,便可在 Nacos 启动时通过读取 application.properties 配置文件中 spring.datasource.platform 配置项选择加载对应多数据源插件.
在这里插入图片描述
Nacos 官方默认实现 MySQL、Derby ,其他类型数据库接入需要参考下文自己扩展。
在这里插入图片描述

一、Nacos官方文档

Nacos整体介绍可看Nacos官方文档

二、实现步骤

下面,重点讲解如何通过修改源码实现金仓KINGBASE8适配

2.1 2.2.0版本源码下载

2.2.0版本源码下载

2.2 引入Kingbase8驱动包

根据适配版本选择对应驱动包

2.2.1 nacos-all pom.xml

<kingbase-connector-java.version>8.6.0</kingbase-connector-java.version>
     <dependency>
         <groupId>com.kingbase8</groupId>
         <artifactId>kingbase8</artifactId>
         <version>${kingbase-connector-java.version}</version>
     </dependency>

2.2.2 nacos-config pom.xml

	 <dependency>
         <groupId>com.kingbase8</groupId>
         <artifactId>kingbase8</artifactId>
     </dependency>

2.3 nacos-config模块修改

修改类config/src/main/java/com/alibaba/nacos/config/server/service/datasource/ExternalDataSourceProperties.java

修改后的代码如下:

/*
 * 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 com.alibaba.nacos.common.utils.Preconditions;
import com.alibaba.nacos.common.utils.StringUtils;
import com.zaxxer.hikari.HikariDataSource;
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 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 String jdbcDriverName = "com.kingbase8.Driver";

    private String testQuery = "SELECT 1";

    private Integer num;

    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(poolProperties.getDataSource().getDriverClassName())) {
   
                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(testQuery);
            }
            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);
    }
}

2.4 nacos-datasource-plugin模块修改

2.4.1 DataSourceConstant类修改

修改类:plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/constants/DataSourceConstant.java

修改后的代码如下:

/*
 * Copyright 1999-2022 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.plugin.datasource.constants;

/**
 * The data source name.
 *
 * @author czf
 **/

public class DataSourceConstant {
   
    public static final String MYSQL = "mysql";

    public static final String DERBY = "derby";

    public static final String KINGBASE = "kingbase8";

}

2.4.2 增加KINGBASE8的mapper实现类

先上截图,如下新增10个类:
在这里插入图片描述
在plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/目录新建kingbase目录,再在dm目录新增实现类,各个类代码如下:
1、KingbaseAbstractMapper

package com.alibaba.nacos.plugin.datasource.impl.kingbase;


import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;

import java.util.List;

/**
 * @author czf
 */
public abstract class KingbaseAbstractMapper extends AbstractMapper {
   

    @Override
    public String select(List<String> columns, List<String> where) {
   
        StringBuilder sql = new StringBuilder("SELECT ");

        for (int i = 0; i < columns.size(); i++) {
   
            sql.append(columns.get(i));
            if (i == columns.size() - 1) {
   
                sql.append(" ");
            }
            else {
   
                sql.append(",");
            }
        }
        sql.append("FROM ");
        sql.append(getTableName());
        sql.append(" ");

        if (where.size() == 0) {
   
            return sql.toString();
        }

        sql.append("WHERE ");
        for (int i = 0; i < where.size(); i++) {
   
            String column = where.get(i);

            // 租户列特殊处理 避免前端传空字符串是Oracle查询不到数据
            if ("tenant_id".equalsIgnoreCase(column)) {
   
                sql.append("(");
                sql.append(column).append(" = ").append("?");
                sql.append(" OR ");
                sql.append(column).append(" IS NULL ");
                sql.append(")");
            }
            else {
   
                sql.append(column).append(" = ").append("?");
            }

            if (i != where.size() - 1) {
   
                sql.append(" AND ");
            }
        }
        return sql.toString();
    }

    @Override
    public String update(List<String> columns, List<String> where) {
   
        StringBuilder sql = new StringBuilder();
        String method = "UPDATE ";
        sql.append(method);
        sql.append(getTableName()).append(" ").append("SET ");

        for (int i = 0; i < columns.size(); i++) {
   
            sql.append(columns.get(i)).append(" = ").append("?");
            if (i != columns.size() - 1) {
   
                sql.append(",");
            }
        }

        if (where.size() == 0) {
   
            return sql.toString();
        }

        sql.append(" WHERE ");

        for (int i = 0; i < where.size(); i++) {
   
            String column = where.get(i);
            if ("tenant_id".equalsIgnoreCase(column)) {
   
                sql.append("(");
                sql.append(column).append(" = ").append("?");
                sql.append(" OR ");
                sql.append(column).append(" IS NULL ");
                sql.append(")");
            }
            else {
   
                sql.append(column).append(" = ").append("?");
            }
            if (i != where.size() - 1) {
   
                sql.append(" AND ");
            }
        }
        return sql.toString();
    }

    @Override
    public String delete(List<String> params) 
### 关于面包板电源模块 MB102 的 USB 供电规格及兼容性 #### 1. **MB102 基本功能** 面包板电源模块 MB102 是一种常见的实验工具,主要用于为基于面包板的小型电子项目提供稳定的电压输出。它通常具有两路独立的稳压输出:一路为 5V 和另一路可调电压(一般范围为 3V 至 12V)。这种设计使得它可以满足多种芯片和传感器的不同工作电压需求。 #### 2. **USB 供电方式** MB102 支持通过 USB 接口供电,输入电压通常是标准的 5V DC[^1]。由于其内部集成了 LM7805 稳压器以及可调节电位器控制的直流-直流变换电路,因此即使输入来自电脑或其他低功率 USB 设备,也能稳定地向负载供应电力。不过需要注意的是,如果项目的功耗较高,则可能超出某些 USB 端口的最大电流能力(一般是 500mA),从而引起不稳定现象或者保护机制启动断开连接的情况发生。 #### 3. **兼容性分析** 该型号广泛适用于各种微控制器单元 (MCU),特别是那些像 Wemos D1 R32 这样可以通过杜邦线轻松接入并共享相同逻辑级别的系统[^2]。另外,在提到 Arduino Uno 板时也表明了良好的互操作性,因为两者均采用相似的标准接口定义与电气特性参数设置[^4]: - 对于需要 3.3V 工作环境下的组件来说,只需调整好对应跳线帽位置即可实现精准匹配; - 当涉及到更多外围扩展应用场合下,例如带有多重模拟信号采集任务的情形里,利用 MB102 提供干净无干扰的基础能源供给就显得尤为重要了[^3]。 综上所述,对于打算构建以单片机为核心的原型验证平台而言,选用具备良好声誉记录且易于获取配件支持服务链路上下游资源丰富的品牌产品——如这里讨论过的这款特定类型的配电装置不失为明智之举之一。 ```python # 示例 Python 代码展示如何检测硬件状态 import machine pin = machine.Pin(2, machine.Pin.IN) if pin.value() == 1: print("Power supply is stable.") else: print("Check your connections and power source.") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值