从2.2.0版本开始,可通过SPI机制注入多数据源实现插件,并在引入对应数据源实现后,便可在Nacos启动时通过读取application.properties配置文件中spring.datasource.platform配置项选择加载对应多数据源插件.本文档详细介绍一个多数据源插件如何实现以及如何使其生效。
文章目录
Nacos整体介绍可看Nacos官方文档
下面,重点讲解如何通过修改源码实现Oracle适配
根据适配版本选择对应驱动包
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>${ojdbc.version}</version>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
</dependency>
修改类config/src/main/java/com/alibaba/nacos/config/server/service/datasource/ExternalDataSourceProperties.java
修改后的代码如下:
/*
* Copyright 1999-2018 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.config.server.service.datasource;
import com.alibaba.nacos.common.utils.Preconditions;
import com.alibaba.nacos.common.utils.StringUtils;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.Environment;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static com.alibaba.nacos.common.utils.CollectionUtils.getOrDefault;
/**
* Properties of external DataSource.
*
* @author Nacos
*/
public class ExternalDataSourceProperties {
//update
private String jdbcDriverName;
//update
private String testQuery;
private Integer num;
private List<String> url = new ArrayList<>();
private List<String> user = new ArrayList<>();
private List<String> password = new ArrayList<>();
public void setNum(Integer num) {
this.num = num;
}
public void setUrl(List<String> url) {
this.url = url;
}
public void setUser(List<String> user) {
this.user = user;
}
public void setPassword(List<String> password) {
this.password = password;
}
//add new
public void setTestQuery(String testQuery) {
if (StringUtils.isBlank(testQuery)) {
this.testQuery = "SELECT 1";
} else {
this.testQuery = testQuery;
}
}
//add new
public void setJdbcDriverName(String jdbcDriverName) {
if (StringUtils.isBlank(jdbcDriverName)) {
this.jdbcDriverName = "com.mysql.cj.jdbc.Driver";
} else {
this.jdbcDriverName = jdbcDriverName;
}
}
/**
* Build serveral HikariDataSource.
*
* @param environment {@link Environment}
* @param callback Callback function when constructing data source
* @return List of {@link HikariDataSource}
*/
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);
DataSourcePoolProperties poolProperties = DataSourcePoolProperties.build(environment);
if (StringUtils.isEmpty(poolProperties.getDataSource().getDriverClassName())) {
poolProperties.setDriverClassName(jdbcDriverName);
}
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(testQuery);
}
dataSources.add(ds);
callback.accept(ds);
}
Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available");
return dataSources;
}
interface Callback<D> {
/**
* Perform custom logic.
*
* @param datasource dataSource.
*/
void accept(D datasource);
}
}
修改类:plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/constants/DataSourceConstant.java
修改后的代码如下:
/*
* 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 ORCLE = "oracle";
}
先上截图,如下新增9个类:

在plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/目录新建oracle目录,再在oracle目录新增实现类,各个类代码如下:
1.ConfigInfoAggrMapperByOracle
/*
* 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.oracle;
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.ConfigInfoAggrMapper;
import java.util.List;
/**
* The mysql implementation of ConfigInfoAggrMapper.
*
* @author hyx
**/
public class ConfigInfoAggrMapperByOracle extends AbstractMapper implements ConfigInfoAggrMapper {
@Override
public String batchRemoveAggr(List<String> datumList) {
final StringBuilder datumString = new StringBuilder();
for (String datum : datumList) {
datumString.append('\'').append(datum).append("',");
}
datumString.deleteCharAt(datumString.length() - 1);
return "DELETE FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND datum_id IN ("
+ datumString + ")";
}
@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();
}
@Override
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";
}
@Override
public String findConfigInfoAggrByPageFetchRows(int startRow, int pageSize) {
return "SELECT * FROM (SELECT data_id,group_id,tenant_id,datum_id,app_name,content, ROWNUM as rnum"
+ " FROM config_info_aggr WHERE data_id= ? AND "
+ "group_id= ? AND tenant_id= ? ORDER BY datum_id) WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum";
}
@Override
public String findAllAggrGroupByDistinct() {
return "SELECT DISTINCT data_id, group_id, tenant_id FROM config_info_aggr";
}
@Override
public String getTableName() {
return TableConstant.CONFIG_INFO_AGGR;
}
@Override
public String getDataSource() {
return DataSourceConstant.ORCLE;
}
}
2.ConfigInfoBetaMapperByOracle
/*
* 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.oracle;
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.ConfigInfoBetaMapper;
/**
* The mysql implementation of ConfigInfoBetaMapper.
*
* @author hyx
**/
public class ConfigInfoBetaMapperByOracle extends AbstractMapper implements ConfigInfoBetaMapper {
@Override
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 = '')";
}
@Override
public String findAllConfigInfoBetaForDumpAllFetchRows(int startRow, int pageSize) {
return " SELECT * FROM (SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key "
+ " FROM ( SELECT * FROM (SELECT id, ROWNUM as rnum FROM config_info_beta ORDER BY id) "
+ "WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum" + " )"
+ " g, config_info_beta t WHERE g.id = t.id ";
}
@Override
public String getTableName() {
return TableConstant.CONFIG_INFO_BETA;
}
@Override
public String getDataSource() {
return DataSourceConstant.ORCLE;
}
}
3.ConfigInfoMapperByOracle
/*
* 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.oracle;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;
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.ConfigInfoMapper;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* The mysql implementation of ConfigInfoMapper.
*
* @author hyx
**/
public class ConfigInfoMapperByOracle 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";
@Override
public String findConfigMaxId() {
return "SELECT MAX(id) FROM config_info";
}
@Override
public String findAllDataIdAndGroup() {
return "SELECT DISTINCT data_id, group_id FROM config_info";
}
@Override
public String findConfigInfoByAppCountRows() {
return "SELECT count(*) FROM config_info WHERE tenant_id LIKE ? AND app_name= ?";
}
@Override
public String findConfigInfoByAppFetchRows(int startRow, int pageSize) {
return "SELECT * FROM (SELECT id,data_id,group_id,tenant_id,app_name,content, ROWNUM as rnum FROM config_info"
+ " WHERE tenant_id LIKE ? AND app_name= ?)" + " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum";
}
@Override
public String configInfoLikeTenantCount() {
return "SELECT count(*) FROM config_info WHERE tenant_id LIKE ?";
}
@Override
public String getTenantIdList(int startRow, int pageSize) {
return "SELECT * FROM (SELECT tenant_id, ROWNUM as rnum FROM config_info WHERE tenant_id != '' GROUP BY tenant_id)"
+ " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum";
}
@Override
public String getGroupIdList(int startRow, int pageSize) {
return "SELECT * FROM (SELECT group_id, ROWNUM as rnum FROM config_info WHERE tenant_id ='' GROUP BY group_id) "
+ " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum";
}
@Override
public String findAllConfigKey(int startRow, int pageSize) {
return " SELECT data_id,group_id,app_name FROM ( "
+ " SELECT * FROM (SELECT id, ROWNUM as rnum FROM config_info WHERE tenant_id LIKE ? ORDER BY id)"
+ " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum"
+ " )" + " g, config_info t WHERE g.id = t.id ";
}
@Override
public String findAllConfigInfoBaseFetchRows(int startRow, int pageSize) {
return "SELECT t.id,data_id,group_id,content,md5"
+ " FROM (SELECT * FROM ( SELECT id, ROWNUM as rnum FROM config_info ORDER BY id)"
+ " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum) "
+ " g, config_info t WHERE g.id = t.id ";
}
@Override
public String findAllConfigInfoFragment(int startRow, int pageSize) {
return "SELECT * FROM (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) " + " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum) ";
}
@Override
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 <= ?";
}
@Override
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;
}
@Override
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 * FROM (SELECT id,data_id,group_id,tenant_id,app_name,content,type,md5,gmt_modified"
+ ", ROWNUM as rnum 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)" + " WHERE rnum >= " + 0 + " and " + pageSize + " >= rnum";
}
@Override
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 * FROM ( SELECT id, ROWNUM as rnum FROM config_info ORDER BY id) "
+ " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum "
+ " ) g, config_info t WHERE g.id = t.id";
}
@Override
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;
}
@Override
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;
}
@Override
public String findConfigInfoBaseLikeFetchRows(Map<String, String> params, int startRow, int pageSize) {
final String sqlFetchRows = "SELECT * FROM (SELECT id,data_id,group_id,tenant_id,content, ROWNUM as rnum 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 + ") " + " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum ";
}
@Override
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;
}
@Override
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 * FROM (SELECT id,data_id,group_id,tenant_id,app_name,content,type,encrypted_data_key,"
+ " ROWNUM as rnum 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 + ") " + " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum ";
}
@Override
public String findConfigInfoBaseByGroupFetchRows(int startRow, int pageSize) {
return "SELECT * FROM (SELECT id,data_id,group_id,content, ROWNUM as rnum FROM config_info WHERE group_id=? AND tenant_id=?" + ") "
+ " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum ";
}
@Override
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;
}
@Override
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 * FROM (SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key,"
+ " ROWNUM as rnum 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 + " ) " + " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum ";
}
@Override
public String findAllConfigInfoFetchRows(int startRow, int pageSize) {
return "SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5 "
+ " FROM (SELECT * FROM ( SELECT id, ROWNUM as rnum FROM config_info WHERE tenant_id LIKE ? ORDER BY id)"
+ " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum)"
+ " g, config_info t WHERE g.id = t.id ";
}
@Override
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();
}
@Override
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();
}
@Override
public String getTableName() {
return TableConstant.CONFIG_INFO;
}
@Override
public String getDataSource() {
return DataSourceConstant.ORCLE;
}
@Override
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='')";
}
}
4.ConfigInfoTagMapperByOracle
/*
* 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.oracle;
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.ConfigInfoTagMapper;
/**
* The mysql implementation of ConfigInfoTagMapper.
*
* @author hyx
**/
public class ConfigInfoTagMapperByOracle extends AbstractMapper implements ConfigInfoTagMapper {
@Override
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 = '')";
}
@Override
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 * FROM ( SELECT id, ROWNUM as rnum FROM config_info_tag ORDER BY id) "
+ " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum ) "
+ "g, config_info_tag t WHERE g.id = t.id ";
}
@Override
public String getTableName() {
return TableConstant.CONFIG_INFO_TAG;
}
@Override
public String getDataSource() {
return DataSourceConstant.ORCLE;
}
}
5.ConfigTagsRelationMapperByOracle
/*
* 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.oracle;
import com.alibaba.nacos.common.utils.StringUtils;
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.ConfigTagsRelationMapper;
import java.util.Map;
/**
* The mysql implementation of ConfigTagsRelationMapper.
*
* @author hyx
**/
public class ConfigTagsRelationMapperByOracle extends AbstractMapper implements ConfigTagsRelationMapper {
@Override
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;
}
@Override
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 * FROM (SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content, ROWNUM as rnum 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 + ")" + " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum ";
}
@Override
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;
}
@Override
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 * FROM (SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content, ROWNUM as rnum "
+ "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 + ") " + " WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum ";
}
@Override
public String getTableName() {
return TableConstant.CONFIG_TAGS_RELATION;
}
@Override
public String getDataSource() {
return DataSourceConstant.ORCLE;
}
}
6.GroupCapacityMapperByOracle
/*
* 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.oracle;
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.GroupCapacityMapper;
/**
* The derby implementation of {@link GroupCapacityMapper}.
*
* @author lixiaoshuang
*/
public class GroupCapacityMapperByOracle extends AbstractMapper implements GroupCapacityMapper {
@Override
public String getTableName() {
return TableConstant.GROUP_CAPACITY;
}
@Override
public String getDataSource() {
return DataSourceConstant.ORCLE;
}
@Override
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";
}
@Override
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 = ''";
}
@Override
public String incrementUsageByWhereQuotaEqualZero() {
return "UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ? AND `usage` < ? AND quota = 0";
}
@Override
public String incrementUsageByWhereQuotaNotEqualZero() {
return "UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ? AND `usage` < quota AND quota != 0";
}
@Override
public String incrementUsageByWhere() {
return "UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ?";
}
@Override
public String decrementUsageByWhere() {
return "UPDATE group_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE group_id = ? AND `usage` > 0";
}
@Override
public String updateUsage() {
return "UPDATE group_capacity SET `usage` = (SELECT count(*) FROM config_info), gmt_modified = ? WHERE group_id = ?";
}
@Override
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= ?";
}
@Override
public String selectGroupInfoBySize() {
return "SELECT id, group_id FROM group_capacity WHERE id > ? LIMIT ?";
}
}
7.HistoryConfigInfoMapperByOracle
/*
* 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.oracle;
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.HistoryConfigInfoMapper;
/**
* The mysql implementation of HistoryConfigInfoMapper.
*
* @author hyx
**/
public class HistoryConfigInfoMapperByOracle extends AbstractMapper implements HistoryConfigInfoMapper {
@Override
public String removeConfigHistory() {
return "DELETE FROM his_config_info WHERE gmt_modified < ? LIMIT ?";
}
@Override
public String findConfigHistoryCountByTime() {
return "SELECT count(*) FROM his_config_info WHERE gmt_modified < ?";
}
@Override
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 <= ?";
}
@Override
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";
}
@Override
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 = ?) ";
}
@Override
public String getTableName() {
return TableConstant.HIS_CONFIG_INFO;
}
@Override
public String getDataSource() {
return DataSourceConstant.ORCLE;
}
}
8.TenantCapacityMapperByOracle
/*
* 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.oracle;
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.TenantCapacityMapper;
/**
* The mysql implementation of TenantCapacityMapper.
*
* @author hyx
**/
public class TenantCapacityMapperByOracle extends AbstractMapper implements TenantCapacityMapper {
@Override
public String getTableName() {
return TableConstant.TENANT_CAPACITY;
}
@Override
public String getDataSource() {
return DataSourceConstant.ORCLE;
}
@Override
public String incrementUsageWithDefaultQuotaLimit() {
return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` <"
+ " ? AND quota = 0";
}
@Override
public String incrementUsageWithQuotaLimit() {
return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` < "
+ "quota AND quota != 0";
}
@Override
public String incrementUsage() {
return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ?";
}
@Override
public String decrementUsage() {
return "UPDATE tenant_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` > 0";
}
@Override
public String correctUsage() {
return "UPDATE tenant_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE tenant_id = ?), "
+ "gmt_modified = ? WHERE tenant_id = ?";
}
@Override
public String getCapacityList4CorrectUsage() {
return "SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?";
}
@Override
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=?;";
}
}
9.TenantInfoMapperByOracle
/*
* 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.oracle;
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;
/**
* The mysql implementation of TenantInfoMapper.
*
* @author hyx
**/
public class TenantInfoMapperByOracle extends AbstractMapper implements TenantInfoMapper {
@Override
public String getTableName() {
return TableConstant.TENANT_INFO;
}
@Override
public String getDataSource() {
return DataSourceConstant.ORCLE;
}
}
修改类:plugin/datasource/src/main/resources/META-INF/services/com.alibaba.nacos.plugin.datasource.mapper.Mapper
#
# 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.
#
com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoAggrMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoBetaMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoTagMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigTagsRelationMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.HistoryConfigInfoMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.TenantInfoMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.TenantCapacityMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.GroupCapacityMapperByMysql
com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoAggrMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoBetaMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoTagMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoTagsRelationMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.HistoryConfigInfoMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.TenantInfoMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.TenantCapacityMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.GroupCapacityMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.oracle.ConfigInfoAggrMapperByOracle
com.alibaba.nacos.plugin.datasource.impl.oracle.ConfigInfoBetaMapperByOracle
com.alibaba.nacos.plugin.datasource.impl.oracle.ConfigInfoMapperByOracle
com.alibaba.nacos.plugin.datasource.impl.oracle.ConfigInfoTagMapperByOracle
com.alibaba.nacos.plugin.datasource.impl.oracle.ConfigTagsRelationMapperByOracle
com.alibaba.nacos.plugin.datasource.impl.oracle.HistoryConfigInfoMapperByOracle
com.alibaba.nacos.plugin.datasource.impl.oracle.TenantInfoMapperByOracle
com.alibaba.nacos.plugin.datasource.impl.oracle.TenantCapacityMapperByOracle
com.alibaba.nacos.plugin.datasource.impl.oracle.GroupCapacityMapperByOracle
修改配置文件:console/src/main/resources/application.properties
只修改一部分,数据源连接信息,截图如下:

