All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ibizsys.model.engine.cloud.util.NacosCloudConfigService Maven / Gradle / Ivy

The newest version!
package net.ibizsys.model.engine.cloud.util;

import java.util.Map;

import org.springframework.util.StringUtils;
import org.yaml.snakeyaml.Yaml;

import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;

import net.ibizsys.model.engine.cloud.util.domain.DeploySystem;
import net.ibizsys.model.engine.plugin.PSModelEngineAddinException;

public class NacosCloudConfigService extends CloudConfigServiceBase {

	public final static String PARAM_CLOUD_NACOS_CONFIG_SERVERADDR = "cloud.nacos.config.server-addr";
	
	public final static String PARAM_CLOUD_NACOS_CONFIG_GROUP = "cloud.nacos.config.group";
	
	private String configGroup = null;
	private ConfigService configService = null;
	private static Yaml yaml = new Yaml();
	
	@Override
	protected void onInit() throws Exception {
		if(this.getConfigService(true) == null) {
			prepareConfigService();
			this.getConfigService(false);
		}
		super.onInit();
	}
	
	
	protected void setConfigService(ConfigService configService){
		this.configService = configService;
	}
	
//	protected ConfigService getConfigService(){
//		try {
//			return this.getConfigService(false);
//		}
//		catch (Throwable ex) {
//			throw new PSModelEngineAddinException(this, ex.getMessage(), ex);
//		}
//	}

	
	protected ConfigService getConfigService(boolean tryMode) throws Exception{
		if(this.configService != null || tryMode) {
			return this.configService;
		}
		throw new Exception("Nacos配置服务对象无效");
	}
	
	protected void prepareConfigService() throws Exception{
		String nacosAddr = this.getPSModelEngineHolder().getParam(PARAM_CLOUD_NACOS_CONFIG_SERVERADDR, null);
		if(!StringUtils.hasLength(nacosAddr)) {
			throw new Exception(String.format("Nacos服务器地址无效"));
		}
		this.setConfigGroup(this.getPSModelEngineHolder().getParam(PARAM_CLOUD_NACOS_CONFIG_GROUP, "default"));
		ConfigService configService = ConfigFactory.createConfigService(nacosAddr);
		this.setConfigService(configService);
	}
	
	protected void setConfigGroup(String configGroup) {
		this.configGroup = configGroup;
	}
	
	protected String getConfigGroup() {
		return this.configGroup ;
	}


	@Override
	public DeploySystem getDeploySystem(String systemId) {
		try {
			return onGetDeploySystem(systemId);
		}
		catch (Throwable ex) {
			throw new PSModelEngineAddinException(this, String.format("获取Cloud部署系统发生异常,%1$s", ex.getMessage()), ex);
		}
	}
	
	protected DeploySystem onGetDeploySystem(String systemId) throws Throwable{
		String deploySystemConfig = this.getConfigService(false).getConfig(DATAID_DEPLOYSYSTEM_PREFIX + systemId, this.getConfigGroup(), 5000);
		if (!StringUtils.hasLength(deploySystemConfig)) {
			throw new Exception(String.format("部署系统[%1$s]配置无效", systemId.toString()));
		}
		Map map2 = yaml.loadAs(deploySystemConfig, Map.class);
		DeploySystem deploySystem = new DeploySystem();
		deploySystem.putAll(map2);
		deploySystem.setDeploySystemId(systemId);
		return deploySystem;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy