-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from wuchubuzai2018/20221107_datasource_init
[ISSUE #16]Nacos插件仓库增加Postgresql插件基础支持实现
- Loading branch information
Showing
33 changed files
with
2,020 additions
and
1 deletion.
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.