首先尝试使用官方插件进行扩展,各种报错后放弃,不如自己修改源码吧。
一、官方解决方案
1、nocos 文档地址:Nacos 配置中心简介, Nacos 是什么 | Nacos 官网
2、官方解答:nacos支持postgresql数据库吗 | Nacos 官网
3、源码下载地址:Release 2.3.0 (Nov 30, 2023) · alibaba/nacos · GitHub4、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><dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>${postgresql.version}</version> </dependency>
2、在项目的nacos-config-plugin、nacos-persistence模块的pom.xml
文件中添加如下依赖:
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency>
3、在项目的nacos-plugin模块的nacos-datasource-plugin模块com.alibaba.nacos.plugin.datasource.impl
包下新建一个包,名为postgresql
,拷贝同级包mysql
下的所有文件到postgresql
包下,修改类名和文件名为xxxByPostgresql
,如图
4、将这几个类中LIMIT ?,?的
写法为OFFSET ? LIMIT ? 如图
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
如图:
6、在DataSourceConstant类中增加pgsql的常量
public static final String POSTGRESQL = "postgresql";
7、persistence模块,修改PersistenceConstant
public static final String POSTGRESQL = "postgresql";
修改ExternalDataSourceProperties类
代码如下,直接复制粘贴即可:
/*
* 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}
* &