mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
upgrade sentinel version to 1.4.1 and remove NacosDataSourceWithAuthorization
This commit is contained in:
parent
ada963c330
commit
b39fb80a53
@ -17,7 +17,7 @@
|
||||
<description>Spring Cloud Alibaba Dependencies</description>
|
||||
|
||||
<properties>
|
||||
<sentinel.version>1.4.0</sentinel.version>
|
||||
<sentinel.version>1.4.1</sentinel.version>
|
||||
<oss.version>3.1.0</oss.version>
|
||||
<nacos.client.version>0.6.2</nacos.client.version>
|
||||
<nacos.config.version>0.6.1</nacos.config.version>
|
||||
|
@ -1,135 +0,0 @@
|
||||
/*
|
||||
* 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();
|
||||
}
|
||||
}
|
@ -5,7 +5,6 @@ import javax.validation.constraints.NotEmpty;
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -40,8 +39,6 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
||||
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()
|
||||
@ -119,8 +116,6 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
||||
|
||||
public static NacosDataSourceProperties buildByEDAS(String type) {
|
||||
NacosDataSourceProperties result = new NacosDataSourceProperties();
|
||||
result.setFactoryBeanName(
|
||||
NacosDataSourceWithAuthorizationFactoryBean.class.getName());
|
||||
result.setEndpoint(System.getProperties()
|
||||
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT));
|
||||
result.setNamespace(System.getProperties()
|
||||
|
@ -1,9 +1,14 @@
|
||||
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.SentinelDataSourceConstants;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import com.alibaba.nacos.api.PropertyKeyConst;
|
||||
|
||||
/**
|
||||
* A {@link FactoryBean} for creating {@link NacosDataSource} instance.
|
||||
@ -13,50 +18,96 @@ import org.springframework.beans.factory.FactoryBean;
|
||||
*/
|
||||
public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource> {
|
||||
|
||||
private String serverAddr;
|
||||
private String groupId;
|
||||
private String dataId;
|
||||
private Converter converter;
|
||||
private String serverAddr;
|
||||
private String groupId;
|
||||
private String dataId;
|
||||
private Converter converter;
|
||||
|
||||
@Override
|
||||
public NacosDataSource getObject() throws Exception {
|
||||
return new NacosDataSource(serverAddr, groupId, dataId, converter);
|
||||
}
|
||||
private String endpoint;
|
||||
private String namespace;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return NacosDataSource.class;
|
||||
}
|
||||
@Override
|
||||
public NacosDataSource getObject() throws Exception {
|
||||
if (!StringUtils.isEmpty(System.getProperties()
|
||||
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT))) {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty(PropertyKeyConst.ACCESS_KEY, this.accessKey);
|
||||
properties.setProperty(PropertyKeyConst.SERVER_ADDR, this.secretKey);
|
||||
properties.setProperty(PropertyKeyConst.ENDPOINT, this.endpoint);
|
||||
properties.setProperty(PropertyKeyConst.NAMESPACE, this.namespace);
|
||||
return new NacosDataSource(properties, groupId, dataId, converter);
|
||||
}
|
||||
return new NacosDataSource(serverAddr, groupId, dataId, converter);
|
||||
}
|
||||
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
}
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return NacosDataSource.class;
|
||||
}
|
||||
|
||||
public void setServerAddr(String serverAddr) {
|
||||
this.serverAddr = serverAddr;
|
||||
}
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
}
|
||||
|
||||
public String getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
public void setServerAddr(String serverAddr) {
|
||||
this.serverAddr = serverAddr;
|
||||
}
|
||||
|
||||
public void setGroupId(String groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
public String getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
public void setGroupId(String groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
public void setDataId(String dataId) {
|
||||
this.dataId = dataId;
|
||||
}
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
public Converter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
public void setDataId(String dataId) {
|
||||
this.dataId = dataId;
|
||||
}
|
||||
|
||||
public void setConverter(Converter converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
public Converter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
public void setConverter(Converter converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,101 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user