nacos2.3.0 接入pgsql或其他数据库

写过一篇  论坛元老 | 2025-3-29 08:27:02 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1664|帖子 1664|积分 4992

首先实验利用官方插件举行扩展,各种报错后放弃,不如自己修改源码吧。
一、官方办理方案

   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 postgresql  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的常量
  1.     public static final String POSTGRESQL = "postgresql";
复制代码
7、persistence模块,修改PersistenceConstant
  1.     public static final String POSTGRESQL = "postgresql";
复制代码
修改ExternalDataSourceProperties类


代码如下,直接复制粘贴即可:
  1. /*
  2. * Copyright 1999-2023 Alibaba Group Holding Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.nacos.persistence.datasource;
  17. import com.alibaba.nacos.common.utils.CollectionUtils;
  18. import com.alibaba.nacos.common.utils.Preconditions;
  19. import com.alibaba.nacos.common.utils.StringUtils;
  20. import com.zaxxer.hikari.HikariDataSource;
  21. import org.springframework.boot.context.properties.bind.Bindable;
  22. import org.springframework.boot.context.properties.bind.Binder;
  23. import org.springframework.core.env.Environment;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Objects;
  27. import static com.alibaba.nacos.common.utils.CollectionUtils.getOrDefault;
  28. /**
  29. * Properties of external DataSource.
  30. *
  31. * @author Nacos
  32. */
  33. public class ExternalDataSourceProperties {
  34.    
  35.     private static final String JDBC_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
  36.     private static final String TEST_QUERY = "SELECT 1";
  37.    
  38.     private Integer num;
  39.    
  40.     private List<String> url = new ArrayList<>();
  41.    
  42.     private List<String> user = new ArrayList<>();
  43.    
  44.     private List<String> password = new ArrayList<>();
  45.     private String jdbcDriverName;
  46.     public void setJdbcDriverName(String jdbcDriverName) {
  47.         if (StringUtils.isBlank(jdbcDriverName)) {
  48.             this.jdbcDriverName = JDBC_DRIVER_NAME;
  49.         } else {
  50.             this.jdbcDriverName = jdbcDriverName;
  51.         }
  52.     }
  53.    
  54.     public void setNum(Integer num) {
  55.         this.num = num;
  56.     }
  57.    
  58.     public void setUrl(List<String> url) {
  59.         this.url = url;
  60.     }
  61.    
  62.     public void setUser(List<String> user) {
  63.         this.user = user;
  64.     }
  65.    
  66.     public void setPassword(List<String> password) {
  67.         this.password = password;
  68.     }
  69.     /**
  70.      * Build serveral HikariDataSource.
  71.      *
  72.      * @param environment {@link Environment}
  73.      * @param callback    Callback function when constructing data source
  74.      * @return List of {@link HikariDataSource}
  75.      */
  76.     List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback) {
  77.         List<HikariDataS
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

写过一篇

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表