-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ISSUE #16]Nacos插件仓库增加Postgresql插件基础支持实现 #18
Merged
KomachiSion
merged 4 commits into
nacos-group:develop
from
wuchubuzai2018:20221107_datasource_init
Jan 3, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Nacos数据库适配插件 | ||
|
||
## 一、插件概述 | ||
|
||
### 1.1、简介 | ||
|
||
从Nacos2.2版本开始,Nacos提供了数据源扩展插件,以便让需要进行其他数据库适配的用户自己编写插件来保存数据。当前项目插件目前已简单适配Postgresql。 | ||
|
||
如需Nacos2.1支持,请移步个人的如下这个仓库: | ||
|
||
https://github.com/wuchubuzai2018/nacos-multidatasource | ||
|
||
当前项目基于Nacos2.2版本的扩展插件口进行开发。 | ||
|
||
### 2.2、插件工程结构说明 | ||
|
||
nacos-datasource-plugin-ext-base工程为数据库插件操作的适配抽象。 | ||
|
||
nacos-all-datasource-plugin-ext工程可打包所有适配的数据库插件 | ||
|
||
nacos-postgresql-datasource-plugin-ext工程可打包适配Postgresql的数据库插件 | ||
|
||
## 二、下载和使用 | ||
|
||
### 2.1、插件引入 | ||
|
||
方式一:使用postgresql作为依赖引入到Nacos主分支源码中,例如: | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.alibaba.nacos</groupId> | ||
<artifactId>nacos-postgresql-datasource-plugin-ext</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</dependency> | ||
``` | ||
|
||
方式二:下载当前插件项目源码,打包为jar包,将该文件的路径配置到startup.sh文件中,使用Nacos的loader.path机制指定该插件的路径,可修改startup.sh中的loader.path参数的位置进行指定。 | ||
|
||
### 2.2、修改数据库配置文件 | ||
|
||
在application.properties文件中声明postgresql的配置信息: | ||
|
||
```java | ||
spring.datasource.platform=postgresql | ||
db.url.0=jdbc:postgresql://127.0.0.1:5432/nacos?tcpKeepAlive=true&reWriteBatchedInserts=true&ApplicationName=nacos_java | ||
db.user=nacos | ||
db.password=nacos | ||
db.pool.config.driverClassName=org.postgresql.Driver | ||
``` | ||
|
||
### 2.3、导入Postgresql的数据库脚本文件 | ||
|
||
导入nacos-postgresql的脚本文件,脚本文件在nacos-postgresql-datasource-plugin-ext/src/main/resources/schema文件夹下面. | ||
|
||
上面操作完成后,启动Nacos即可。 | ||
|
||
## 三、其他数据库插件开发 | ||
|
||
可参考nacos-postgresql-datasource-plugin-ext工程,新创建Maven项目,实现AbstractDatabaseDialect类,重写相关的分页操作逻辑与方法,并创建相应的mapper实现。 |
27 changes: 27 additions & 0 deletions
27
nacos-datasource-plugin-ext/nacos-datasource-plugin-ext-base/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>nacos-datasource-plugin-ext</artifactId> | ||
<groupId>com.alibaba.nacos</groupId> | ||
<version>${revision}</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.alibaba.nacos</groupId> | ||
<artifactId>nacos-datasource-plugin-ext-base</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
</dependencies> | ||
|
||
|
||
|
||
|
||
</project> |
30 changes: 30 additions & 0 deletions
30
...ase/src/main/java/com/alibaba/nacos/plugin/datasource/constants/DatabaseTypeConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* DatabaseType Constant. | ||
* | ||
* @author Long Yu | ||
**/ | ||
public class DatabaseTypeConstant { | ||
|
||
public static final String POSTGRESQL = "postgresql"; | ||
|
||
public static final String MYSQL = "mysql"; | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...-base/src/main/java/com/alibaba/nacos/plugin/datasource/constants/PrimaryKeyConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* PrimaryKeyConstant. | ||
* | ||
* @author Long Yu | ||
*/ | ||
public class PrimaryKeyConstant { | ||
|
||
/** | ||
* replace lower Statement.RETURN_GENERATED_KEYS into id key. | ||
*/ | ||
public static final String[] LOWER_RETURN_PRIMARY_KEYS = new String[]{"id"}; | ||
|
||
/** | ||
* upper replace Statement.RETURN_GENERATED_KEYS into id key. | ||
* using dameng database. | ||
*/ | ||
public static final String[] UPPER_RETURN_PRIMARY_KEYS = new String[]{"ID"}; | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...se/src/main/java/com/alibaba/nacos/plugin/datasource/dialect/AbstractDatabaseDialect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.dialect; | ||
|
||
import com.alibaba.nacos.plugin.datasource.constants.PrimaryKeyConstant; | ||
|
||
/** | ||
* Abstract DatabaseDialect. | ||
* Default limit for mysql,postgresql | ||
* @author Long Yu | ||
*/ | ||
public abstract class AbstractDatabaseDialect implements DatabaseDialect { | ||
|
||
@Override | ||
public int getPagePrevNum(int page, int pageSize) { | ||
return (page - 1) * pageSize; | ||
} | ||
|
||
@Override | ||
public int getPageLastNum(int page, int pageSize) { | ||
return pageSize; | ||
} | ||
|
||
@Override | ||
public String getLimitTopSqlWithMark(String sql) { | ||
return sql + " LIMIT ? "; | ||
} | ||
|
||
@Override | ||
public String getLimitPageSqlWithMark(String sql) { | ||
return sql + " LIMIT ?,? "; | ||
} | ||
|
||
@Override | ||
public String getLimitPageSql(String sql, int pageNo, int pageSize) { | ||
return sql + " LIMIT " + getPagePrevNum(pageNo, pageSize) + " , " + pageSize; | ||
} | ||
|
||
@Override | ||
public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSize){ | ||
return sql + " LIMIT " + startOffset + " , " + pageSize; | ||
} | ||
|
||
@Override | ||
public String[] getReturnPrimaryKeys() { | ||
return PrimaryKeyConstant.LOWER_RETURN_PRIMARY_KEYS; | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...n-ext-base/src/main/java/com/alibaba/nacos/plugin/datasource/dialect/DatabaseDialect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.dialect; | ||
|
||
/** | ||
* DatabaseDialect interface. | ||
* @author Long Yu | ||
*/ | ||
public interface DatabaseDialect { | ||
|
||
/** | ||
* get database type. | ||
* @return return database type name | ||
*/ | ||
public String getType(); | ||
|
||
/** | ||
* get frist index page param. | ||
* @param page current pageNo | ||
* @param pageSize current pageSize | ||
* @return offset val or maxRange | ||
*/ | ||
public int getPagePrevNum(int page, int pageSize); | ||
|
||
/** | ||
* get second index page param. | ||
* @param page current pageNo | ||
* @param pageSize current pageSize | ||
* @return limit val or minRange | ||
*/ | ||
public int getPageLastNum(int page, int pageSize); | ||
|
||
/** | ||
* get page limit top data sql,contain placeholder. | ||
* @param sql orign sql | ||
* @return append limit sql | ||
*/ | ||
public String getLimitTopSqlWithMark(String sql); | ||
|
||
/** | ||
* get page limit page data sql,contain placeholder. | ||
* @param sql orign sql | ||
* @return append limit sql | ||
*/ | ||
public String getLimitPageSqlWithMark(String sql); | ||
|
||
/** | ||
* get page limit page data sql,using number. | ||
* @param sql orign sql | ||
* @param pageNo current pageNo | ||
* @param pageSize current pageSize | ||
* @return contain page number param sql | ||
*/ | ||
public String getLimitPageSql(String sql, int pageNo, int pageSize); | ||
|
||
/** | ||
* get page limit page data sql,using offset. | ||
* @param sql orign sql | ||
* @param startOffset current offset row | ||
* @param pageSize current pageSize | ||
* @return contain page number param sql | ||
*/ | ||
public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSize); | ||
|
||
/** | ||
* get database return primary keys. | ||
* @return | ||
*/ | ||
public String[] getReturnPrimaryKeys(); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...ase/src/main/java/com/alibaba/nacos/plugin/datasource/dialect/DefaultDatabaseDialect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.dialect; | ||
|
||
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant; | ||
|
||
/** | ||
* defauLT database dialect. | ||
* @author Long Yu | ||
*/ | ||
public class DefaultDatabaseDialect extends AbstractDatabaseDialect { | ||
|
||
@Override | ||
public String getType() { | ||
return DatabaseTypeConstant.MYSQL; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...src/main/java/com/alibaba/nacos/plugin/datasource/impl/base/BaseConfigInfoAggrMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.impl.base; | ||
|
||
import com.alibaba.nacos.plugin.datasource.constants.TableConstant; | ||
import com.alibaba.nacos.plugin.datasource.dialect.DatabaseDialect; | ||
import com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoAggrMapperByMySql; | ||
import com.alibaba.nacos.plugin.datasource.manager.DatabaseDialectManager; | ||
|
||
/** | ||
* The base implementation of ConfigTagsRelationMapper. | ||
* | ||
* @author Long Yu | ||
**/ | ||
public class BaseConfigInfoAggrMapper extends ConfigInfoAggrMapperByMySql { | ||
|
||
private DatabaseDialect databaseDialect; | ||
|
||
public BaseConfigInfoAggrMapper() { | ||
databaseDialect = DatabaseDialectManager.getInstance().getDialect(getDataSource()); | ||
} | ||
|
||
@Override | ||
public String getTableName() { | ||
return TableConstant.CONFIG_INFO_AGGR; | ||
} | ||
|
||
@Override | ||
public String findConfigInfoAggrByPageFetchRows(int startRow, int pageSize) { | ||
return databaseDialect.getLimitPageSqlWithOffset( | ||
"SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM config_info_aggr WHERE data_id= ? AND " | ||
+ "group_id= ? AND tenant_id= ? ORDER BY datum_id ", startRow, pageSize); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nacos-all-datasource-plugin-ext模块和nacos-all-datasource-plugin-ext模块可以后续提单独的issue来做
先做单独PG数据源的适配即可