nacos.plugin.datasource.log.enabled=true
spring.sql.init.platform=oracle
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:oracle:thin:@192.168.10.2:1521/xe
db.user.0=nacos220
db.password.0=123456
db.jdbcDriverName=oracle.jdbc.OracleDriver
db.testQuery=select 1 from dual
在nacos-all目录下执行mvn命令
mvn -Prelease-nacos -Dmaven.test.skip=true -Dpmd.skip=true -Drat.skip=true -Dcheckstyle.skip=true clean install -U
最后在distributioni目录下的target就会出现压缩文件了,这样就相当于从nacos官网直接下载的压缩包,具体nacos配置可以自行完成。

以上就是Nacos2.2.0多数据源适配oracle12C-修改Nacos源码的步骤
曾经淋过雨,希望大家都有伞!
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。问题1)我想知道rubyonrails是否有功能类似于primefaces的gem。我问的原因是如果您使用primefaces(http://www.primefaces.org/showcase-labs/ui/home.jsf),开发人员无需担心javascript或jquery的东西。据我所知,JSF是一个规范,基于规范的各种可用实现,prim
我的项目布局如下:-Project-css-import.scss-_sass/main.scssimport.scss的内容是:------@import"main.scss";我期望发生的是将main.scss导入到import.scss中,然后,import.scss将在生成的_site/目录中编译为import.css。相反,我收到以下错误Conversionerror:Therewasanerrorconverting'css/import.scss'.jekyll2.0.3|Error:InvalidCSSafter"-":expectednumberorfunction,
Enumerable#each和Enumerable#map的区别在于返回的是接收者还是映射后的结果。回到接收者是微不足道的,你通常不需要在each之后继续一个方法链,比如each{...}.another_method(我可能没见过这样的案例。即使你想回到接收者那里,你也可以通过tap来实现)。所以我认为所有或者大部分使用Enumerable#each的情况都可以用Enumerable#map代替。我错了吗?如果我是对的,each的目的是什么?map是否比each慢?编辑:我知道当您对返回值不感兴趣时使用each是一种常见的做法。我对这种做法是否存在不感兴趣,但感兴趣的是,除了从
plsql连接Oracle超时,完犊子了肯定是服务器断电了。得马上检查Oracle服务器状态1、检查数据库是否启动su-oracle切换到Oracle用户,输入sqlplus/assysdba显示连接状态。如果末尾显示的状态是Connectedtoanidleinstance.证明未启动2、启动数据库startup启动数据库,末尾出现Databaseopened说明数据库启动成功3、查看数据库监听是否正常先quit;断开Oracle连接,使用lsnrctlstatus查看监听状态,如果出现TNS-开头的Nolistener、Connectionrefused等错误,说明监听未启动4、启动数据库
我想在AmazonOpsWorks上使用Ruby2.0,所以我正在尝试以下操作:选择自定义Recipe并将它们设置到我的forkhttps://github.com/aws/opsworks-cookbooks在此处更新所有版本号https://github.com/aws/opsworks-cookbooks/blob/master/ruby/attributes/ruby.rb到2.0值。虽然这似乎没有任何效果。自定义说明书是否会覆盖其内置的说明书?OpsWorks是否使用Recipe中的Ruby配方来进行基本的Ruby设置?同样的问题也适用于Nginx-我可以通过更改Recipe
我正在尝试将Ruby1.9.3应用程序升级到2.0,除了一个小问题外,一切似乎都很顺利。我写了一个模块,我将其包含在我的模型中以覆盖activerecorddestroy。它将现有的destroy方法别名为destroy!,然后覆盖destroy以更改记录上的deleted_at时间戳。仅当我升级到ruby2.0时,destroy!不再破坏记录,但其行为就像我的新覆盖方法一样。知道为什么会这样吗?下面是更相关的代码部分。完整要点here.defself.included(base)base.class_evaldoalias_method:destroy!,:destroyalia
一些我找到的选项是ActiveCouchCouchRESTCouchPotatoRelaxDBcouch_foo我更喜欢GitHub上的项目,因为这让我更容易fork和推送修复。所有这些都符合该要求。我习惯了Rails,所以我喜欢像ActiveRecord模型一样工作的东西。另一方面,我也不希望我和Couch之间太多--毕竟我使用它作为我的数据库是有原因的。最后,它们似乎都得到了相当积极的维护(couch_foo可能是个异常(exception))。所以我想这归结为(不可否认和不幸的)主观:有没有人对他们有过好的或坏的经历? 最佳答案
我已经克隆了Diaspora的源代码,并且我有一个可以正常运行的本地pod,它似乎可以正常运行。但是当我运行$rakespec时,一些初始测试通过了,然后所有测试都开始失败。此外,我发现有趣的是,每次运行rakespec时,它们都会在不同的点失败。它们都因错误而失败:AnerroroccurredinanafterhookActiveRecord::StatementInvalid:PG::ConnectionBad:PQsocket()can'tgetsocketdescriptor:ROLLBACKoccurredat/home/darshan/.rvm/gems/ruby-2.0
我尝试用Ruby设计一个基于Web的应用程序。我开发了一个简单的核心应用程序,在没有框架和数据库的情况下在六边形架构中实现DCI范例。核心六边形中有小六边形和网络,数据库,日志等适配器。每个六边形都在没有数据库和框架的情况下自行运行。在这种方法中,我如何提供与数据库模型和实体类的关系作为独立于数据库的关系。我想在将来将框架从Rails更改为Sinatra或数据库。事实上,我如何在这个核心Hexagon中实现完全隔离的rails和mongodb的数据库适配器或框架适配器。有什么想法吗? 最佳答案 ROM呢?(Ruby对象映射器)。还有
如果我在ruby中有一个接受命名参数的方法...defsmoosh(first:nil,second:nil)first+secondend如果键匹配,将散列传递给该方法的最简单方法是什么:params={first:'peanut',second:'butter'}smoosh(params)以上会产生参数错误。更新:这似乎是Sinatra参数工作方式的问题。当我这样做时:get'a_sinatra_route'dohash=params.clonehash.symbolize_keys!smoosh(hash)end它工作正常。仅自行传递参数时,它不起作用。(即使您可以使用符号