mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish alibaba/spring-cloud-alibaba/#1283 : Renaming spring-cloud-starter-alibaba to be spring-cloud-alibaba-starters
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
public class SeataBeanPostProcessor implements BeanPostProcessor {
|
||||
|
||||
private final SeataFeignObjectWrapper seataFeignObjectWrapper;
|
||||
|
||||
SeataBeanPostProcessor(SeataFeignObjectWrapper seataFeignObjectWrapper) {
|
||||
this.seataFeignObjectWrapper = seataFeignObjectWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return this.seataFeignObjectWrapper.wrap(bean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.cloud.openfeign.FeignContext;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
public class SeataContextBeanPostProcessor implements BeanPostProcessor {
|
||||
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
private SeataFeignObjectWrapper seataFeignObjectWrapper;
|
||||
|
||||
SeataContextBeanPostProcessor(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
if (bean instanceof FeignContext && !(bean instanceof SeataFeignContext)) {
|
||||
return new SeataFeignContext(getSeataFeignObjectWrapper(),
|
||||
(FeignContext) bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
private SeataFeignObjectWrapper getSeataFeignObjectWrapper() {
|
||||
if (this.seataFeignObjectWrapper == null) {
|
||||
this.seataFeignObjectWrapper = this.beanFactory
|
||||
.getBean(SeataFeignObjectWrapper.class);
|
||||
}
|
||||
return this.seataFeignObjectWrapper;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Request;
|
||||
import feign.Response;
|
||||
|
||||
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
|
||||
|
||||
/**
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
public class SeataFeignBlockingLoadBalancerClient
|
||||
extends FeignBlockingLoadBalancerClient {
|
||||
|
||||
public SeataFeignBlockingLoadBalancerClient(Client delegate,
|
||||
BlockingLoadBalancerClient loadBalancerClient,
|
||||
SeataFeignObjectWrapper seataFeignObjectWrapper) {
|
||||
super((Client) seataFeignObjectWrapper.wrap(delegate), loadBalancerClient);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response execute(Request request, Request.Options options) throws IOException {
|
||||
return super.execute(request, options);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import feign.Feign;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
final class SeataFeignBuilder {
|
||||
|
||||
private SeataFeignBuilder() {
|
||||
}
|
||||
|
||||
static Feign.Builder builder(BeanFactory beanFactory) {
|
||||
return Feign.builder().client(new SeataFeignClient(beanFactory));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Request;
|
||||
import feign.Response;
|
||||
import io.seata.core.context.RootContext;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
public class SeataFeignClient implements Client {
|
||||
|
||||
private final Client delegate;
|
||||
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
private static final int MAP_SIZE = 16;
|
||||
|
||||
SeataFeignClient(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
this.delegate = new Client.Default(null, null);
|
||||
}
|
||||
|
||||
SeataFeignClient(BeanFactory beanFactory, Client delegate) {
|
||||
this.delegate = delegate;
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response execute(Request request, Request.Options options) throws IOException {
|
||||
|
||||
Request modifiedRequest = getModifyRequest(request);
|
||||
return this.delegate.execute(modifiedRequest, options);
|
||||
}
|
||||
|
||||
private Request getModifyRequest(Request request) {
|
||||
|
||||
String xid = RootContext.getXID();
|
||||
|
||||
if (StringUtils.isEmpty(xid)) {
|
||||
return request;
|
||||
}
|
||||
|
||||
Map<String, Collection<String>> headers = new HashMap<>(MAP_SIZE);
|
||||
headers.putAll(request.headers());
|
||||
|
||||
List<String> seataXid = new ArrayList<>();
|
||||
seataXid.add(xid);
|
||||
headers.put(RootContext.KEY_XID, seataXid);
|
||||
|
||||
return Request.create(request.method(), request.url(), headers, request.body(),
|
||||
request.charset());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Feign;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(Client.class)
|
||||
@AutoConfigureBefore(FeignAutoConfiguration.class)
|
||||
public class SeataFeignClientAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@Scope("prototype")
|
||||
@ConditionalOnClass(name = "com.netflix.hystrix.HystrixCommand")
|
||||
@ConditionalOnProperty(name = "feign.hystrix.enabled", havingValue = "true")
|
||||
Feign.Builder feignHystrixBuilder(BeanFactory beanFactory) {
|
||||
return SeataHystrixFeignBuilder.builder(beanFactory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope("prototype")
|
||||
@ConditionalOnClass(name = "com.alibaba.csp.sentinel.SphU")
|
||||
@ConditionalOnProperty(name = "feign.sentinel.enabled", havingValue = "true")
|
||||
Feign.Builder feignSentinelBuilder(BeanFactory beanFactory) {
|
||||
return SeataSentinelFeignBuilder.builder(beanFactory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@Scope("prototype")
|
||||
Feign.Builder feignBuilder(BeanFactory beanFactory) {
|
||||
return SeataFeignBuilder.builder(beanFactory);
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
protected static class FeignBeanPostProcessorConfiguration {
|
||||
|
||||
@Bean
|
||||
SeataBeanPostProcessor seataBeanPostProcessor(
|
||||
SeataFeignObjectWrapper seataFeignObjectWrapper) {
|
||||
return new SeataBeanPostProcessor(seataFeignObjectWrapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
SeataContextBeanPostProcessor seataContextBeanPostProcessor(
|
||||
BeanFactory beanFactory) {
|
||||
return new SeataContextBeanPostProcessor(beanFactory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
SeataFeignObjectWrapper seataFeignObjectWrapper(BeanFactory beanFactory) {
|
||||
return new SeataFeignObjectWrapper(beanFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import feign.Client;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignContext;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
public class SeataFeignContext extends FeignContext {
|
||||
|
||||
private final SeataFeignObjectWrapper seataFeignObjectWrapper;
|
||||
|
||||
private final FeignContext delegate;
|
||||
|
||||
SeataFeignContext(SeataFeignObjectWrapper seataFeignObjectWrapper,
|
||||
FeignContext delegate) {
|
||||
this.seataFeignObjectWrapper = seataFeignObjectWrapper;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getInstance(String name, Class<T> type) {
|
||||
T object = this.delegate.getInstance(name, type);
|
||||
if (object instanceof Client) {
|
||||
return object;
|
||||
}
|
||||
return (T) this.seataFeignObjectWrapper.wrap(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Map<String, T> getInstances(String name, Class<T> type) {
|
||||
Map<String, T> instances = this.delegate.getInstances(name, type);
|
||||
if (instances == null) {
|
||||
return null;
|
||||
}
|
||||
Map<String, T> convertedInstances = new HashMap<>();
|
||||
for (Map.Entry<String, T> entry : instances.entrySet()) {
|
||||
if (entry.getValue() instanceof Client) {
|
||||
convertedInstances.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
else {
|
||||
convertedInstances.put(entry.getKey(),
|
||||
(T) this.seataFeignObjectWrapper.wrap(entry.getValue()));
|
||||
}
|
||||
}
|
||||
return convertedInstances;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import feign.Client;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
|
||||
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
|
||||
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
public class SeataFeignObjectWrapper {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(SeataFeignObjectWrapper.class);
|
||||
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
private CachingSpringLoadBalancerFactory cachingSpringLoadBalancerFactory;
|
||||
|
||||
private SpringClientFactory springClientFactory;
|
||||
|
||||
SeataFeignObjectWrapper(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
Object wrap(Object bean) {
|
||||
if (bean instanceof Client && !(bean instanceof SeataFeignClient)) {
|
||||
if (bean instanceof LoadBalancerFeignClient) {
|
||||
LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean);
|
||||
return new SeataLoadBalancerFeignClient(client.getDelegate(), factory(),
|
||||
clientFactory(), this);
|
||||
}
|
||||
if (bean instanceof FeignBlockingLoadBalancerClient) {
|
||||
FeignBlockingLoadBalancerClient client = (FeignBlockingLoadBalancerClient) bean;
|
||||
return new SeataFeignBlockingLoadBalancerClient(client.getDelegate(),
|
||||
beanFactory.getBean(BlockingLoadBalancerClient.class), this);
|
||||
}
|
||||
return new SeataFeignClient(this.beanFactory, (Client) bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
CachingSpringLoadBalancerFactory factory() {
|
||||
if (this.cachingSpringLoadBalancerFactory == null) {
|
||||
this.cachingSpringLoadBalancerFactory = this.beanFactory
|
||||
.getBean(CachingSpringLoadBalancerFactory.class);
|
||||
}
|
||||
return this.cachingSpringLoadBalancerFactory;
|
||||
}
|
||||
|
||||
SpringClientFactory clientFactory() {
|
||||
if (this.springClientFactory == null) {
|
||||
this.springClientFactory = this.beanFactory
|
||||
.getBean(SpringClientFactory.class);
|
||||
}
|
||||
return this.springClientFactory;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import feign.Feign;
|
||||
import feign.Retryer;
|
||||
import feign.hystrix.HystrixFeign;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
final class SeataHystrixFeignBuilder {
|
||||
|
||||
private SeataHystrixFeignBuilder() {
|
||||
}
|
||||
|
||||
static Feign.Builder builder(BeanFactory beanFactory) {
|
||||
return HystrixFeign.builder().retryer(Retryer.NEVER_RETRY)
|
||||
.client(new SeataFeignClient(beanFactory));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Request;
|
||||
import feign.Response;
|
||||
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
|
||||
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
public class SeataLoadBalancerFeignClient extends LoadBalancerFeignClient {
|
||||
|
||||
SeataLoadBalancerFeignClient(Client delegate,
|
||||
CachingSpringLoadBalancerFactory lbClientFactory,
|
||||
SpringClientFactory clientFactory,
|
||||
SeataFeignObjectWrapper seataFeignObjectWrapper) {
|
||||
super((Client) seataFeignObjectWrapper.wrap(delegate), lbClientFactory,
|
||||
clientFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response execute(Request request, Request.Options options) throws IOException {
|
||||
return super.execute(request, options);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.seata.feign;
|
||||
|
||||
import com.alibaba.cloud.sentinel.feign.SentinelFeign;
|
||||
import feign.Feign;
|
||||
import feign.Retryer;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
final class SeataSentinelFeignBuilder {
|
||||
|
||||
private SeataSentinelFeignBuilder() {
|
||||
}
|
||||
|
||||
static Feign.Builder builder(BeanFactory beanFactory) {
|
||||
return SentinelFeign.builder().retryer(Retryer.NEVER_RETRY)
|
||||
.client(new SeataFeignClient(beanFactory));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.seata.feign.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(HystrixCommand.class)
|
||||
public class SeataHystrixAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
SeataHystrixConcurrencyStrategy seataHystrixConcurrencyStrategy() {
|
||||
return new SeataHystrixConcurrencyStrategy();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.seata.feign.hystrix;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import com.netflix.hystrix.strategy.HystrixPlugins;
|
||||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
|
||||
import io.seata.core.context.RootContext;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
|
||||
|
||||
private HystrixConcurrencyStrategy delegate;
|
||||
|
||||
public SeataHystrixConcurrencyStrategy() {
|
||||
this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy();
|
||||
HystrixPlugins.reset();
|
||||
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K> Callable<K> wrapCallable(Callable<K> c) {
|
||||
if (c instanceof SeataContextCallable) {
|
||||
return c;
|
||||
}
|
||||
|
||||
Callable<K> wrappedCallable;
|
||||
if (this.delegate != null) {
|
||||
wrappedCallable = this.delegate.wrapCallable(c);
|
||||
}
|
||||
else {
|
||||
wrappedCallable = c;
|
||||
}
|
||||
if (wrappedCallable instanceof SeataContextCallable) {
|
||||
return wrappedCallable;
|
||||
}
|
||||
|
||||
return new SeataContextCallable<>(wrappedCallable);
|
||||
}
|
||||
|
||||
private static class SeataContextCallable<K> implements Callable<K> {
|
||||
|
||||
private final Callable<K> actual;
|
||||
|
||||
private final String xid;
|
||||
|
||||
SeataContextCallable(Callable<K> actual) {
|
||||
this.actual = actual;
|
||||
this.xid = RootContext.getXID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public K call() throws Exception {
|
||||
try {
|
||||
RootContext.bind(xid);
|
||||
return actual.call();
|
||||
}
|
||||
finally {
|
||||
RootContext.unbind();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.seata.rest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class SeataRestTemplateAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public SeataRestTemplateInterceptor seataRestTemplateInterceptor() {
|
||||
return new SeataRestTemplateInterceptor();
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
private Collection<RestTemplate> restTemplates;
|
||||
|
||||
@Autowired
|
||||
private SeataRestTemplateInterceptor seataRestTemplateInterceptor;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
if (this.restTemplates != null) {
|
||||
for (RestTemplate restTemplate : restTemplates) {
|
||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
|
||||
restTemplate.getInterceptors());
|
||||
interceptors.add(this.seataRestTemplateInterceptor);
|
||||
restTemplate.setInterceptors(interceptors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.seata.rest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import io.seata.core.context.RootContext;
|
||||
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.client.support.HttpRequestWrapper;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
public class SeataRestTemplateInterceptor implements ClientHttpRequestInterceptor {
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes,
|
||||
ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
|
||||
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(httpRequest);
|
||||
|
||||
String xid = RootContext.getXID();
|
||||
|
||||
if (!StringUtils.isEmpty(xid)) {
|
||||
requestWrapper.getHeaders().add(RootContext.KEY_XID, xid);
|
||||
}
|
||||
return clientHttpRequestExecution.execute(requestWrapper, bytes);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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.seata.web;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import io.seata.core.context.RootContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*
|
||||
* Seata HandlerInterceptor, Convert Seata information into
|
||||
* @see io.seata.core.context.RootContext from http request's header in
|
||||
* {@link org.springframework.web.servlet.HandlerInterceptor#preHandle(HttpServletRequest , HttpServletResponse , Object )},
|
||||
* And clean up Seata information after servlet method invocation in
|
||||
* {@link org.springframework.web.servlet.HandlerInterceptor#afterCompletion(HttpServletRequest, HttpServletResponse, Object, Exception)}
|
||||
*/
|
||||
public class SeataHandlerInterceptor implements HandlerInterceptor {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(SeataHandlerInterceptor.class);
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler) {
|
||||
|
||||
String xid = RootContext.getXID();
|
||||
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid);
|
||||
}
|
||||
|
||||
if (xid == null && rpcXid != null) {
|
||||
RootContext.bind(rpcXid);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("bind {} to RootContext", rpcXid);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler, Exception e) {
|
||||
|
||||
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
||||
|
||||
if (StringUtils.isEmpty(rpcXid)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String unbindXid = RootContext.unbind();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("unbind {} from RootContext", unbindXid);
|
||||
}
|
||||
if (!rpcXid.equalsIgnoreCase(unbindXid)) {
|
||||
log.warn("xid in change during RPC from {} to {}", rpcXid, unbindXid);
|
||||
if (unbindXid != null) {
|
||||
RootContext.bind(unbindXid);
|
||||
log.warn("bind {} back to RootContext", unbindXid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.seata.web;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@ConditionalOnWebApplication
|
||||
public class SeataHandlerInterceptorConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new SeataHandlerInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.alibaba.cloud.seata.rest.SeataRestTemplateAutoConfiguration,\
|
||||
com.alibaba.cloud.seata.web.SeataHandlerInterceptorConfiguration,\
|
||||
com.alibaba.cloud.seata.feign.SeataFeignClientAutoConfiguration,\
|
||||
com.alibaba.cloud.seata.feign.hystrix.SeataHystrixAutoConfiguration
|
||||
|
Reference in New Issue
Block a user