<dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-datasource-plugin</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.1.3.RELEASE</version> </dependency> <!-- 達夢 --> <dependency> <groupId>com.dameng</groupId> <artifactId>Dm8JdbcDriver18</artifactId> <version>8.1.1.49</version> </dependency> <!-- 瀚高 --> <dependency> <groupId>com.highgo</groupId> <artifactId>HgdbJdbc</artifactId> <version>6.2.2</version> </dependency> <!--人大金倉 --> <dependency> <groupId>com.kingbase</groupId> <artifactId>kingbase8</artifactId> <version>8.2.0</version> </dependency>
public class DataSourceConstant { public static final String MYSQL = "mysql"; public static final String DERBY = "derby"; public static final String DM="dm"; public static final String HIGHGO="highgo"; public static final String KINGBASE="kingbase"; }
package com.alibaba.nacos.plugin.datasource.impl.common; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper; public abstract class AbstractConfigInfoAggrMapperCommon extends AbstractMapper implements ConfigInfoAggrMapper { @Override public String batchRemoveAggr(int datumSize) { final StringBuilder placeholderString = new StringBuilder(); for (int i = 0; i < datumSize; i++) { if (i != 0) { placeholderString.append(", "); } placeholderString.append('?'); } return "DELETE FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND datum_id IN (" + placeholderString + ")"; } @Override public String aggrConfigInfoCount(int size, boolean isIn) { StringBuilder sql = new StringBuilder( "SELECT count(*) FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND datum_id"); if (isIn) { sql.append(" IN ("); } else { sql.append(" NOT IN ("); } for (int i = 0; i < size; i++) { if (i > 0) { sql.append(", "); } sql.append('?'); } sql.append(')'); return sql.toString(); } public String findConfigInfoAggrIsOrdered() { return "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"; } public String findConfigInfoAggrByPageFetchRows(int startRow, int pageSize) { return "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 LIMIT " + pageSize + " OFFSET " + startRow; } public String findAllAggrGroupByDistinct() { return "SELECT DISTINCT data_id, group_id, tenant_id FROM config_info_aggr"; } public String getTableName() { return TableConstant.CONFIG_INFO_AGGR; } }
package com.alibaba.nacos.plugin.datasource.impl.common; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoBetaMapper; public abstract class AbstractConfigInfoBetaMapperCommon extends AbstractMapper implements ConfigInfoBetaMapper { public String updateConfigInfo4BetaCas() { return "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? " + "WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND (md5 = ? or md5 is null or md5 = '')"; } public String findAllConfigInfoBetaForDumpAllFetchRows(int startRow, int pageSize) { return " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key " + " FROM ( SELECT id FROM config_info_beta ORDER BY id LIMIT " + pageSize + " OFFSET " + startRow + " )" + " g, config_info_beta t WHERE g.id = t.id "; } public String getTableName() { return TableConstant.CONFIG_INFO_BETA; } }
package com.alibaba.nacos.plugin.datasource.impl.common; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoTagMapper; public abstract class AbstractConfigInfoTagMapperCommon extends AbstractMapper implements ConfigInfoTagMapper { public String updateConfigInfo4TagCas() { return "UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? " + "WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND tag_id = ? AND (md5 = ? OR md5 IS NULL OR md5 = '')"; } public String findAllConfigInfoTagForDumpAllFetchRows(int startRow, int pageSize) { return " SELECT t.id,data_id,group_id,tenant_id,tag_id,app_name,content,md5,gmt_modified " + " FROM ( SELECT id FROM config_info_tag ORDER BY id LIMIT " + pageSize + " OFFSET " + startRow + " ) " + "g, config_info_tag t WHERE g.id = t.id "; } public String getTableName() { return TableConstant.CONFIG_INFO_TAG; } }
package com.alibaba.nacos.plugin.datasource.impl.common; import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper; import java.util.Map; public abstract class AbstractConfigTagsRelationMapperCommon extends AbstractMapper implements ConfigTagsRelationMapper { public String findConfigInfo4PageCountRows(final Map<String, String> params, final int tagSize) { final String appName = params.get("appName"); final String dataId = params.get("dataId"); final String group = params.get("group"); StringBuilder where = new StringBuilder(" WHERE "); final String sqlCount = "SELECT count(*) FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id"; where.append(" a.tenant_id=? "); if (StringUtils.isNotBlank(dataId)) { where.append(" AND a.data_id=? "); } if (StringUtils.isNotBlank(group)) { where.append(" AND a.group_id=? "); } if (StringUtils.isNotBlank(appName)) { where.append(" AND a.app_name=? "); } where.append(" AND b.tag_name IN ("); for (int i = 0; i < tagSize; i++) { if (i != 0) { where.append(", "); } where.append('?'); } where.append(") "); return sqlCount + where; } public String findConfigInfo4PageFetchRows(Map<String, String> params, int tagSize, int startRow, int pageSize) { final String appName = params.get("appName"); final String dataId = params.get("dataId"); final String group = params.get("group"); StringBuilder where = new StringBuilder(" WHERE "); final String sql = "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content FROM config_info a LEFT JOIN " + "config_tags_relation b ON a.id=b.id"; where.append(" a.tenant_id=? "); if (StringUtils.isNotBlank(dataId)) { where.append(" AND a.data_id=? "); } if (StringUtils.isNotBlank(group)) { where.append(" AND a.group_id=? "); } if (StringUtils.isNotBlank(appName)) { where.append(" AND a.app_name=? "); } where.append(" AND b.tag_name IN ("); for (int i = 0; i < tagSize; i++) { if (i != 0) { where.append(", "); } where.append('?'); } where.append(") "); return sql + where + " LIMIT " + pageSize + " OFFSET " + startRow; } public String findConfigInfoLike4PageCountRows(final Map<String, String> params, int tagSize) { final String appName = params.get("appName"); final String content = params.get("content"); final String dataId = params.get("dataId"); final String group = params.get("group"); StringBuilder where = new StringBuilder(" WHERE "); final String sqlCountRows = "SELECT count(*) FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id "; where.append(" a.tenant_id LIKE ? "); if (!StringUtils.isBlank(dataId)) { where.append(" AND a.data_id LIKE ? "); } if (!StringUtils.isBlank(group)) { where.append(" AND a.group_id LIKE ? "); } if (!StringUtils.isBlank(appName)) { where.append(" AND a.app_name = ? "); } if (!StringUtils.isBlank(content)) { where.append(" AND a.content LIKE ? "); } where.append(" AND b.tag_name IN ("); for (int i = 0; i < tagSize; i++) { if (i != 0) { where.append(", "); } where.append('?'); } where.append(") "); return sqlCountRows + where; } public String findConfigInfoLike4PageFetchRows(final Map<String, String> params, int tagSize, int startRow, int pageSize) { final String appName = params.get("appName"); final String content = params.get("content"); final String dataId = params.get("dataId"); final String group = params.get("group"); StringBuilder where = new StringBuilder(" WHERE "); final String sqlFetchRows = "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content " + "FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id "; where.append(" a.tenant_id LIKE ? "); if (!StringUtils.isBlank(dataId)) { where.append(" AND a.data_id LIKE ? "); } if (!StringUtils.isBlank(group)) { where.append(" AND a.group_id LIKE ? "); } if (!StringUtils.isBlank(appName)) { where.append(" AND a.app_name = ? "); } if (!StringUtils.isBlank(content)) { where.append(" AND a.content LIKE ? "); } where.append(" AND b.tag_name IN ("); for (int i = 0; i < tagSize; i++) { if (i != 0) { where.append(", "); } where.append('?'); } where.append(") "); return sqlFetchRows + where + " LIMIT " + startRow + "," + pageSize; } public String getTableName() { return TableConstant.CONFIG_TAGS_RELATION; } }
package com.alibaba.nacos.plugin.datasource.impl.common; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.GroupCapacityMapper; public abstract class AbstractGroupCapacityMapperCommon extends AbstractMapper implements GroupCapacityMapper { public String insertIntoSelect() { return "INSERT INTO group_capacity (group_id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size,gmt_create," + " gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info"; } public String insertIntoSelectByWhere() { return "INSERT INTO group_capacity (group_id, quota,`usage`, `max_size`, max_aggr_count, max_aggr_size, gmt_create," + " gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info WHERE group_id=? AND tenant_id = ''"; } public String incrementUsageByWhereQuotaEqualZero() { return "UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ? AND `usage` < ? AND quota = 0"; } public String incrementUsageByWhereQuotaNotEqualZero() { return "UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ? AND `usage` < quota AND quota != 0"; } public String incrementUsageByWhere() { return "UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ?"; } public String decrementUsageByWhere() { return "UPDATE group_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE group_id = ? AND `usage` > 0"; } public String updateUsage() { return "UPDATE group_capacity SET `usage` = (SELECT count(*) FROM config_info), gmt_modified = ? WHERE group_id = ?"; } public String updateUsageByWhere() { return "UPDATE group_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE group_id=? AND tenant_id = '')," + " gmt_modified = ? WHERE group_id= ?"; } public String selectGroupInfoBySize() { return "SELECT id, group_id FROM group_capacity WHERE id > ? LIMIT ?"; } public String getTableName() { return TableConstant.GROUP_CAPACITY; } }
package com.alibaba.nacos.plugin.datasource.impl.common; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.HistoryConfigInfoMapper; public abstract class AbstractHistoryConfigInfoMapperCommon extends AbstractMapper implements HistoryConfigInfoMapper { public String removeConfigHistory() { return "DELETE FROM his_config_info WHERE gmt_modified < ? LIMIT ?"; } public String findConfigHistoryCountByTime() { return "SELECT count(*) FROM his_config_info WHERE gmt_modified < ?"; } public String findDeletedConfig() { return "SELECT DISTINCT data_id, group_id, tenant_id FROM his_config_info WHERE op_type = 'D' AND gmt_modified >= ? AND gmt_modified <= ?"; } public String findConfigHistoryFetchRows() { return "SELECT nid,data_id,group_id,tenant_id,app_name,src_ip,src_user,op_type,gmt_create,gmt_modified FROM his_config_info " + "WHERE data_id = ? AND group_id = ? AND tenant_id = ? ORDER BY nid DESC"; } public String detailPreviousConfigHistory() { return "SELECT nid,data_id,group_id,tenant_id,app_name,content,md5,src_user,src_ip,op_type,gmt_create,gmt_modified " + "FROM his_config_info WHERE nid = (SELECT max(nid) FROM his_config_info WHERE id = ?) "; } public String pageFindConfigHistoryFetchRows(int pageNo, int pageSize) { final int offset = (pageNo - 1) * pageSize; final int limit = pageSize; return "SELECT nid,data_id,group_id,tenant_id,app_name,src_ip,src_user,op_type,gmt_create,gmt_modified FROM his_config_info " + "WHERE data_id = ? AND group_id = ? AND tenant_id = ? ORDER BY nid DESC LIMIT " + offset + "," + limit; } public String getTableName() { return TableConstant.HIS_CONFIG_INFO; } }
package com.alibaba.nacos.plugin.datasource.impl.common; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.TenantCapacityMapper; public abstract class AbstractTenantCapacityMapperCommon extends AbstractMapper implements TenantCapacityMapper { public String incrementUsageWithDefaultQuotaLimit() { return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` <" + " ? AND quota = 0"; } public String incrementUsageWithQuotaLimit() { return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` < " + "quota AND quota != 0"; } public String incrementUsage() { return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ?"; } public String decrementUsage() { return "UPDATE tenant_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` > 0"; } public String correctUsage() { return "UPDATE tenant_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE tenant_id = ?), " + "gmt_modified = ? WHERE tenant_id = ?"; } public String getCapacityList4CorrectUsage() { return "SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?"; } public String insertTenantCapacity() { return "INSERT INTO tenant_capacity (tenant_id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size, " + "gmt_create, gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info WHERE tenant_id=?;"; } public String getTableName() { return TableConstant.TENANT_CAPACITY; } }
package com.alibaba.nacos.plugin.datasource.impl.common; import com.alibaba.nacos.common.utils.CollectionUtils; import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import java.util.Map; public abstract class AbstractConfigInfoMapperCommon extends AbstractMapper implements ConfigInfoMapper { private static final String DATA_ID = "dataId"; private static final String GROUP = "group"; private static final String APP_NAME = "appName"; private static final String CONTENT = "content"; private static final String TENANT = "tenant"; public String findConfigMaxId() { return "SELECT MAX(id) FROM config_info"; } public String findAllDataIdAndGroup() { return "SELECT DISTINCT data_id, group_id FROM config_info"; } public String findConfigInfoByAppCountRows() { return "SELECT count(*) FROM config_info WHERE tenant_id LIKE ? AND app_name= ?"; } public String findConfigInfoByAppFetchRows(int startRow, int pageSize) { return "SELECT id,data_id,group_id,tenant_id,app_name,content FROM config_info" + " WHERE tenant_id LIKE ? AND app_name= ?" + " LIMIT " + pageSize + " OFFSET " + startRow; } public String configInfoLikeTenantCount() { return "SELECT count(*) FROM config_info WHERE tenant_id LIKE ?"; } public String getTenantIdList(int startRow, int pageSize) { return "SELECT tenant_id FROM config_info WHERE tenant_id != '' GROUP BY tenant_id LIMIT " + pageSize + " OFFSET " + startRow; } public String getGroupIdList(int startRow, int pageSize) { return "SELECT group_id FROM config_info WHERE tenant_id ='' GROUP BY group_id LIMIT " + pageSize + " OFFSET " + startRow; } public String findAllConfigKey(int startRow, int pageSize) { return " SELECT data_id,group_id,app_name FROM ( " + " SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id LIMIT " + pageSize + " OFFSET " + startRow + " )" + " g, config_info t WHERE g.id = t.id "; } public String findAllConfigInfoBaseFetchRows(int startRow, int pageSize) { return "SELECT t.id,data_id,group_id,content,md5" + " FROM ( SELECT id FROM config_info ORDER BY id LIMIT ?,? ) " + " g, config_info t WHERE g.id = t.id "; } public String findAllConfigInfoFragment(int startRow, int pageSize) { return "SELECT id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,type,encrypted_data_key " + "FROM config_info WHERE id > ? ORDER BY id ASC LIMIT " + pageSize + " OFFSET " + startRow; } public String findChangeConfig() { return "SELECT data_id, group_id, tenant_id, app_name, content, gmt_modified,encrypted_data_key " + "FROM config_info WHERE gmt_modified >= ? AND gmt_modified <= ?"; } public String findChangeConfigCountRows(Map<String, String> params, final Timestamp startTime, final Timestamp endTime) { final String tenant = params.get(TENANT); final String dataId = params.get(DATA_ID); final String group = params.get(GROUP); final String appName = params.get(APP_NAME); final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; final String sqlCountRows = "SELECT count(*) FROM config_info WHERE "; String where = " 1=1 "; if (!StringUtils.isBlank(dataId)) { where += " AND data_id LIKE ? "; } if (!StringUtils.isBlank(group)) { where += " AND group_id LIKE ? "; } if (!StringUtils.isBlank(tenantTmp)) { where += " AND tenant_id = ? "; } if (!StringUtils.isBlank(appName)) { where += " AND app_name = ? "; } if (startTime != null) { where += " AND gmt_modified >=? "; } if (endTime != null) { where += " AND gmt_modified <=? "; } return sqlCountRows + where; } public String findChangeConfigFetchRows(Map<String, String> params, final Timestamp startTime, final Timestamp endTime, int startRow, int pageSize, long lastMaxId) { final String tenant = params.get(TENANT); final String dataId = params.get(DATA_ID); final String group = params.get(GROUP); final String appName = params.get(APP_NAME); final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; final String sqlFetchRows = "SELECT id,data_id,group_id,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 ? "; } if (!StringUtils.isBlank(group)) { where += " AND group_id LIKE ? "; } if (!StringUtils.isBlank(tenantTmp)) { where += " AND tenant_id = ? "; } if (!StringUtils.isBlank(appName)) { where += " AND app_name = ? "; } if (startTime != null) { where += " AND gmt_modified >=? "; } if (endTime != null) { where += " AND gmt_modified <=? "; } return sqlFetchRows + where + " AND id > " + lastMaxId + " ORDER BY id ASC" + " LIMIT " + 0 + " OFFSET " + pageSize; } public String listGroupKeyMd5ByPageFetchRows(int startRow, int pageSize) { return "SELECT t.id,data_id,group_id,tenant_id,app_name,md5,type,gmt_modified,encrypted_data_key FROM " + "( SELECT id FROM config_info ORDER BY id LIMIT " + pageSize + " OFFSET " + startRow + " ) g, config_info t WHERE g.id = t.id"; } public String findAllConfigInfo4Export(List<Long> ids, Map<String, String> params) { String tenant = params.get("tenant"); String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content,type,md5,gmt_create,gmt_modified,src_user,src_ip," + "c_desc,c_use,effect,c_schema,encrypted_data_key FROM config_info"; StringBuilder where = new StringBuilder(" WHERE "); List<Object> paramList = new ArrayList<>(); if (!CollectionUtils.isEmpty(ids)) { where.append(" id IN ("); for (int i = 0; i < ids.size(); i++) { if (i != 0) { where.append(", "); } where.append('?'); paramList.add(ids.get(i)); } where.append(") "); } else { where.append(" tenant_id= ? "); paramList.add(tenantTmp); if (!StringUtils.isBlank(params.get(DATA_ID))) { where.append(" AND data_id LIKE ? "); } if (StringUtils.isNotBlank(params.get(GROUP))) { where.append(" AND group_id= ? "); } if (StringUtils.isNotBlank(params.get(APP_NAME))) { where.append(" AND app_name= ? "); } } return sql + where; } public String findConfigInfoBaseLikeCountRows(Map<String, String> params) { final String sqlCountRows = "SELECT count(*) FROM config_info WHERE "; String where = " 1=1 AND tenant_id='' "; if (!StringUtils.isBlank(params.get(DATA_ID))) { where += " AND data_id LIKE ? "; } if (!StringUtils.isBlank(params.get(GROUP))) { where += " AND group_id LIKE "; } if (!StringUtils.isBlank(params.get(CONTENT))) { where += " AND content LIKE ? "; } return sqlCountRows + where; } public String findConfigInfoBaseLikeFetchRows(Map<String, String> params, int startRow, int pageSize) { final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,content FROM config_info WHERE "; String where = " 1=1 AND tenant_id='' "; if (!StringUtils.isBlank(params.get(DATA_ID))) { where += " AND data_id LIKE ? "; } if (!StringUtils.isBlank(params.get(GROUP))) { where += " AND group_id LIKE "; } if (!StringUtils.isBlank(params.get(CONTENT))) { where += " AND content LIKE ? "; } return sqlFetchRows + where + " LIMIT " + startRow + "," + pageSize; } public String findConfigInfo4PageCountRows(Map<String, String> params) { final String appName = params.get(APP_NAME); final String dataId = params.get(DATA_ID); final String group = params.get(GROUP); final String sqlCount = "SELECT count(*) FROM config_info"; StringBuilder where = new StringBuilder(" WHERE "); where.append(" tenant_id=? "); if (StringUtils.isNotBlank(dataId)) { where.append(" AND data_id=? "); } if (StringUtils.isNotBlank(group)) { where.append(" AND group_id=? "); } if (StringUtils.isNotBlank(appName)) { where.append(" AND app_name=? "); } return sqlCount + where; } public String findConfigInfo4PageFetchRows(Map<String, String> params, int startRow, int pageSize) { final String appName = params.get(APP_NAME); final String dataId = params.get(DATA_ID); final String group = params.get(GROUP); final String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content,type,encrypted_data_key FROM config_info"; StringBuilder where = new StringBuilder(" WHERE "); where.append(" tenant_id=? "); if (StringUtils.isNotBlank(dataId)) { where.append(" AND data_id=? "); } if (StringUtils.isNotBlank(group)) { where.append(" AND group_id=? "); } if (StringUtils.isNotBlank(appName)) { where.append(" AND app_name=? "); } return sql + where + " LIMIT " + pageSize + " OFFSET " + startRow; } public String findConfigInfoBaseByGroupFetchRows(int startRow, int pageSize) { return "SELECT id,data_id,group_id,content FROM config_info WHERE group_id=? AND tenant_id=?" + " LIMIT " + pageSize + " OFFSET " + startRow; } public String findConfigInfoLike4PageCountRows(Map<String, String> params) { String dataId = params.get(DATA_ID); String group = params.get(GROUP); final String appName = params.get(APP_NAME); final String content = params.get(CONTENT); final String sqlCountRows = "SELECT count(*) FROM config_info"; StringBuilder where = new StringBuilder(" WHERE "); where.append(" tenant_id LIKE ? "); if (!StringUtils.isBlank(dataId)) { where.append(" AND data_id LIKE ? "); } if (!StringUtils.isBlank(group)) { where.append(" AND group_id LIKE ? "); } if (!StringUtils.isBlank(appName)) { where.append(" AND app_name = ? "); } if (!StringUtils.isBlank(content)) { where.append(" AND content LIKE ? "); } return sqlCountRows + where; } public String findConfigInfoLike4PageFetchRows(Map<String, String> params, int startRow, int pageSize) { String dataId = params.get(DATA_ID); String group = params.get(GROUP); final String appName = params.get(APP_NAME); final String content = params.get(CONTENT); final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key FROM config_info"; StringBuilder where = new StringBuilder(" WHERE "); where.append(" tenant_id LIKE ? "); if (!StringUtils.isBlank(dataId)) { where.append(" AND data_id LIKE ? "); } if (!StringUtils.isBlank(group)) { where.append(" AND group_id LIKE ? "); } if (!StringUtils.isBlank(appName)) { where.append(" AND app_name = ? "); } if (!StringUtils.isBlank(content)) { where.append(" AND content LIKE ? "); } return sqlFetchRows + where + " LIMIT " + pageSize + " OFFSET " + startRow; } public String findAllConfigInfoFetchRows(int startRow, int pageSize) { return "SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5 " + " FROM ( SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id LIMIT " + pageSize + " OFFSET " + startRow + " )" + " g, config_info t WHERE g.id = t.id "; } public String findConfigInfosByIds(int idSize) { StringBuilder sql = new StringBuilder( "SELECT ID,data_id,group_id,tenant_id,app_name,content,md5 FROM config_info WHERE "); sql.append("id IN ("); for (int i = 0; i < idSize; i++) { if (i != 0) { sql.append(", "); } sql.append('?'); } sql.append(") "); return sql.toString(); } public String removeConfigInfoByIdsAtomic(int size) { StringBuilder sql = new StringBuilder("DELETE FROM config_info WHERE "); sql.append("id IN ("); for (int i = 0; i < size; i++) { if (i != 0) { sql.append(", "); } sql.append('?'); } sql.append(") "); return sql.toString(); } public String updateConfigInfoAtomicCas() { return "UPDATE config_info SET " + "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?, app_name=?,c_desc=?,c_use=?,effect=?,type=?,c_schema=? " + "WHERE data_id=? AND group_id=? AND tenant_id=? AND (md5=? OR md5 IS NULL OR md5='')"; } public String getTableName() { return TableConstant.CONFIG_INFO; } }
package com.alibaba.nacos.plugin.datasource.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.impl.common.AbstractConfigInfoAggrMapperCommon; public class ConfigInfoAggrMapperByDM extends AbstractConfigInfoAggrMapperCommon { @Override public String getDataSource() { return DataSourceConstant.DM; } }
package com.alibaba.nacos.plugin.datasource.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.impl.common.AbstractConfigInfoBetaMapperCommon; public class ConfigInfoBetaMapperByDM extends AbstractConfigInfoBetaMapperCommon { @Override public String getDataSource() { return DataSourceConstant.DM; } }
package com.alibaba.nacos.plugin.datasource.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.impl.common.AbstractConfigInfoMapperCommon; public class ConfigInfoMapperByDM extends AbstractConfigInfoMapperCommon { @Override public String getDataSource() { return DataSourceConstant.DM; } }
package com.alibaba.nacos.plugin.datasource.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.impl.common.AbstractConfigInfoTagMapperCommon; public class ConfigInfoTagMapperByDM extends AbstractConfigInfoTagMapperCommon { @Override public String getDataSource() { return DataSourceConstant.DM; } }
package com.alibaba.nacos.plugin.datasource.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.impl.common.AbstractConfigTagsRelationMapperCommon; public class ConfigTagsRelationMapperByDM extends AbstractConfigTagsRelationMapperCommon { @Override public String getDataSource() { return DataSourceConstant.DM; } }
package com.alibaba.nacos.plugin.datasource.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.impl.common.AbstractGroupCapacityMapperCommon; public class GroupCapacityMapperByDM extends AbstractGroupCapacityMapperCommon { @Override public String getDataSource() { return DataSourceConstant.DM; } }
package com.alibaba.nacos.plugin.datasource.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.impl.common.AbstractHistoryConfigInfoMapperCommon; public class HistoryConfigInfoMapperByDM extends AbstractHistoryConfigInfoMapperCommon { @Override public String getDataSource() { return DataSourceConstant.DM; } }
package com.alibaba.nacos.plugin.datasource.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.impl.common.AbstractTenantCapacityMapperCommon; public class TenantCapacityMapperByDM extends AbstractTenantCapacityMapperCommon { @Override public String getDataSource() { return DataSourceConstant.DM; } }
package com.alibaba.nacos.plugin.datasource.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; public class TenantInfoMapperByDM extends AbstractMapper implements TenantInfoMapper { @Override public String getTableName() { return TableConstant.TENANT_INFO; } @Override public String getDataSource() { return DataSourceConstant.DM; } }
跟上面達夢資料來源適配實現一致,只需將 getDataSource 方法替換成對應的資料來源型別就OK;
在resources下面建立resources/META-INF/services/com.alibaba.nacos.plugin.datasource.mapper.Mapper檔案
com.alibaba.nacos.plugin.datasource.impl.dm.ConfigInfoAggrMapperByDM
com.alibaba.nacos.plugin.datasource.impl.dm.ConfigInfoBetaMapperByDM
com.alibaba.nacos.plugin.datasource.impl.dm.ConfigInfoMapperByDM
com.alibaba.nacos.plugin.datasource.impl.dm.ConfigInfoTagMapperByDM
com.alibaba.nacos.plugin.datasource.impl.dm.ConfigTagsRelationMapperByDM
com.alibaba.nacos.plugin.datasource.impl.dm.GroupCapacityMapperByDM
com.alibaba.nacos.plugin.datasource.impl.dm.HistoryConfigInfoMapperByDM
com.alibaba.nacos.plugin.datasource.impl.dm.TenantCapacityMapperByDM
com.alibaba.nacos.plugin.datasource.impl.dm.TenantInfoMapperByDM
#其他類似..............
1)mvn install後該jar包放入到Nacos(版本2.2.0才支援資料來源外掛化)的plugins目錄下(沒有則新建),Nacos的啟動指令碼startup.sh中-Dloader.path引數將會載入外部jar包檔案到Nacos的環境中;
2)修改Nacos的conf/application.properties,增加對應資料來源的連線引數資訊:
db.num=1 #指定資料來源型別,對應DataSourceConstant引數 spring.sql.init.platform=dm db.url.0=jdbc:dm://ip:5236/ db.user.0=username db.password.0=password #指定driver db.pool.config.driverClassName=dm.jdbc.driver.DmDriver
1)啟動命令:
單機模式:sudo sh startup.sh -m standalone
叢集模式:./startup.sh
PS:
叢集模式下修改cluster.conf.example檔案為cluster.conf,不然報錯誤: Caused by: com.alibaba.nacos.api.exception.NacosException: java.net.UnknownHostException: jmenv.tbsite.net
2)ip存取驗證:http://ip:8848/nacos/index.html
原始碼nacos-2.2.0:https://github.com/alibaba/nacos.git
nacos-console模組com.alibaba.nacos.Nacos
@PostConstruct public void init() { //獲取資料來源 dataSourceService = DynamicDataSource.getInstance().getDataSource(); jt = getJdbcTemplate(); tjt = getTransactionTemplate(); Boolean isDataSourceLogEnable = EnvUtil .getProperty(Constants.NACOS_PLUGIN_DATASOURCE_LOG, Boolean.class, false); mapperManager = MapperManager.instance(isDataSourceLogEnable); } com.alibaba.nacos.config.server.service.datasource.DynamicDataSource#getDataSource public synchronized DataSourceService getDataSource() { try { // Embedded storage is used by default in stand-alone mode // In cluster mode, external databases are used by default //單機模式 if (PropertyUtil.isEmbeddedStorage()) { if (localDataSourceService == null) { localDataSourceService = new LocalDataSourceServiceImpl(); localDataSourceService.init(); } return localDataSourceService; } else { //叢集模式 if (basicDataSourceService == null) { basicDataSourceService = new ExternalDataSourceServiceImpl(); basicDataSourceService.init(); } return basicDataSourceService; } } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void init() { queryTimeout = ConvertUtils.toInt(System.getProperty("QUERYTIMEOUT"), 3); jt = new JdbcTemplate(); // Set the maximum number of records to prevent memory expansion jt.setMaxRows(50000); jt.setQueryTimeout(queryTimeout); testMasterJT = new JdbcTemplate(); testMasterJT.setQueryTimeout(queryTimeout); testMasterWritableJT = new JdbcTemplate(); // Prevent the login interface from being too long because the main library is not available testMasterWritableJT.setQueryTimeout(1); // Database health check testJtList = new ArrayList<>(); isHealthList = new ArrayList<>(); tm = new DataSourceTransactionManager(); tjt = new TransactionTemplate(tm); // Transaction timeout needs to be distinguished from ordinary operations. tjt.setTimeout(TRANSACTION_QUERY_TIMEOUT); //獲取資料來源型別:spring.sql.init.platform或spring.datasource.platform指定的引數值 dataSourceType = DatasourcePlatformUtil.getDatasourcePlatform(defaultDataSourceType); if (PropertyUtil.isUseExternalDB()) { try { //載入資料來源連線 reload(); } catch (IOException e) { FATAL_LOG.error("[ExternalDataSourceService] datasource reload error", e); throw new RuntimeException(DB_LOAD_ERROR_MSG, e); } if (this.dataSourceList.size() > DB_MASTER_SELECT_THRESHOLD) { ConfigExecutor.scheduleConfigTask(new SelectMasterTask(), 10, 10, TimeUnit.SECONDS); } ConfigExecutor.scheduleConfigTask(new CheckDbHealthTask(), 10, 10, TimeUnit.SECONDS); } }
@Override public synchronized void reload() throws IOException { try { final List<JdbcTemplate> testJtListNew = new ArrayList<JdbcTemplate>(); final List<Boolean> isHealthListNew = new ArrayList<Boolean>(); //com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceProperties#build:獲取資料來源 List<HikariDataSource> dataSourceListNew = new ExternalDataSourceProperties() .build(EnvUtil.getEnvironment(), (dataSource) -> { JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setQueryTimeout(queryTimeout); jdbcTemplate.setDataSource(dataSource); testJtListNew.add(jdbcTemplate); isHealthListNew.add(Boolean.TRUE); }); final List<HikariDataSource> dataSourceListOld = dataSourceList; final List<JdbcTemplate> testJtListOld = testJtList; dataSourceList = dataSourceListNew; testJtList = testJtListNew; isHealthList = isHealthListNew; //選擇主資料來源:測試資料來源,資料來源設定到JdbcTemplate jt中(重點,後續請求從jt中獲取dataSource) new SelectMasterTask().run(); //校驗資料來源的狀態 new CheckDbHealthTask().run(); //close old datasource. if (dataSourceListOld != null && !dataSourceListOld.isEmpty()) { for (HikariDataSource dataSource : dataSourceListOld) { dataSource.close(); } } if (testJtListOld != null && !testJtListOld.isEmpty()) { for (JdbcTemplate oldJdbc : testJtListOld) { oldJdbc.setDataSource(null); } } } catch (RuntimeException e) { FATAL_LOG.error(DB_LOAD_ERROR_MSG, e); throw new IOException(e); } }
List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback) { List<HikariDataSource> dataSources = new ArrayList<>(); Binder.get(environment).bind("db", Bindable.ofInstance(this)); Preconditions.checkArgument(Objects.nonNull(num), "db.num is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(user), "db.user or db.user.[index] is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(password), "db.password or db.password.[index] is null"); for (int index = 0; index < num; index++) { int currentSize = index + 1; Preconditions.checkArgument(url.size() >= currentSize, "db.url.%s is null", index); //獲取driverClassName DataSourcePoolProperties poolProperties = DataSourcePoolProperties.build(environment); if (StringUtils.isEmpty(poolProperties.getDataSource().getDriverClassName())) { poolProperties.setDriverClassName(JDBC_DRIVER_NAME); } poolProperties.setJdbcUrl(url.get(index).trim()); poolProperties.setUsername(getOrDefault(user, index, user.get(0)).trim()); poolProperties.setPassword(getOrDefault(password, index, password.get(0)).trim()); HikariDataSource ds = poolProperties.getDataSource(); if (StringUtils.isEmpty(ds.getConnectionTestQuery())) { ds.setConnectionTestQuery(TEST_QUERY); } dataSources.add(ds); callback.accept(ds); } Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available"); return dataSources; }
public static DataSourcePoolProperties build(Environment environment) { DataSourcePoolProperties result = new DataSourcePoolProperties(); //繫結driverClassName值 Binder.get(environment).bind("db.pool.config", Bindable.ofInstance(result.getDataSource())); return result; }
public void loadInitial() { //spi載入外掛實現類 Collection<Mapper> mappers = NacosServiceLoader.load(Mapper.class); for (Mapper mapper : mappers) { Map<String, Mapper> mapperMap = MAPPER_SPI_MAP.getOrDefault(mapper.getDataSource(), new HashMap<>(16)); mapperMap.put(mapper.getTableName(), mapper); MAPPER_SPI_MAP.put(mapper.getDataSource(), mapperMap); LOGGER.info("[MapperManager] Load Mapper({}) datasource({}) tableName({}) successfully.", mapper.getClass(), mapper.getDataSource(), mapper.getTableName()); } }
ps:
SPI:Service Provider Interface (服務提供介面) 是java提供的一套用來被第三方實現或擴充套件的介面。它可以用來啟用框架 擴充套件和替換 元件;
SPI 與API 區別與聯絡
API: (Application Programming Interface) 在絕大多數情況下,都是實現方客製化介面並進行介面的實現,呼叫者只能選擇呼叫,而無權選擇不同實現;
SPI: 呼叫方制定介面規範,提供給外部實現。呼叫方在呼叫時選擇自己需要的外部實現;
流程:
1)定義一個介面,定義介面的實現類;
2)在resources目錄下新建META-INF/services目錄,並且在這個目錄下新建一個與上述介面的全限定名一致的檔案,在這個檔案中寫入介面的實現類的全限定名;
3)通過serviceLoader載入實現類並呼叫
public static void main(String[] args) { ServiceLoader<Mapper> mappers = ServiceLoader.load(Mapper.class); for (Mapper m : mappers) { m.getTableName(); } }
pps:在 ServiceLoader.load 後遍歷 mappers 時可以通過 BeanDefinitionRegistry 可以將mapper實現類註冊到 ApplicationContext中,這樣就可以對介面實現類使用 @Autowired 來解決依賴注入問題了;
@PostConstruct @Override protected void init() throws Throwable { dumpOperate(processor, dumpAllProcessor, dumpAllBetaProcessor, dumpAllTagProcessor); }
ps:
有興趣可研究 ExternalDumpService#init -> DumpService#dumpConfigInfo -> DumpAllProcessor#process(第一行定位到3.8) -> ExternalStoragePersistServiceImpl#findAllConfigInfoFragment -> 分頁將資料庫的config_info(nacos的設定管理的設定列表資訊)dump到本地磁碟 -> ConfigCacheService#dump -> DiskUtil#saveToDisk
pps:所以nacos的設定資訊優先從本地取,本地為空再從資料庫取並更新到本地;
可從 ConfigController#getConfig -> ConfigServletInner#doGetConfig() 中看到從本地快取取,失敗再從資料庫查詢並更新到本地;
public long findConfigMaxId() { //獲取config_info表實現類 ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO); String sql = configInfoMapper.findConfigMaxId(); try { return jt.queryForObject(sql, Long.class); } catch (NullPointerException e) { return 0; } }
public <R extends Mapper> R findMapper(String dataSource, String tableName) { LOGGER.info("[MapperManager] findMapper dataSource: {}, tableName: {}", dataSource, tableName); if (StringUtils.isBlank(dataSource) || StringUtils.isBlank(tableName)) { throw new NacosRuntimeException(FIND_DATASOURCE_ERROR_CODE, "dataSource or tableName is null"); } Map<String, Mapper> tableMapper = MAPPER_SPI_MAP.get(dataSource); if (Objects.isNull(tableMapper)) { throw new NacosRuntimeException(FIND_DATASOURCE_ERROR_CODE, "[MapperManager] Failed to find the datasource,dataSource:" + dataSource); } Mapper mapper = tableMapper.get(tableName); if (Objects.isNull(mapper)) { throw new NacosRuntimeException(FIND_TABLE_ERROR_CODE, "[MapperManager] Failed to find the table ,tableName:" + tableName); } //nacos.plugin.datasource.log.enabled=true開啟紀錄檔:jdk代理 if (dataSourceLogEnable) { MapperProxy mapperProxy = new MapperProxy(); return (R) mapperProxy.createProxy(mapper); } return (R) mapper; }