Merge pull request #201 from fangjian0423/master
refactor sentinel datasource and support commercializedpull/224/head
commit
4b055b93c1
@ -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;
|
||||
}
|
||||
}
|
@ -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";
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue