mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #1364 from mercyblitz/finchley
[Feature] Synchronizing code to Spring Cloud Alibaba finchley branch
This commit is contained in:
commit
420f4698d8
@ -89,6 +89,12 @@
|
|||||||
<version>${sentinel.version}</version>
|
<version>${sentinel.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-datasource-consul</artifactId>
|
||||||
|
<version>${sentinel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.csp</groupId>
|
<groupId>com.alibaba.csp</groupId>
|
||||||
<artifactId>sentinel-web-servlet</artifactId>
|
<artifactId>sentinel-web-servlet</artifactId>
|
||||||
|
@ -83,6 +83,12 @@
|
|||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-datasource-consul</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-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
|
||||||
|
*
|
||||||
|
* https://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 com.alibaba.cloud.sentinel.datasource.config;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.sentinel.datasource.factorybean.ConsulDataSourceFactoryBean;
|
||||||
|
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Consul Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
||||||
|
* {@link ConsulDataSourceFactoryBean}.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:mengjindc@gmail.com">mengjin</a>
|
||||||
|
*/
|
||||||
|
public class ConsulDataSourceProperties extends AbstractDataSourceProperties {
|
||||||
|
|
||||||
|
public ConsulDataSourceProperties() {
|
||||||
|
super(ConsulDataSourceFactoryBean.class.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* consul server host.
|
||||||
|
*/
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* consul server port.
|
||||||
|
*/
|
||||||
|
private int port = 8500;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* data key in Redis.
|
||||||
|
*/
|
||||||
|
private String ruleKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request of query will hang until timeout (in second) or get updated value.
|
||||||
|
*/
|
||||||
|
private int waitTimeoutInSecond = 1;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void preCheck(String dataSourceName) {
|
||||||
|
if (StringUtils.isEmpty(host)) {
|
||||||
|
throw new IllegalArgumentException("ConsulDataSource server-host is empty");
|
||||||
|
}
|
||||||
|
if (StringUtils.isEmpty(ruleKey)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"ConsulDataSource ruleKey can not be empty");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost() {
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHost(String host) {
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRuleKey() {
|
||||||
|
return ruleKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRuleKey(String ruleKey) {
|
||||||
|
this.ruleKey = ruleKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWaitTimeoutInSecond() {
|
||||||
|
return waitTimeoutInSecond;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWaitTimeoutInSecond(int waitTimeoutInSecond) {
|
||||||
|
this.waitTimeoutInSecond = waitTimeoutInSecond;
|
||||||
|
}
|
||||||
|
}
|
@ -34,6 +34,7 @@ import org.springframework.util.ObjectUtils;
|
|||||||
* @see ZookeeperDataSourceProperties
|
* @see ZookeeperDataSourceProperties
|
||||||
* @see FileDataSourceProperties
|
* @see FileDataSourceProperties
|
||||||
* @see RedisDataSourceProperties
|
* @see RedisDataSourceProperties
|
||||||
|
* @see ConsulDataSourceProperties
|
||||||
*/
|
*/
|
||||||
public class DataSourcePropertiesConfiguration {
|
public class DataSourcePropertiesConfiguration {
|
||||||
|
|
||||||
@ -47,9 +48,23 @@ public class DataSourcePropertiesConfiguration {
|
|||||||
|
|
||||||
private RedisDataSourceProperties redis;
|
private RedisDataSourceProperties redis;
|
||||||
|
|
||||||
|
private ConsulDataSourceProperties consul;
|
||||||
|
|
||||||
public DataSourcePropertiesConfiguration() {
|
public DataSourcePropertiesConfiguration() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DataSourcePropertiesConfiguration(ConsulDataSourceProperties consul) {
|
||||||
|
this.consul = consul;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConsulDataSourceProperties getConsul() {
|
||||||
|
return consul;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConsul(ConsulDataSourceProperties consul) {
|
||||||
|
this.consul = consul;
|
||||||
|
}
|
||||||
|
|
||||||
public DataSourcePropertiesConfiguration(FileDataSourceProperties file) {
|
public DataSourcePropertiesConfiguration(FileDataSourceProperties file) {
|
||||||
this.file = file;
|
this.file = file;
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ import com.alibaba.cloud.sentinel.datasource.factorybean.RedisDataSourceFactoryB
|
|||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zookeeper Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
* Redis Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
||||||
* {@link RedisDataSourceFactoryBean}.
|
* {@link RedisDataSourceFactoryBean}.
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:wangiegie@gmail.com">lengleng</a>
|
* @author <a href="mailto:wangiegie@gmail.com">lengleng</a>
|
||||||
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-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
|
||||||
|
*
|
||||||
|
* https://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 com.alibaba.cloud.sentinel.datasource.factorybean;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
|
import com.alibaba.csp.sentinel.datasource.consul.ConsulDataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link FactoryBean} for creating {@link ConsulDataSource} instance.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:mengjindc@gmail.com">mengjin</a>
|
||||||
|
* @see ConsulDataSource
|
||||||
|
*/
|
||||||
|
public class ConsulDataSourceFactoryBean implements FactoryBean<ConsulDataSource> {
|
||||||
|
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
private String ruleKey;
|
||||||
|
|
||||||
|
private int waitTimeoutInSecond;
|
||||||
|
|
||||||
|
private Converter converter;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConsulDataSource getObject() throws Exception {
|
||||||
|
return new ConsulDataSource(host, port, ruleKey, waitTimeoutInSecond, converter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getObjectType() {
|
||||||
|
return ConsulDataSource.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost() {
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHost(String host) {
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRuleKey() {
|
||||||
|
return ruleKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRuleKey(String ruleKey) {
|
||||||
|
this.ruleKey = ruleKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWaitTimeoutInSecond() {
|
||||||
|
return waitTimeoutInSecond;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWaitTimeoutInSecond(int waitTimeoutInSecond) {
|
||||||
|
this.waitTimeoutInSecond = waitTimeoutInSecond;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Converter getConverter() {
|
||||||
|
return converter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConverter(Converter converter) {
|
||||||
|
this.converter = converter;
|
||||||
|
}
|
||||||
|
}
|
@ -3,3 +3,4 @@ file =com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource
|
|||||||
apollo = com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource
|
apollo = com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource
|
||||||
zk = com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource
|
zk = com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource
|
||||||
redis = com.alibaba.csp.sentinel.datasource.redis.RedisDataSource
|
redis = com.alibaba.csp.sentinel.datasource.redis.RedisDataSource
|
||||||
|
consul = com.alibaba.csp.sentinel.datasource.consul.ConsulDataSource
|
||||||
|
@ -16,23 +16,104 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.seata.feign.hystrix;
|
package com.alibaba.cloud.seata.feign.hystrix;
|
||||||
|
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import com.netflix.hystrix.HystrixThreadPoolKey;
|
||||||
|
import com.netflix.hystrix.HystrixThreadPoolProperties;
|
||||||
import com.netflix.hystrix.strategy.HystrixPlugins;
|
import com.netflix.hystrix.strategy.HystrixPlugins;
|
||||||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
|
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
|
||||||
|
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariable;
|
||||||
|
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle;
|
||||||
|
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
|
||||||
|
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
|
||||||
|
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
|
||||||
|
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
|
||||||
|
import com.netflix.hystrix.strategy.properties.HystrixProperty;
|
||||||
import io.seata.core.context.RootContext;
|
import io.seata.core.context.RootContext;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.web.context.request.RequestAttributes;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
*/
|
*/
|
||||||
public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
|
public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory
|
||||||
|
.getLogger(SeataHystrixConcurrencyStrategy.class);
|
||||||
private HystrixConcurrencyStrategy delegate;
|
private HystrixConcurrencyStrategy delegate;
|
||||||
|
|
||||||
public SeataHystrixConcurrencyStrategy() {
|
public SeataHystrixConcurrencyStrategy() {
|
||||||
|
try {
|
||||||
this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy();
|
this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy();
|
||||||
|
if (this.delegate instanceof SeataHystrixConcurrencyStrategy) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins
|
||||||
|
.getInstance().getCommandExecutionHook();
|
||||||
|
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance()
|
||||||
|
.getEventNotifier();
|
||||||
|
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance()
|
||||||
|
.getMetricsPublisher();
|
||||||
|
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance()
|
||||||
|
.getPropertiesStrategy();
|
||||||
|
logCurrentStateOfHystrixPlugins(eventNotifier, metricsPublisher,
|
||||||
|
propertiesStrategy);
|
||||||
HystrixPlugins.reset();
|
HystrixPlugins.reset();
|
||||||
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
|
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
|
||||||
|
HystrixPlugins.getInstance()
|
||||||
|
.registerCommandExecutionHook(commandExecutionHook);
|
||||||
|
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
|
||||||
|
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
|
||||||
|
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
logger.error("Failed to register Seata Hystrix Concurrency Strategy", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logCurrentStateOfHystrixPlugins(HystrixEventNotifier eventNotifier,
|
||||||
|
HystrixMetricsPublisher metricsPublisher,
|
||||||
|
HystrixPropertiesStrategy propertiesStrategy) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Current Hystrix plugins configuration is ["
|
||||||
|
+ "concurrencyStrategy [" + this.delegate + "]," + "eventNotifier ["
|
||||||
|
+ eventNotifier + "]," + "metricPublisher [" + metricsPublisher + "],"
|
||||||
|
+ "propertiesStrategy [" + propertiesStrategy + "]," + "]");
|
||||||
|
logger.debug("Registering Seata Hystrix Concurrency Strategy.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
|
||||||
|
HystrixProperty<Integer> corePoolSize,
|
||||||
|
HystrixProperty<Integer> maximumPoolSize,
|
||||||
|
HystrixProperty<Integer> keepAliveTime, TimeUnit unit,
|
||||||
|
BlockingQueue<Runnable> workQueue) {
|
||||||
|
return this.delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize,
|
||||||
|
keepAliveTime, unit, workQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
|
||||||
|
HystrixThreadPoolProperties threadPoolProperties) {
|
||||||
|
return this.delegate.getThreadPool(threadPoolKey, threadPoolProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) {
|
||||||
|
return this.delegate.getBlockingQueue(maxQueueSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> HystrixRequestVariable<T> getRequestVariable(
|
||||||
|
HystrixRequestVariableLifecycle<T> rv) {
|
||||||
|
return this.delegate.getRequestVariable(rv);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -52,7 +133,8 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
|||||||
return wrappedCallable;
|
return wrappedCallable;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SeataContextCallable<>(wrappedCallable);
|
return new SeataContextCallable<>(wrappedCallable,
|
||||||
|
RequestContextHolder.getRequestAttributes());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SeataContextCallable<K> implements Callable<K> {
|
private static class SeataContextCallable<K> implements Callable<K> {
|
||||||
@ -61,19 +143,24 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
|||||||
|
|
||||||
private final String xid;
|
private final String xid;
|
||||||
|
|
||||||
SeataContextCallable(Callable<K> actual) {
|
private final RequestAttributes requestAttributes;
|
||||||
|
|
||||||
|
SeataContextCallable(Callable<K> actual, RequestAttributes requestAttribute) {
|
||||||
this.actual = actual;
|
this.actual = actual;
|
||||||
|
this.requestAttributes = requestAttribute;
|
||||||
this.xid = RootContext.getXID();
|
this.xid = RootContext.getXID();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public K call() throws Exception {
|
public K call() throws Exception {
|
||||||
try {
|
try {
|
||||||
|
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||||
RootContext.bind(xid);
|
RootContext.bind(xid);
|
||||||
return actual.call();
|
return actual.call();
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
RootContext.unbind();
|
RootContext.unbind();
|
||||||
|
RequestContextHolder.resetRequestAttributes();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,9 +403,11 @@ public class DubboServiceMetadataRepository
|
|||||||
return emptyList();
|
return emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
return hasText(protocol) ? urls.stream()
|
return hasText(protocol)
|
||||||
|
? urls.stream()
|
||||||
.filter(url -> url.getProtocol().equalsIgnoreCase(protocol))
|
.filter(url -> url.getProtocol().equalsIgnoreCase(protocol))
|
||||||
.collect(Collectors.toList()) : unmodifiableList(urls);
|
.collect(Collectors.toList())
|
||||||
|
: unmodifiableList(urls);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user