Skip to content
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

添加GoldenDb支持 #56

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public class DatabaseTypeConstant {
public static final String MYSQL = "mysql";

public static final String ORACLE = "oracle";

public static final String DM = "dm";

public static final String SQLSERVER = "sqlserver";

public static final String GOLDENDB = "goldendb";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.plugin.datasource.impl.base;

import com.alibaba.nacos.common.utils.ArrayUtils;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
Expand All @@ -37,21 +38,21 @@
* @author Long Yu
**/
public class BaseConfigInfoMapper extends ConfigInfoMapperByMySql {

private DatabaseDialect databaseDialect;

public BaseConfigInfoMapper() {
databaseDialect = DatabaseDialectManager.getInstance().getDialect(getDataSource());
}

public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSize) {
return databaseDialect.getLimitPageSqlWithOffset(sql, startOffset, pageSize);
}

public String getLimitPageSqlWithMark(String sql) {
return databaseDialect.getLimitPageSqlWithMark(sql);
}

@Override
public MapperResult findConfigInfoByAppFetchRows(MapperContext context) {
int startRow = context.getStartRow();
Expand All @@ -62,7 +63,7 @@ public MapperResult findConfigInfoByAppFetchRows(MapperContext context) {
+ " WHERE tenant_id LIKE ? AND app_name= ?", startRow, pageSize);
return new MapperResult(sql, CollectionUtils.list(tenantId, appName));
}

@Override
public MapperResult getTenantIdList(MapperContext context) {
int startRow = context.getStartRow();
Expand All @@ -71,26 +72,24 @@ public MapperResult getTenantIdList(MapperContext context) {
"SELECT tenant_id FROM config_info WHERE tenant_id != '' GROUP BY tenant_id ", startRow, pageSize);
return new MapperResult(sql, Collections.emptyList());
}

@Override
public MapperResult getGroupIdList(MapperContext context) {
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String sql = getLimitPageSqlWithOffset(
"SELECT group_id FROM config_info WHERE tenant_id ='' GROUP BY group_id ", +startRow, pageSize);
String sql = getLimitPageSqlWithOffset("SELECT group_id FROM config_info WHERE tenant_id ='' GROUP BY group_id ", +startRow, pageSize);
return new MapperResult(sql, Collections.emptyList());
}

@Override
public MapperResult findAllConfigKey(MapperContext context) {
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String innerSql = getLimitPageSqlWithOffset(" SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id ",
startRow, pageSize);
String sql = " SELECT data_id,group_id,app_name FROM ( " + innerSql + " g, config_info t WHERE g.id = t.id ";
String innerSql = getLimitPageSqlWithOffset(" SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id ", startRow, pageSize);
String sql = " SELECT data_id,group_id,app_name FROM ( " + innerSql + " g, config_info t WHERE g.id = t.id ";
return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.TENANT_ID)));
}

@Override
public MapperResult findAllConfigInfoBaseFetchRows(MapperContext context) {
int startRow = context.getStartRow();
Expand All @@ -100,17 +99,16 @@ public MapperResult findAllConfigInfoBaseFetchRows(MapperContext context) {
+ " g, config_info t WHERE g.id = t.id ";
return new MapperResult(sql, CollectionUtils.list(startRow, pageSize));
}

@Override
public MapperResult findAllConfigInfoFragment(MapperContext context) {
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String sql = getLimitPageSqlWithOffset(
"SELECT id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,type,encrypted_data_key "
String sql = getLimitPageSqlWithOffset("SELECT id,data_id,group_id,type,tenant_id,app_name,content,md5,gmt_modified,type,encrypted_data_key "
+ "FROM config_info WHERE id > ? ORDER BY id ASC ", startRow, pageSize);
return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.ID)));
}

@Override
public MapperResult findChangeConfigFetchRows(MapperContext context) {
final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
Expand All @@ -123,8 +121,8 @@ public MapperResult findChangeConfigFetchRows(MapperContext context) {
final long lastMaxId = (long) context.getWhereParameter(FieldConstant.LAST_MAX_ID);
final int pageSize = context.getPageSize();
List<Object> paramList = new ArrayList<>();
final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,type,md5,gmt_modified FROM config_info WHERE ";

final String sqlFetchRows = "SELECT id,data_id,group_id,type,tenant_id,app_name,content,type,md5,gmt_modified FROM config_info WHERE ";
String where = " 1=1 ";
if (!StringUtils.isBlank(dataId)) {
where += " AND data_id LIKE ? ";
Expand Down Expand Up @@ -154,18 +152,16 @@ public MapperResult findChangeConfigFetchRows(MapperContext context) {
String sql = getLimitPageSqlWithOffset(originSql, 0, pageSize);
return new MapperResult(sql, paramList);
}

@Override
public MapperResult listGroupKeyMd5ByPageFetchRows(MapperContext context) {
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String innerSql = getLimitPageSqlWithOffset(" SELECT id FROM config_info ORDER BY id ", startRow, pageSize);
String sql =
" SELECT t.id,data_id,group_id,tenant_id,app_name,md5,type,gmt_modified,encrypted_data_key FROM " + "( "
+ innerSql + " ) g, config_info t WHERE g.id = t.id";
String sql = " SELECT t.id,data_id,group_id,tenant_id,app_name,md5,type,gmt_modified,encrypted_data_key FROM " + "( " + innerSql + " ) g, config_info t WHERE g.id = t.id";
return new MapperResult(sql, Collections.emptyList());
}

@Override
public MapperResult findConfigInfoBaseLikeFetchRows(MapperContext context) {
final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID);
Expand All @@ -191,7 +187,7 @@ public MapperResult findConfigInfoBaseLikeFetchRows(MapperContext context) {
String sql = getLimitPageSqlWithOffset(sqlFetchRows + where, startRow, pageSize);
return new MapperResult(sql, paramList);
}

@Override
public MapperResult findConfigInfo4PageFetchRows(MapperContext context) {
final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
Expand Down Expand Up @@ -225,7 +221,7 @@ public MapperResult findConfigInfo4PageFetchRows(MapperContext context) {
String resultSql = getLimitPageSqlWithOffset(sql + where, startRow, pageSize);
return new MapperResult(resultSql, paramList);
}

@Override
public MapperResult findConfigInfoBaseByGroupFetchRows(MapperContext context) {
int startRow = context.getStartRow();
Expand All @@ -235,15 +231,17 @@ public MapperResult findConfigInfoBaseByGroupFetchRows(MapperContext context) {
return new MapperResult(resultSql, CollectionUtils.list(context.getWhereParameter(FieldConstant.GROUP_ID),
context.getWhereParameter(FieldConstant.TENANT_ID)));
}

@Override
public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) {
final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID);
final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID);
final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME);
final String content = (String) context.getWhereParameter(FieldConstant.CONTENT);
final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key FROM config_info";
final String[] types = (String[]) context.getWhereParameter(FieldConstant.TYPE);

final String sqlFetchRows = "SELECT id,data_id,group_id,type,tenant_id,app_name,content,encrypted_data_key FROM config_info";
StringBuilder where = new StringBuilder(" WHERE ");
where.append(" tenant_id LIKE ? ");
List<Object> paramList = new ArrayList<>();
Expand All @@ -264,25 +262,34 @@ public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) {
where.append(" AND content LIKE ? ");
paramList.add(content);
}
if (!ArrayUtils.isEmpty(types)) {
where.append(" AND type IN (");
for (int i = 0; i < types.length; i++) {
if (i != 0) {
where.append(", ");
}
where.append('?');
paramList.add(types[i]);
}
where.append(") ");
}
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String sql = getLimitPageSqlWithOffset(sqlFetchRows + where, startRow, pageSize);
return new MapperResult(sql, paramList);
}

@Override
public MapperResult findAllConfigInfoFetchRows(MapperContext context) {
String innerSql = getLimitPageSqlWithMark("SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id ");
String sql = " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5 " + " FROM ( " + innerSql + " )"
+ " g, config_info t WHERE g.id = t.id ";
return new MapperResult(sql, CollectionUtils
.list(context.getWhereParameter(FieldConstant.TENANT_ID), context.getStartRow(),
context.getPageSize()));
+ " g, config_info t WHERE g.id = t.id ";
return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.TENANT_ID), context.getStartRow(), context.getPageSize()));
}

@Override
public String getTableName() {
return TableConstant.CONFIG_INFO;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Nacos数据库适配插件

## 一、注意事项

### 1.1、修改数据库配置文件

在application.properties文件中修改goldendb的配置信息:

```properties
spring.datasource.platform=goldendb
db.pool.config.driverClassName=com.goldendb.jdbc.Driver
db.url.0=jdbc:goldendb:loadbalance://x.x.x.x:8880,x.x.x.x:8880,x.x.x.x:8880/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=nacos
db.password.0=xxxx
```

### 1.2、修改插件的pom.xml

推荐修改为最新的版本
```xml
<dependency>
<groupId>com.goldendb</groupId>
<artifactId>gdb_mysql-connector-java</artifactId>
<version>5.1.46.57</version>
</dependency>
```

### 1.3、表结构初始化

表结构和mysql版本一致
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-datasource-plugin-ext</artifactId>
<version>${revision}</version>
</parent>

<artifactId>nacos-goldendb-datasource-plugin-ext</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdbc.goldendb.version>5.1.46.57</jdbc.goldendb.version>
</properties>

<dependencies>
<dependency>
<groupId>com.goldendb</groupId>
<artifactId>gdb_mysql-connector-java</artifactId>
<version>${jdbc.goldendb.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-datasource-plugin-ext-base</artifactId>
<version>${revision}</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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;

/**
* GoldenDb database dialect.
*
* @author brachetto
*/
public class GoldenDbDialect extends AbstractDatabaseDialect {

@Override
public String getType() {
return DatabaseTypeConstant.GOLDENDB;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.goldendb;

import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
import com.alibaba.nacos.plugin.datasource.impl.base.BaseConfigInfoAggrMapper;

/**
* The GoldenDb implementation of ConfigInfoAggrMapper.
*
* @author brachetto
**/

public class ConfigInfoAggrMapperByGoldenDb extends BaseConfigInfoAggrMapper {

@Override
public String getDataSource() {
return DatabaseTypeConstant.GOLDENDB;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.goldendb;

import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
import com.alibaba.nacos.plugin.datasource.impl.base.BaseConfigInfoBetaMapper;

/**
* The GoldenDb implementation of ConfigInfoBetaMapper.
*
* @author Long Yu
**/

public class ConfigInfoBetaMapperByGoldenDb extends BaseConfigInfoBetaMapper {

@Override
public String getDataSource() {
return DatabaseTypeConstant.GOLDENDB;
}

}
Loading