赞
踩
本文参考了 Nacos2.2.0版本适配Oracle 和 Nacos2.2.0版本适配PostgreSQL(含GaussDB),请配合使用。
以下更改均基于 Nacos2.3.2release版本。
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
nacos-all的pom里添加依赖管理,nacos-config和nacos-naming的pom里添加依赖使用。
参考里那篇csdn文章修改了ExternalDataSourceProperties文件(现已处在nacos-persistence模块),但是里面修改完的函数好像没有用到,所以改不改区别不大。
/* * 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 hyx **/ public class DataSourceConstant { public static final String MYSQL = "mysql"; public static final String DERBY = "derby"; public static final String ORACLE = "oracle"; public static final String POSTGRESQL = "postgresql"; }
这里添加了两个字段,ORACLE和POSTGRESQL。(2.2.0那篇csdn里ORACLE拼错了。。)
具体代码详见github项目。
和参考里那篇CSDN描述的一致,9个类放在 com/alibaba/nacos/plugin/datasource/impl 目录的新建oracle文件夹下。
和以往不同的是:
"rnum >= " + startRow + " and " + pageSize + " >= rnum"
这种语句,但这是非常不正确的。因为查询第一页时,startRow是0,pageSize比如是10。查询第二页时的startRow是10,pageSize还是10,这样第二页就只会有第10行的值了。所以正确的写法可以参考github当中的"rnum >= " + (startRow + 1) + " and " + (pageSize + startRow) + " >= rnum"
。同参考里的做法修改com.alibaba.nacos.plugin.datasource.mapper.Mapper,注册我们自己的mapper。
我们做这一步的原因在于nacos的public命名空间的命名空间ID是空字符串,即''
。而mapper里面的sql对应着tenant_id = ?
。如果oracle数据库里面tenant_id是空字符串,我们搜索tenant_id = ''
传入空字符串的话会匹配不到(使用tenant_id is null
倒可以匹配,但修改起来太麻烦,需要从上到下都改),因此我们选择在数据库里存public命名空间的tenant_id时存入'null'
,搜索的时候我们也tenant_id = 'null'
。
因此,我们采用2.2.0版本csdn里2.5当中除了添加id的那些操作(原因看后文),即把所有tenant相关的默认值从空字符串换成null
字符串,这样存的时候存null
字符串,查的时候也查null
字符串。
也就是不修改com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImpl.java,不修改com/alibaba/nacos/config/server/service/repository/extrnal/ExternalHistoryConfigInfoPersistServiceImpl.java,不修改com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.java。
修改com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImpl.java,修改com/alibaba/nacos/config/server/service/repository/extrnal/ExternalHistoryConfigInfoPersistServiceImpl.java。
需要注意,2.2.0版本修改少了一处,这会导致在nacos面板看所有命名空间情况的时候看到public命名空间的配置数总为0:
修改com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImpl.java当中
@Override
public int configInfoCount(String tenant) {
if (tenant.equals(StringUtils.EMPTY))
tenant = StringUtils.NULL;
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
MapperContext context = new MapperContext();
context.putWhereParameter(FieldConstant.TENANT_ID, tenant);
MapperResult mapperResult = configInfoMapper.configInfoLikeTenantCount(context);
Integer result = jt.queryForObject(mapperResult.getSql(), mapperResult.getParamList().toArray(), Integer.class);
if (result == null) {
throw new IllegalArgumentException("configInfoCount error");
}
return result;
}
按链接中文章的2.5.4操作步骤,把所有的resource换成resources,避免oracle当中的resource关键字报错。
如果你要打包后用脚本启动的话在distribution/conf/application.properties下,否则直接启动程序就在console/src/main/resources/application.properties下:
##### Public Config:
nacos.plugin.datasource.log.enabled=true
### Count of DB:
db.num=1
##### If use Oracle as datasource:
spring.sql.init.platform=oracle
### Connect URL of DB:
db.url.0=
db.user.0=
db.password.0=
# db.jdbcDriverName=oracle.jdbc.OracleDriver
db.pool.config.driver-class-name=oracle.jdbc.OracleDriver
db.testQuery=select 1 from dual
sql文件见github项目。
由于 2.2.0版本的oracle建表sql 没有自增主键这种东西,所以我通过 在线SQL转换工具 手动转换了nacos自带的mysql的sql语句,同时修改了包括加密字段默认非空不加鉴权启动时报错、sql语句中check不正确、true转换成1、resource转换成resources等问题。
使用mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
,打出来的在distribution/target下,可以根据官方文档启动。
同oracle
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.10</version>
</dependency>
同oracle,只不过这次的很简单,直接把mysql的mapper文件复制一下,mysql里面LIMIT ?, ?
的换成OFFSET ? LIMIT ?
即可。最后在com.alibaba.nacos.plugin.datasource.mapper.Mapper下添加相应内容。
##### If use POSTGRESQL as datasource:
spring.sql.init.platform=postgresql
db.url.0=
db.user.0=
db.password.0=
db.pool.config.driver-class-name=org.postgresql.Driver
基本直接使用的 Nacos2.2.0版本适配PostgreSQL(含GaussDB) 旧版sql建表,修改了加密字段的not null,为了和oracle一致也修改了resource。
同上,注意多数据源的application.properties配置。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。