提交 741ace20 编写于 作者: sq3536's avatar sq3536

enableApi逻辑,datasource配置逻辑升级

上级 645b656c
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
<revision>2.4.0-SNAPSHOT</revision> <revision>2.4.0-SNAPSHOT</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<ibiz-boot-starter.version>2.4.0-SNAPSHOT</ibiz-boot-starter.version> <ibiz-boot-starter.version>2.4.0-SNAPSHOT</ibiz-boot-starter.version>
<ibiz.cloud.version>8.1.0.181</ibiz.cloud.version> <ibiz.cloud.version>8.1.0.205</ibiz.cloud.version>
<spring-cloud.version>2020.0.1</spring-cloud.version> <spring-cloud.version>2020.0.1</spring-cloud.version>
<spring-cloud-starter-bootstrap.version>3.0.1</spring-cloud-starter-bootstrap.version> <spring-cloud-starter-bootstrap.version>3.0.1</spring-cloud-starter-bootstrap.version>
<fastjson.version>1.2.83</fastjson.version> <fastjson.version>1.2.83</fastjson.version>
......
...@@ -9,7 +9,10 @@ import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSour ...@@ -9,7 +9,10 @@ import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSour
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.ibizsys.central.ISystemRuntimeSetting;
import net.ibizsys.central.cloud.core.ServiceHubSettingBase; import net.ibizsys.central.cloud.core.ServiceHubSettingBase;
import net.ibizsys.central.cloud.core.util.ConfigEntity;
import net.ibizsys.central.cloud.core.util.NacosConfigUtils;
import net.ibizsys.central.cloud.core.util.domain.DataSource; import net.ibizsys.central.cloud.core.util.domain.DataSource;
import net.ibizsys.central.cloud.core.util.domain.DeploySystem; import net.ibizsys.central.cloud.core.util.domain.DeploySystem;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -44,6 +47,11 @@ public class BootServiceHubSetting extends ServiceHubSettingBase { ...@@ -44,6 +47,11 @@ public class BootServiceHubSetting extends ServiceHubSettingBase {
*/ */
public final static String DATAID_DBINST_PREFIX = "dbinst-"; public final static String DATAID_DBINST_PREFIX = "dbinst-";
/**
* 参数:默认数据库实例标记
*/
public final static String PARAM_DEFAULTDBINSTTAG = "defaultdbinsttag";
@JsonIgnore @JsonIgnore
@JSONField(serialize = false) @JSONField(serialize = false)
@Autowired @Autowired
...@@ -111,11 +119,11 @@ public class BootServiceHubSetting extends ServiceHubSettingBase { ...@@ -111,11 +119,11 @@ public class BootServiceHubSetting extends ServiceHubSettingBase {
Map<String, Object> commonSettings = null; Map<String, Object> commonSettings = null;
List<DataSource> dataSourceList = new ArrayList<DataSource>(); List<DataSource> dataSourceList = new ArrayList<DataSource>();
Map<String, DataSource> dataSourceMap = new HashMap<String, DataSource>();
Yaml yaml = new Yaml();
// 本方法启动的时候获取内容 // 本方法启动的时候获取内容
String content = configService.getConfig(DATAID_SERVICEHUB_PREFIX + this.getId(), this.nacosConfigProperties.getGroup(), 5000); String content = configService.getConfig(DATAID_SERVICEHUB_PREFIX + this.getId(), this.nacosConfigProperties.getGroup(), 5000);
if(StringUtils.hasLength(content)) { if(StringUtils.hasLength(content)) {
Yaml yaml = new Yaml();
Map config = yaml.loadAs(content, Map.class); Map config = yaml.loadAs(content, Map.class);
Object value = config.get("enableappgateway"); Object value = config.get("enableappgateway");
...@@ -155,30 +163,61 @@ public class BootServiceHubSetting extends ServiceHubSettingBase { ...@@ -155,30 +163,61 @@ public class BootServiceHubSetting extends ServiceHubSettingBase {
Object objDataSources = config.get("datasources"); Object objDataSources = config.get("datasources");
if (objDataSources instanceof Map) { if (objDataSources instanceof Map) {
Map<String, Object> map = (Map<String, Object>) objDataSources; Map<String, Object> map = (Map<String, Object>) objDataSources;
for (Map.Entry<String, Object> entry : map.entrySet()) { for (java.util.Map.Entry<String, Object> entry : map.entrySet()) {
DataSource dataSource = new DataSource(); DataSource dataSource = new DataSource();
Map<String, Object> source = (Map<String, Object>) entry.getValue(); Object objDataSource = entry.getValue();
dataSource.putAll(source); if(objDataSource instanceof Map) {
//进一步处理属性,这些属性将完全复制之数据源配置
for (Map.Entry<String, Object> item : source.entrySet()) { Map<String, Object> source = (Map<String, Object>)objDataSource;
String strKey = item.getKey().replace("-", ""); dataSource.putAll(source);
if (strKey.equals(item.getKey())) { //进一步处理属性,这些属性将完全复制之数据源配置
continue; for (java.util.Map.Entry<String, Object> item : source.entrySet()) {
String strKey = item.getKey().replace("-", "");
if(strKey.equals(item.getKey())) {
continue;
}
dataSource.set(strKey, item.getValue());
}
if (ObjectUtils.isEmpty(dataSource.getDriverClassName())) {
dataSource.setDriverClassName((String) dataSource.get("driver-class-name"));
} }
dataSource.set(strKey, item.getValue());
} }
else {
String strDBInstConfigKey = String.format("%1$s%2$s", DATAID_DBINST_PREFIX, objDataSource);
String strBackupConfigId = strDBInstConfigKey;
strDBInstConfigKey = NacosConfigUtils.getDataId(strBackupConfigId);
if(!strBackupConfigId.equals(strDBInstConfigKey)) {
log.warn(String.format("键名[%1$s]包含非法字符,转换至[%2$s],可能会出现键名冲突", strBackupConfigId, strDBInstConfigKey));
}
String strDBInstConfig = configService.getConfig(strDBInstConfigKey, this.nacosConfigProperties.getGroup(), 5000);
if(!StringUtils.hasLength(strDBInstConfig)) {
log.warn(String.format("未定义数据库实例[%1$s]配置,访问可能会出现问题", strDBInstConfigKey));
continue;
}
dataSource.setDataSourceId(entry.getKey()); ConfigEntity configEntity = new ConfigEntity(strDBInstConfig);
if (ObjectUtils.isEmpty(dataSource.getDriverClassName())) { String strDBInstConfigExKey = String.format("%1$s%2$s-ex", DATAID_DBINST_PREFIX, objDataSource);
dataSource.setDriverClassName((String) dataSource.get("driver-class-name")); strDBInstConfigExKey = NacosConfigUtils.getDataId(strDBInstConfigExKey);
String strDBInstConfigEx = configService.getConfig(strDBInstConfigExKey, this.nacosConfigProperties.getGroup(), 5000);
if(StringUtils.hasLength(strDBInstConfigExKey)) {
ConfigEntity configexEntity = new ConfigEntity(strDBInstConfigEx);
configexEntity.copyTo(configEntity);
}
configEntity.copyTo(dataSource);
} }
if (ObjectUtils.isEmpty(dataSource.getJdbcUrl())) { if (ObjectUtils.isEmpty(dataSource.getJdbcUrl())) {
dataSource.setJdbcUrl((String) dataSource.get("url")); dataSource.setJdbcUrl((String) dataSource.get("url"));
} }
dataSource.setDataSourceId(entry.getKey());
dataSourceList.add(dataSource); dataSourceList.add(dataSource);
dataSourceMap.put(dataSource.getDataSourceId(), dataSource);
} }
this.setDataSources(dataSourceList); this.setDataSources(dataSourceList);
} }
...@@ -197,6 +236,7 @@ public class BootServiceHubSetting extends ServiceHubSettingBase { ...@@ -197,6 +236,7 @@ public class BootServiceHubSetting extends ServiceHubSettingBase {
dataSource.setUsername(entry.getValue().getUsername()); dataSource.setUsername(entry.getValue().getUsername());
dataSource.setPassword(entry.getValue().getPassword()); dataSource.setPassword(entry.getValue().getPassword());
dataSourceList.add(dataSource); dataSourceList.add(dataSource);
dataSourceMap.put(dataSource.getDataSourceId(), dataSource);
}); });
this.setDataSources(dataSourceList); this.setDataSources(dataSourceList);
} }
...@@ -208,10 +248,19 @@ public class BootServiceHubSetting extends ServiceHubSettingBase { ...@@ -208,10 +248,19 @@ public class BootServiceHubSetting extends ServiceHubSettingBase {
if (ObjectUtils.isEmpty(item)) { if (ObjectUtils.isEmpty(item)) {
continue; continue;
} }
//判断是否指定接口
String strEnableAPIs = "";
String strItem = item.toString();
int nPos = strItem.indexOf(":");
if(nPos != -1) {
item = strItem.substring(0, nPos);
strEnableAPIs = strItem.substring(nPos + 1);
}
DeploySystem deploySystem = new DeploySystem(); DeploySystem deploySystem = new DeploySystem();
String deploySystemConfig = configService.getConfig(DATAID_DEPLOYSYSTEM_PREFIX + item.toString(), this.nacosConfigProperties.getGroup(), 5000); String deploySystemConfig = configService.getConfig(DATAID_DEPLOYSYSTEM_PREFIX + item.toString(), this.nacosConfigProperties.getGroup(), 5000);
if (StringUtils.hasLength(deploySystemConfig)) { if (StringUtils.hasLength(deploySystemConfig)) {
Yaml yaml = new Yaml();
Map map2 = yaml.loadAs(deploySystemConfig, Map.class); Map map2 = yaml.loadAs(deploySystemConfig, Map.class);
deploySystem.putAll(map2); deploySystem.putAll(map2);
deploySystem.setDeploySystemId(item.toString()); deploySystem.setDeploySystemId(item.toString());
...@@ -230,6 +279,23 @@ public class BootServiceHubSetting extends ServiceHubSettingBase { ...@@ -230,6 +279,23 @@ public class BootServiceHubSetting extends ServiceHubSettingBase {
} }
deploySystem.setSettings(settings); deploySystem.setSettings(settings);
} }
//放入启动接口
if(StringUtils.hasLength(strEnableAPIs)) {
if(deploySystem.getSettings()==null) {
deploySystem.setSettings(new HashMap<String, Object>());
}
deploySystem.getSettings().put(ISystemRuntimeSetting.PARAM_ENABLEAPIS, strEnableAPIs);
//deploySystem.setAPIs(Arrays.asList(strInterfaces.split("[;]")));
}
//判断是否存在默认数据源
String strDefaultDBInstTag = String.format("%1$s__default", deploySystem.getDeploySystemId());
if(dataSourceMap.containsKey(strDefaultDBInstTag)) {
if(deploySystem.getSettings()==null) {
deploySystem.setSettings(new HashMap<String, Object>());
}
deploySystem.getSettings().put(PARAM_DEFAULTDBINSTTAG, strDefaultDBInstTag);
}
deploySystemList.add(deploySystem); deploySystemList.add(deploySystem);
} }
this.setDeploySystems(deploySystemList); this.setDeploySystems(deploySystemList);
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册