nacos2.3.0 接入pgsql或其他数据库
首先实验利用官方插件举行扩展,各种报错后放弃,不如自己修改源码吧。一、官方办理方案
1、nocos 文档地址:Nacos 配置中央简介, Nacos 是什么 | Nacos 官网
2、官方解答:nacos支持postgresql数据库吗 | Nacos 官网
3、源码下载地址:Release 2.3.0 (Nov 30, 2023) · alibaba/nacos · GitHub
4、Nacos的数据库插件仓库:nacos-plugin/nacos-datasource-plugin-ext at develop · nacos-group/nacos-plugin · GitHub
按照官方的方案,下载插件后,更改pom文件中nacos的版本,之后将插件打成jar包,放到nacos的plugins目录下,更改配置,启动后各种报错找不到方法,遂放弃。
二、更改源码
1、在项目根(nacos-all)下的pom.xml文件中添加如下依靠:
<postgresql.version>42.3.8</postgresql.version>
org.postgresql postgresql ${postgresql.version}2、在项目标nacos-config-plugin、nacos-persistence模块的pom.xml文件中添加如下依靠:
org.postgresql postgresql3、在项目标nacos-plugin模块的nacos-datasource-plugin模块com.alibaba.nacos.plugin.datasource.impl包下新建一个包,名为postgresql,拷贝同级包mysql下的所有文件到postgresql包下,修改类名和文件名为xxxByPostgresql,如图
https://i-blog.csdnimg.cn/direct/acb8a310a08b4323b1dc92162d35266b.png
4、将这几个类中LIMIT ?,?的写法为OFFSET ? LIMIT ? 如图
https://i-blog.csdnimg.cn/direct/6fc7306cee2c4f9398f6fc7c4cf22af5.png
5、在改项目resource目录下,找到com.alibaba.nacos.plugin.datasource.mapper.Mapper文件,在背面追加:
com.alibaba.nacos.plugin.datasource.impl.postgresql.ConfigInfoAggrMapperByPostgresql
com.alibaba.nacos.plugin.datasource.impl.postgresql.ConfigInfoBetaMapperByPostgresql
com.alibaba.nacos.plugin.datasource.impl.postgresql.ConfigInfoMapperByPostgresql
com.alibaba.nacos.plugin.datasource.impl.postgresql.ConfigInfoTagMapperByPostgresql
com.alibaba.nacos.plugin.datasource.impl.postgresql.ConfigTagsRelationMapperByPostgresql
com.alibaba.nacos.plugin.datasource.impl.postgresql.HistoryConfigInfoMapperByPostgresql
com.alibaba.nacos.plugin.datasource.impl.postgresql.TenantInfoMapperByPostgresql
com.alibaba.nacos.plugin.datasource.impl.postgresql.TenantCapacityMapperByPostgresql
com.alibaba.nacos.plugin.datasource.impl.postgresql.GroupCapacityMapperByPostgresql
如图:https://i-blog.csdnimg.cn/direct/e76f7d8b183e49fdaa8170fe70b9f9be.png
6、在DataSourceConstant类中增加pgsql的常量
public static final String POSTGRESQL = "postgresql";
7、persistence模块,修改PersistenceConstant
public static final String POSTGRESQL = "postgresql";
修改ExternalDataSourceProperties类
https://i-blog.csdnimg.cn/direct/6e2771054bdf44f2af068ddc7268c9d2.png
https://i-blog.csdnimg.cn/direct/0bfb848b087041f1ad8c1b43cc8dad30.png
代码如下,直接复制粘贴即可:
/*
* 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 List<String> url = new ArrayList<>();
private List<String> user = new ArrayList<>();
private List<String> password = new ArrayList<>();
private String jdbcDriverName;
public void setJdbcDriverName(String jdbcDriverName) {
if (StringUtils.isBlank(jdbcDriverName)) {
this.jdbcDriverName = JDBC_DRIVER_NAME;
} else {
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;
}
/**
* 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<HikariDataS
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]