mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
refactor sentinel datasource and support commercialized
This commit is contained in:
parent
24ead9599d
commit
bf40e5261d
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.cloud.alibaba.sentinel.datasource;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
|
||||
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
|
||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
|
||||
/**
|
||||
* {@link NacosDataSource} now is not support ak、sk,namespace and endpoint. This class may
|
||||
* be delete when {@link NacosDataSource} support commercialized
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class NacosDataSourceWithAuthorization<T> extends AbstractDataSource<String, T> {
|
||||
|
||||
private static final int DEFAULT_TIMEOUT = 3000;
|
||||
|
||||
private final ExecutorService pool = new ThreadPoolExecutor(1, 1, 0,
|
||||
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1),
|
||||
new NamedThreadFactory("sentinel-nacos-auth-ds-update"),
|
||||
new ThreadPoolExecutor.DiscardOldestPolicy());
|
||||
|
||||
private final Listener configListener;
|
||||
private final Properties properties;
|
||||
private final String dataId;
|
||||
private final String groupId;
|
||||
|
||||
private ConfigService configService = null;
|
||||
|
||||
public NacosDataSourceWithAuthorization(final Properties properties,
|
||||
final String groupId, final String dataId, Converter<String, T> parser) {
|
||||
super(parser);
|
||||
if (StringUtil.isBlank(groupId) || StringUtil.isBlank(dataId)) {
|
||||
throw new IllegalArgumentException(String
|
||||
.format("Bad argument: groupId=[%s], dataId=[%s]", groupId, dataId));
|
||||
}
|
||||
this.groupId = groupId;
|
||||
this.dataId = dataId;
|
||||
this.properties = properties;
|
||||
this.configListener = new Listener() {
|
||||
@Override
|
||||
public Executor getExecutor() {
|
||||
return pool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveConfigInfo(final String configInfo) {
|
||||
RecordLog.info(String.format(
|
||||
"[NacosDataSourceWithAuthorization] New property value received for %s",
|
||||
properties.toString()));
|
||||
T newValue = NacosDataSourceWithAuthorization.this.parser
|
||||
.convert(configInfo);
|
||||
// Update the new value to the property.
|
||||
getProperty().updateValue(newValue);
|
||||
}
|
||||
};
|
||||
initNacosListener();
|
||||
loadInitialConfig();
|
||||
}
|
||||
|
||||
private void loadInitialConfig() {
|
||||
try {
|
||||
T newValue = loadConfig();
|
||||
if (newValue == null) {
|
||||
RecordLog.warn(
|
||||
"[NacosDataSourceWithAuthorization] WARN: initial config is null, you may have to check your data source");
|
||||
}
|
||||
getProperty().updateValue(newValue);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
RecordLog.warn(
|
||||
"[NacosDataSourceWithAuthorization] Error when loading initial config",
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void initNacosListener() {
|
||||
try {
|
||||
this.configService = NacosFactory.createConfigService(properties);
|
||||
// Add config listener.
|
||||
configService.addListener(dataId, groupId, configListener);
|
||||
}
|
||||
catch (Exception e) {
|
||||
RecordLog.warn(
|
||||
"[NacosDataSourceWithAuthorization] Error occurred when initializing Nacos data source",
|
||||
e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readSource() throws Exception {
|
||||
if (configService == null) {
|
||||
throw new IllegalStateException(
|
||||
"Nacos config service has not been initialized or error occurred");
|
||||
}
|
||||
return configService.getConfig(dataId, groupId, DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (configService != null) {
|
||||
configService.removeListener(dataId, groupId, configListener);
|
||||
}
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
@ -21,13 +21,14 @@ package org.springframework.cloud.alibaba.sentinel.datasource;
|
||||
*/
|
||||
public interface SentinelDataSourceConstants {
|
||||
|
||||
String PROPERTY_PREFIX = "spring.cloud.sentinel";
|
||||
String PROPERTY_PREFIX = "spring.cloud.sentinel";
|
||||
|
||||
String PROPERTY_ITEM_SEPARATOR = ".";
|
||||
String NACOS_DATASOURCE_AK = PROPERTY_PREFIX + ".nacos.config.access-key";
|
||||
|
||||
String PROPERTY_DATASOURCE_NAME = "datasource";
|
||||
String NACOS_DATASOURCE_SK = PROPERTY_PREFIX + ".nacos.config.secret-key";
|
||||
|
||||
String PROPERTY_DATASOURCE_PREFIX = PROPERTY_PREFIX + PROPERTY_ITEM_SEPARATOR
|
||||
+ PROPERTY_DATASOURCE_NAME;
|
||||
String NACOS_DATASOURCE_NAMESPACE = PROPERTY_PREFIX + ".nacos.config.namespace";
|
||||
|
||||
String NACOS_DATASOURCE_ENDPOINT = PROPERTY_PREFIX + ".nacos.config.endpoint";
|
||||
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class AbstractDataSourceProperties {
|
||||
private String dataType = "json";
|
||||
private String converterClass;
|
||||
@JsonIgnore
|
||||
private final String factoryBeanName;
|
||||
protected String factoryBeanName;
|
||||
|
||||
public AbstractDataSourceProperties(String factoryBeanName) {
|
||||
this.factoryBeanName = factoryBeanName;
|
||||
@ -38,4 +38,12 @@ public class AbstractDataSourceProperties {
|
||||
return factoryBeanName;
|
||||
}
|
||||
|
||||
public void setFactoryBeanName(String factoryBeanName) {
|
||||
this.factoryBeanName = factoryBeanName;
|
||||
}
|
||||
|
||||
public void preCheck() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ public class DataSourcePropertiesConfiguration {
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public List<String> getInvalidField() {
|
||||
public List<String> getValidField() {
|
||||
return Arrays.stream(this.getClass().getDeclaredFields()).map(field -> {
|
||||
try {
|
||||
if (!ObjectUtils.isEmpty(field.get(this))) {
|
||||
@ -76,4 +76,24 @@ public class DataSourcePropertiesConfiguration {
|
||||
}).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public AbstractDataSourceProperties getValidDataSourceProperties() {
|
||||
List<String> invalidFields = getValidField();
|
||||
if (invalidFields.size() == 1) {
|
||||
try {
|
||||
this.getClass().getDeclaredField(invalidFields.get(0))
|
||||
.setAccessible(true);
|
||||
return (AbstractDataSourceProperties) this.getClass()
|
||||
.getDeclaredField(invalidFields.get(0)).get(this);
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
// won't happen
|
||||
}
|
||||
catch (NoSuchFieldException e) {
|
||||
// won't happen
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
package org.springframework.cloud.alibaba.sentinel.datasource.config;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* File Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
||||
@ -50,4 +54,19 @@ public class FileDataSourceProperties extends AbstractDataSourceProperties {
|
||||
public void setBufSize(int bufSize) {
|
||||
this.bufSize = bufSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preCheck() {
|
||||
super.preCheck();
|
||||
try {
|
||||
this.setFile(
|
||||
ResourceUtils.getFile(StringUtils.trimAllWhitespace(this.getFile()))
|
||||
.getAbsolutePath());
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException("[Sentinel Starter] " + " handle file ["
|
||||
+ this.getFile() + "] error: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package org.springframework.cloud.alibaba.sentinel.datasource.config;
|
||||
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants;
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceWithAuthorizationFactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Nacos Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
||||
@ -14,10 +17,35 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
||||
private String groupId;
|
||||
private String dataId;
|
||||
|
||||
// commercialized usage
|
||||
|
||||
private String endpoint;
|
||||
private String namespace;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
|
||||
public NacosDataSourceProperties() {
|
||||
super(NacosDataSourceFactoryBean.class.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preCheck() {
|
||||
if (!StringUtils.isEmpty(System.getProperties()
|
||||
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT))) {
|
||||
this.setServerAddr(null);
|
||||
this.setFactoryBeanName(
|
||||
NacosDataSourceWithAuthorizationFactoryBean.class.getName());
|
||||
this.setEndpoint(System.getProperties()
|
||||
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT));
|
||||
this.setNamespace(System.getProperties()
|
||||
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_NAMESPACE));
|
||||
this.setAccessKey(System.getProperties()
|
||||
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_AK));
|
||||
this.setSecretKey(System.getProperties()
|
||||
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_SK));
|
||||
}
|
||||
}
|
||||
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
}
|
||||
@ -41,4 +69,36 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
||||
public void setDataId(String dataId) {
|
||||
this.dataId = dataId;
|
||||
}
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,101 @@
|
||||
package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.NacosDataSourceWithAuthorization;
|
||||
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
|
||||
import com.alibaba.nacos.api.PropertyKeyConst;
|
||||
|
||||
/**
|
||||
* A {@link FactoryBean} for creating {@link NacosDataSource} instance.
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
* @see NacosDataSource
|
||||
*/
|
||||
public class NacosDataSourceWithAuthorizationFactoryBean
|
||||
implements FactoryBean<NacosDataSourceWithAuthorization> {
|
||||
|
||||
private String endpoint;
|
||||
private String namespace;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
|
||||
private String groupId;
|
||||
private String dataId;
|
||||
private Converter converter;
|
||||
|
||||
@Override
|
||||
public NacosDataSourceWithAuthorization getObject() throws Exception {
|
||||
Properties properties = new Properties();
|
||||
properties.put(PropertyKeyConst.ACCESS_KEY, accessKey);
|
||||
properties.put(PropertyKeyConst.SECRET_KEY, secretKey);
|
||||
properties.put(PropertyKeyConst.NAMESPACE, namespace);
|
||||
properties.put(PropertyKeyConst.ENDPOINT, endpoint);
|
||||
return new NacosDataSourceWithAuthorization(properties, groupId, dataId,
|
||||
converter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return NacosDataSourceWithAuthorization.class;
|
||||
}
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
|
||||
public String getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
public void setGroupId(String groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
public void setDataId(String dataId) {
|
||||
this.dataId = dataId;
|
||||
}
|
||||
|
||||
public Converter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
public void setConverter(Converter converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package org.springframework.cloud.alibaba.sentinel.custom;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -24,7 +23,6 @@ import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConver
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
|
||||
@ -77,49 +75,19 @@ public class SentinelDataSourceHandler {
|
||||
|
||||
sentinelProperties.getDatasource()
|
||||
.forEach((dataSourceName, dataSourceProperties) -> {
|
||||
if (dataSourceProperties.getInvalidField().size() != 1) {
|
||||
List<String> validFields = dataSourceProperties.getValidField();
|
||||
if (validFields.size() != 1) {
|
||||
logger.error("[Sentinel Starter] DataSource " + dataSourceName
|
||||
+ " multi datasource active and won't loaded: "
|
||||
+ dataSourceProperties.getInvalidField());
|
||||
+ dataSourceProperties.getValidField());
|
||||
return;
|
||||
}
|
||||
Optional.ofNullable(dataSourceProperties.getFile())
|
||||
.ifPresent(file -> {
|
||||
try {
|
||||
dataSourceProperties.getFile().setFile(ResourceUtils
|
||||
.getFile(StringUtils.trimAllWhitespace(
|
||||
dataSourceProperties.getFile()
|
||||
.getFile()))
|
||||
.getAbsolutePath());
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.error("[Sentinel Starter] DataSource "
|
||||
+ dataSourceName + " handle file error: "
|
||||
+ e.getMessage());
|
||||
throw new RuntimeException(
|
||||
"[Sentinel Starter] DataSource "
|
||||
+ dataSourceName
|
||||
+ " handle file error: "
|
||||
+ e.getMessage(),
|
||||
e);
|
||||
}
|
||||
registerBean(beanFactory, file,
|
||||
dataSourceName + "-sentinel-file-datasource");
|
||||
});
|
||||
Optional.ofNullable(dataSourceProperties.getNacos())
|
||||
.ifPresent(nacos -> {
|
||||
registerBean(beanFactory, nacos,
|
||||
dataSourceName + "-sentinel-nacos-datasource");
|
||||
});
|
||||
Optional.ofNullable(dataSourceProperties.getApollo())
|
||||
.ifPresent(apollo -> {
|
||||
registerBean(beanFactory, apollo,
|
||||
dataSourceName + "-sentinel-apollo-datasource");
|
||||
});
|
||||
Optional.ofNullable(dataSourceProperties.getZk()).ifPresent(zk -> {
|
||||
registerBean(beanFactory, zk,
|
||||
dataSourceName + "-sentinel-zk-datasource");
|
||||
});
|
||||
|
||||
AbstractDataSourceProperties abstractDataSourceProperties = dataSourceProperties
|
||||
.getValidDataSourceProperties();
|
||||
abstractDataSourceProperties.preCheck();
|
||||
registerBean(beanFactory, abstractDataSourceProperties, dataSourceName
|
||||
+ "-sentinel-" + validFields.get(0) + "-datasource");
|
||||
});
|
||||
|
||||
dataSourceBeanNameList.forEach(beanName -> {
|
||||
@ -251,7 +219,8 @@ public class SentinelDataSourceHandler {
|
||||
}
|
||||
else {
|
||||
// wired properties
|
||||
builder.addPropertyValue(propertyName, propertyValue);
|
||||
Optional.ofNullable(propertyValue)
|
||||
.ifPresent(v -> builder.addPropertyValue(propertyName, v));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.cloud.alicloud.context;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public interface Constants {
|
||||
|
||||
interface Sentinel {
|
||||
String PROPERTY_PREFIX = "spring.cloud.sentinel";
|
||||
String NACOS_DATASOURCE_AK = PROPERTY_PREFIX + ".nacos.config.access-key";
|
||||
String NACOS_DATASOURCE_SK = PROPERTY_PREFIX + ".nacos.config.secret-key";
|
||||
String NACOS_DATASOURCE_NAMESPACE = PROPERTY_PREFIX + ".nacos.config.namespace";
|
||||
String NACOS_DATASOURCE_ENDPOINT = PROPERTY_PREFIX + ".nacos.config.endpoint";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.cloud.alicloud.context.sentinel;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||
import org.springframework.cloud.alicloud.context.Constants;
|
||||
import org.springframework.cloud.alicloud.context.listener.AbstractOnceApplicationListener;
|
||||
|
||||
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
|
||||
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class SentinelAliCloudListener
|
||||
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(SentinelAliCloudListener.class);
|
||||
|
||||
@Override
|
||||
protected void handleEvent(ApplicationEnvironmentPreparedEvent event) {
|
||||
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
|
||||
.getEdasChangeOrderConfiguration();
|
||||
logger.info("Sentinel Nacos datasource will"
|
||||
+ (edasChangeOrderConfiguration.isEdasManaged() ? " be " : " not be ")
|
||||
+ "changed by edas change order.");
|
||||
if (!edasChangeOrderConfiguration.isEdasManaged()) {
|
||||
return;
|
||||
}
|
||||
System.getProperties().setProperty(Constants.Sentinel.NACOS_DATASOURCE_ENDPOINT,
|
||||
edasChangeOrderConfiguration.getAddressServerDomain());
|
||||
System.getProperties().setProperty(Constants.Sentinel.NACOS_DATASOURCE_NAMESPACE,
|
||||
edasChangeOrderConfiguration.getTenantId());
|
||||
System.getProperties().setProperty(Constants.Sentinel.NACOS_DATASOURCE_AK,
|
||||
edasChangeOrderConfiguration.getDauthAccessKey());
|
||||
System.getProperties().setProperty(Constants.Sentinel.NACOS_DATASOURCE_SK,
|
||||
edasChangeOrderConfiguration.getDauthSecretKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String conditionalOnClass() {
|
||||
return "com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource";
|
||||
}
|
||||
|
||||
}
|
@ -9,4 +9,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.alicloud.context.statistics.StatisticsTaskStarter
|
||||
org.springframework.context.ApplicationListener=\
|
||||
org.springframework.cloud.alicloud.context.ans.AnsContextApplicationListener,\
|
||||
org.springframework.cloud.alicloud.context.nacos.NacosParameterInitListener
|
||||
org.springframework.cloud.alicloud.context.nacos.NacosParameterInitListener,\
|
||||
org.springframework.cloud.alicloud.context.sentinel.SentinelAliCloudListener
|
Loading…
x
Reference in New Issue
Block a user