提交 7ed6e397 编写于 作者: sq3536's avatar sq3536

配置文件

上级 6389eda0
......@@ -22,11 +22,12 @@
<revision>2.4.0-SNAPSHOT</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<ibiz-boot-starter.version>2.4.0-SNAPSHOT</ibiz-boot-starter.version>
<ibiz.cloud.version>8.1.0.132</ibiz.cloud.version>
<ibiz.cloud.version>8.1.0.151</ibiz.cloud.version>
<spring-cloud.version>2020.0.1</spring-cloud.version>
<spring-cloud-starter-bootstrap.version>3.0.1</spring-cloud-starter-bootstrap.version>
<fastjson.version>1.2.83</fastjson.version>
<zalando-problem-spring-web.version>0.23.0</zalando-problem-spring-web.version>
<druid-spring-boot-starter.version>1.2.11</druid-spring-boot-starter.version>
<maven.deploy.skip>false</maven.deploy.skip>
<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
</properties>
......@@ -88,6 +89,12 @@
<version>${ibiz-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-spring-boot-starter.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
......
......@@ -88,10 +88,11 @@
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-bootstrap</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
......
package net.ibizsys.central.plugin.boot.core.runtime;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
import java.util.Map;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Component
@ConfigurationProperties(prefix="ibiz")
public class BootDeploySystems{
private Map<String,BootDeploySystem> deploySystems=new LinkedHashMap<>();
public void setDeploySystems(Map<String, BootDeploySystem> deploySystems) {
if(deploySystems!=null)
deploySystems.entrySet().forEach(entry->{
entry.getValue().setDeploySystemId(entry.getKey());
});
this.deploySystems = deploySystems;
}
public BootDeploySystem get(String systemId) {
return getDeploySystem(systemId);
}
public BootDeploySystem getDeploySystem(String systemId) {
return deploySystems.get(systemId);
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class BootDeploySystem {
@JsonProperty("deploysystemid")
@JSONField(name = "deploysystemid")
private String deploySystemId;
@JsonProperty("runtimeclassname")
@JSONField(name = "runtimeclassname")
private String runtimeClassName;
@JsonProperty("modelpath")
@JSONField(name = "modelpath")
private String modelPath;
@NestedConfigurationProperty
private BootDeploySystemSetting settings;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class BootDeploySystemSetting {
@JsonProperty("sysdbscheme")
@JSONField(name = "sysdbscheme")
private Map<String, SysDBScheme> sysDBScheme;
public void addSysDBScheme(String key,SysDBScheme value) {
if(sysDBScheme==null)
sysDBScheme=new LinkedHashMap<>();
sysDBScheme.put(key,value);
}
@JsonProperty("sysbdscheme")
@JSONField(name = "sysbdscheme")
private Map<String, SysBDScheme> sysBDScheme;
public void addSysBDScheme(String key,SysBDScheme value) {
if(sysBDScheme==null)
sysBDScheme=new LinkedHashMap<>();
sysBDScheme.put(key,value);
}
@JsonProperty("subsysserviceapi")
@JSONField(name = "subsysserviceapi")
@NestedConfigurationProperty
private SubApis subSysServiceApi;
@JsonProperty("cloudclientutil")
@JSONField(name = "cloudclientutil")
@NestedConfigurationProperty
private SubSysServiceApi cloudClientUtil;
@JsonProperty("sysutil")
@JSONField(name = "sysutil")
@NestedConfigurationProperty
private SysUtil sysUtil;
public void addSysUtil(String key,Map value) {
if(sysUtil==null)
sysUtil=new SysUtil();
sysUtil.put(key,value);
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class SysDBScheme {
@JsonProperty("datasource")
@JSONField(name = "datasource")
private String dataSource;
@JsonProperty("dbtype")
@JSONField(name = "dbtype")
private String dbType;
}
public static class SubApis extends LinkedHashMap<String,SubApiModule>{
}
public static class SubApiModule extends LinkedHashMap<String,SubSysServiceApi>{
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class SubSysServiceApi {
@JsonProperty("serviceurl")
@JSONField(name = "serviceurl")
private String serviceUrl ;
@JsonProperty("globalheadermap")
@JSONField(name = "globalheadermap")
private Map<String, Object> globalHeaderMap;
@JsonProperty("defaultheadermap")
@JSONField(name = "defaultheadermap")
private Map<String, Object> defaultHeaderMap;
@JsonProperty("accesstokenurl")
@JSONField(name = "accesstokenurl")
private String accessTokenUrl;
@JsonProperty("authmode")
@JSONField(name = "authmode")
private String authMode;
@JsonProperty("clientid")
@JSONField(name = "clientid")
private String clientId = null;
@JsonProperty("clientsecret")
@JSONField(name = "clientsecret")
private String clientSecret = null;
@JsonProperty("tokentimeout")
@JSONField(name = "tokentimeout")
private Long tokenTimeout;
@JsonProperty("defaulttokentimeout")
@JSONField(name = "defaulttokentimeout")
private Integer defaultTokenTimeout;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class SysBDScheme {
@JsonProperty("serviceurl")
@JSONField(name = "serviceurl")
private String serviceUrl;
}
public static class SysUtil extends LinkedHashMap<String,Map>{
}
}
package net.ibizsys.central.plugin.boot.core.runtime;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import net.ibizsys.central.cloud.core.ServiceHubBase;
import net.ibizsys.central.cloud.core.service.ISysServiceAPIDocAdapter;
import net.ibizsys.central.cloud.core.util.domain.DataSource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import javax.annotation.PostConstruct;
import java.sql.Connection;
@Component("ServiceHub")
public class BootServiceHub extends ServiceHubBase implements ApplicationContextAware,ApplicationRunner{
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(BootServiceHub.class);
@Autowired
BootServiceHubSetting iServiceHubSetting;
@Autowired
RequestMappingHandlerMapping requestMappingHandlerMapping;
@Autowired(required=false)
ISysServiceAPIDocAdapter sysServiceAPIDocAdapter = null;
@Autowired
NacosDiscoveryProperties nacosDiscoveryProperties;
@Autowired
ServerProperties serverProperties;
@Autowired(required=false)
javax.sql.DataSource dataSource = null;
@Autowired
@Lazy
private DataSourceCreator druidDataSourceCreator;
private ConfigurableApplicationContext ctx;
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.ctx = (ConfigurableApplicationContext) ctx;
}
@PostConstruct
public void postConstruct() {
this.setServiceHubSetting(iServiceHubSetting);
this.setDefaultDataSource(dataSource);
this.setRequestMappingHandlerMapping(requestMappingHandlerMapping);
this.setSysServiceAPIDocAdapter(sysServiceAPIDocAdapter);
onPostConstruct();
}
protected void registerDataSource(DataSource ds) throws Exception{
DataSourceProperty dataSourceProperty = new DataSourceProperty();
dataSourceProperty.setDriverClassName(ds.getDriverClassName());
dataSourceProperty.setUrl(ds.getJdbcUrl());
dataSourceProperty.setUsername(ds.getUsername());
dataSourceProperty.setPassword(ds.getPassword());
javax.sql.DataSource dataSource = druidDataSourceCreator.createDataSource(dataSourceProperty);
Connection connection = dataSource.getConnection();
ctx.getBeanFactory().registerSingleton("dynadatasource_"+ds.getDataSourceId(), dataSource);
try {
connection.close();
}
catch(Exception ex) {
log.error(ex);
}
this.setDataSource(ds.getDataSourceId(), dataSource);
}
@Override
public void run(ApplicationArguments args) throws Exception {
this.install();
}
}
package net.ibizsys.central.plugin.boot.core.runtime;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.api.config.ConfigService;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.extern.slf4j.Slf4j;
import net.ibizsys.central.cloud.core.ServiceHubSettingBase;
import net.ibizsys.central.cloud.core.util.domain.DataSource;
import net.ibizsys.central.cloud.core.util.domain.DeploySystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.yaml.snakeyaml.Yaml;
import javax.annotation.PostConstruct;
import java.util.*;
@Component
@ConfigurationProperties(BootServiceHubSetting.PREFIX)
@Slf4j
public class BootServiceHubSetting extends ServiceHubSettingBase {
/**
* 数据项:服务网关前缀
*/
public final static String DATAID_SERVICEHUB_PREFIX = "servicehub-";
/**
* 数据项:部署系统前缀
*/
public final static String DATAID_DEPLOYSYSTEM_PREFIX = "deploysystem-";
/**
* 数据项:数据库实例前缀
*/
public final static String DATAID_DBINST_PREFIX = "dbinst-";
@Autowired
private NacosConfigManager nacosConfigManager;
@Autowired
NacosConfigProperties nacosConfigProperties;
@Autowired
BootDeploySystems bootDeploySystems;
@Autowired
DynamicDataSourceProperties dynamicDataSourceProperties;
private List<String> loadDeploySystems;
public List<String> getLoadDeploySystems() {
return loadDeploySystems;
}
public void setLoadDeploySystems(List<String> loadDeploySystems) {
this.loadDeploySystems = loadDeploySystems;
}
@JsonProperty("systemsettings")
@JSONField(name = "systemsettings")
@NestedConfigurationProperty
private BootDeploySystems.BootDeploySystemSetting systemSettings;
public BootDeploySystems.BootDeploySystemSetting getSystemSettings() {
return systemSettings;
}
public void setSystemSettings(BootDeploySystems.BootDeploySystemSetting systemSettings) {
this.systemSettings = systemSettings;
}
@PostConstruct
public void postConstruct() {
loadConfig();
}
private boolean bLoadConfig = false;
public synchronized void loadConfig() {
if (bLoadConfig) {
return;
}
try {
onLoadConfig();
bLoadConfig = true;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
protected void onLoadConfig() throws Exception {
ConfigService configService = nacosConfigManager.getConfigService();
Map<String, Object> commonSettings = null;
List<DataSource> dataSourceList = new ArrayList<DataSource>();
// 本方法启动的时候获取内容
String content = configService.getConfig(DATAID_SERVICEHUB_PREFIX + this.getId(), this.nacosConfigProperties.getGroup(), 5000);
if(StringUtils.hasLength(content)) {
Yaml yaml = new Yaml();
Map config = yaml.loadAs(content, Map.class);
Object value = config.get("enableappgateway");
if (value != null) {
this.setEnableAppGateway(value.toString().equalsIgnoreCase("true"));
}
value = config.get("tempfolder");
if (value != null) {
this.setTempFolder(value.toString());
}
value = config.get("filefolder");
if (value != null) {
this.setFileFolder(value.toString());
}
value = config.get("fontfolder");
if (value != null) {
this.setFontFolder(value.toString());
}
value = config.get("reportfolder");
if (value != null) {
this.setReportFolder(value.toString());
}
value = config.get("node");
if (value != null) {
this.setNode(Long.valueOf(value.toString()));
}
if (config.get("systemsettings") instanceof Map) {
commonSettings = (Map) config.get("systemsettings");
}
Object objDeploySystems = config.get("deploysystems");
if (objDeploySystems instanceof List)
loadDeploySystems = (List) objDeploySystems;
Object objDataSources = config.get("datasources");
if (objDataSources instanceof Map) {
Map<String, Object> map = (Map<String, Object>) objDataSources;
for (Map.Entry<String, Object> entry : map.entrySet()) {
DataSource dataSource = new DataSource();
Map<String, Object> source = (Map<String, Object>) entry.getValue();
dataSource.putAll(source);
//进一步处理属性,这些属性将完全复制之数据源配置
for (Map.Entry<String, Object> item : source.entrySet()) {
String strKey = item.getKey().replace("-", "");
if (strKey.equals(item.getKey())) {
continue;
}
dataSource.set(strKey, item.getValue());
}
dataSource.setDataSourceId(entry.getKey());
if (ObjectUtils.isEmpty(dataSource.getDriverClassName())) {
dataSource.setDriverClassName((String) dataSource.get("driver-class-name"));
}
if (ObjectUtils.isEmpty(dataSource.getJdbcUrl())) {
dataSource.setJdbcUrl((String) dataSource.get("url"));
}
dataSourceList.add(dataSource);
}
this.setDataSources(dataSourceList);
}
}
else {
log.warn(String.format("无法获取远程配置[%1$s],使用本地配置文件", DATAID_SERVICEHUB_PREFIX + this.getId()));
if(!ObjectUtils.isEmpty(this.systemSettings))
commonSettings= JSON.parseObject(JSON.toJSONString(this.systemSettings), LinkedHashMap.class);
dynamicDataSourceProperties.getDatasource().entrySet().forEach(entry->{
DataSource dataSource = new DataSource();
dataSource.setDataSourceId(getId()+"__"+(entry.getKey().equalsIgnoreCase("master")?"default":entry.getKey()));
dataSource.setDriverClassName(entry.getValue().getDriverClassName());
dataSource.setJdbcUrl(entry.getValue().getUrl());
dataSource.setUsername(entry.getValue().getUsername());
dataSource.setPassword(entry.getValue().getPassword());
dataSourceList.add(dataSource);
});
this.setDataSources(dataSourceList);
}
if(!ObjectUtils.isEmpty(loadDeploySystems))
{
List<DeploySystem> deploySystemList = new ArrayList<DeploySystem>();
for (Object item : loadDeploySystems) {
if (ObjectUtils.isEmpty(item)) {
continue;
}
DeploySystem deploySystem = new DeploySystem();
String deploySystemConfig = configService.getConfig(DATAID_DEPLOYSYSTEM_PREFIX + item.toString(), this.nacosConfigProperties.getGroup(), 5000);
if (StringUtils.hasLength(deploySystemConfig)) {
Yaml yaml = new Yaml();
Map map2 = yaml.loadAs(deploySystemConfig, Map.class);
deploySystem.putAll(map2);
deploySystem.setDeploySystemId(item.toString());
}
else if(bootDeploySystems.getDeploySystem(item.toString())!=null){
deploySystem.putAll(JSON.parseObject(JSON.toJSONString(bootDeploySystems.getDeploySystem(item.toString())),LinkedHashMap.class));
}
else
continue;
// 合并设置
if (commonSettings != null) {
Map<String, Object> settings = new HashMap<String, Object>();
settings.putAll(commonSettings);
if (deploySystem.getSettings() != null) {
settings.putAll(deploySystem.getSettings());
}
deploySystem.setSettings(settings);
}
deploySystemList.add(deploySystem);
}
this.setDeploySystems(deploySystemList);
}
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册