mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
commit
6095930326
20
pom.xml
20
pom.xml
@ -8,7 +8,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-build</artifactId>
|
||||
<version>2.2.0.RELEASE</version>
|
||||
<version>2.2.2.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
@ -73,15 +73,15 @@
|
||||
|
||||
<properties>
|
||||
<!-- Dependency Versions -->
|
||||
<spring-cloud-commons.version>2.2.0.RELEASE</spring-cloud-commons.version>
|
||||
<spring-cloud-netflix.version>2.2.0.RELEASE</spring-cloud-netflix.version>
|
||||
<spring-cloud-openfeign.version>2.2.0.RELEASE</spring-cloud-openfeign.version>
|
||||
<spring-cloud-bus.version>2.2.0.RELEASE</spring-cloud-bus.version>
|
||||
<spring-cloud-gateway.version>2.2.0.RELEASE</spring-cloud-gateway.version>
|
||||
<spring-cloud-stream.version>Horsham.RELEASE</spring-cloud-stream.version>
|
||||
<spring-cloud-consul.version>2.2.0.RELEASE</spring-cloud-consul.version>
|
||||
<spring-cloud-config.version>2.2.0.RELEASE</spring-cloud-config.version>
|
||||
<spring-cloud-zookeeper.version>2.2.0.RELEASE</spring-cloud-zookeeper.version>
|
||||
<spring-cloud-commons.version>2.2.2.RELEASE</spring-cloud-commons.version>
|
||||
<spring-cloud-netflix.version>2.2.2.RELEASE</spring-cloud-netflix.version>
|
||||
<spring-cloud-openfeign.version>2.2.2.RELEASE</spring-cloud-openfeign.version>
|
||||
<spring-cloud-bus.version>2.2.1.RELEASE</spring-cloud-bus.version>
|
||||
<spring-cloud-gateway.version>2.2.2.RELEASE</spring-cloud-gateway.version>
|
||||
<spring-cloud-stream.version>Horsham.SR3</spring-cloud-stream.version>
|
||||
<spring-cloud-consul.version>2.2.2.RELEASE</spring-cloud-consul.version>
|
||||
<spring-cloud-config.version>2.2.2.RELEASE</spring-cloud-config.version>
|
||||
<spring-cloud-zookeeper.version>2.2.1.RELEASE</spring-cloud-zookeeper.version>
|
||||
|
||||
<junit.version>4.12</junit.version>
|
||||
<javax-servlet-api>3.0</javax-servlet-api>
|
||||
|
@ -20,7 +20,7 @@
|
||||
<properties>
|
||||
<sentinel.version>1.7.1</sentinel.version>
|
||||
<oss.version>3.1.0</oss.version>
|
||||
<seata.version>1.0.0</seata.version>
|
||||
<seata.version>1.1.0</seata.version>
|
||||
<nacos.client.version>1.2.0</nacos.client.version>
|
||||
<nacos.config.version>0.8.0</nacos.config.version>
|
||||
<acm.version>1.0.9</acm.version>
|
||||
|
@ -5,7 +5,7 @@ spring.cloud.nacos.discovery.server-addr=localhost:8848
|
||||
spring.datasource.name="accountDataSource"
|
||||
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://xxx:3306/fescar?useSSL=false&serverTimezone=UTC
|
||||
spring.datasource.url=jdbc:mysql://xxx:3306/seata?useSSL=false&serverTimezone=UTC
|
||||
spring.datasource.username=xxx
|
||||
spring.datasource.password=xxx
|
||||
spring.datasource.druid.max-active=20
|
||||
@ -13,8 +13,9 @@ spring.datasource.druid.min-idle=2
|
||||
spring.datasource.druid.initial-size=2
|
||||
|
||||
seata.enabled=true
|
||||
seata.service.vgroup-mapping=default
|
||||
seata.service.grouplist=127.0.0.1:8091
|
||||
spring.cloud.alibaba.seata.tx-service-group=account-service
|
||||
seata.service.vgroup-mapping.account-service=default
|
||||
seata.service.grouplist.default=127.0.0.1:8091
|
||||
seata.service.disable-global-transaction=false
|
||||
|
||||
## if use registry center
|
||||
|
@ -9,8 +9,9 @@ spring.cloud.nacos.discovery.server-addr=localhost:8848
|
||||
logging.level.io.seata=debug
|
||||
|
||||
seata.enabled=true
|
||||
seata.service.vgroup-mapping=default
|
||||
seata.service.grouplist=127.0.0.1:8091
|
||||
spring.cloud.alibaba.seata.tx-service-group=business-service
|
||||
seata.service.vgroup-mapping.business-service=default
|
||||
seata.service.grouplist.default=127.0.0.1:8091
|
||||
seata.service.disable-global-transaction=false
|
||||
|
||||
spring.cloud.loadbalancer.ribbon.enabled=true
|
||||
|
@ -1,4 +0,0 @@
|
||||
service {
|
||||
#disable
|
||||
disableGlobalTransaction = false
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
registry {
|
||||
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
|
||||
type = "file"
|
||||
|
||||
nacos {
|
||||
serverAddr = "localhost"
|
||||
namespace = ""
|
||||
cluster = "default"
|
||||
}
|
||||
eureka {
|
||||
serviceUrl = "http://localhost:8761/eureka"
|
||||
application = "default"
|
||||
weight = "1"
|
||||
}
|
||||
redis {
|
||||
serverAddr = "localhost:6379"
|
||||
db = "0"
|
||||
}
|
||||
zk {
|
||||
cluster = "default"
|
||||
serverAddr = "127.0.0.1:2181"
|
||||
session.timeout = 6000
|
||||
connect.timeout = 2000
|
||||
}
|
||||
consul {
|
||||
cluster = "default"
|
||||
serverAddr = "127.0.0.1:8500"
|
||||
}
|
||||
etcd3 {
|
||||
cluster = "default"
|
||||
serverAddr = "http://localhost:2379"
|
||||
}
|
||||
sofa {
|
||||
serverAddr = "127.0.0.1:9603"
|
||||
application = "default"
|
||||
region = "DEFAULT_ZONE"
|
||||
datacenter = "DefaultDataCenter"
|
||||
cluster = "default"
|
||||
group = "SEATA_GROUP"
|
||||
addressWaitTime = "3000"
|
||||
}
|
||||
file {
|
||||
name = "file.conf"
|
||||
}
|
||||
}
|
||||
|
||||
config {
|
||||
# file、nacos 、apollo、zk、consul、etcd3
|
||||
type = "file"
|
||||
|
||||
nacos {
|
||||
serverAddr = "localhost"
|
||||
namespace = ""
|
||||
}
|
||||
consul {
|
||||
serverAddr = "127.0.0.1:8500"
|
||||
}
|
||||
apollo {
|
||||
app.id = "seata-server"
|
||||
apollo.meta = "http://192.168.1.204:8801"
|
||||
}
|
||||
zk {
|
||||
serverAddr = "127.0.0.1:2181"
|
||||
session.timeout = 6000
|
||||
connect.timeout = 2000
|
||||
}
|
||||
etcd3 {
|
||||
serverAddr = "http://localhost:2379"
|
||||
}
|
||||
file {
|
||||
name = "file.conf"
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ spring.cloud.nacos.discovery.server-addr=localhost:8848
|
||||
spring.datasource.name="orderDataSource"
|
||||
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://xxx:3306/fescar?useSSL=false&serverTimezone=UTC
|
||||
spring.datasource.url=jdbc:mysql://xxx:3306/seata?useSSL=false&serverTimezone=UTC
|
||||
spring.datasource.username=xxx
|
||||
spring.datasource.password=xxx
|
||||
spring.datasource.druid.max-active=20
|
||||
@ -13,8 +13,9 @@ spring.datasource.druid.min-idle=2
|
||||
spring.datasource.druid.initial-size=2
|
||||
|
||||
seata.enabled=true
|
||||
seata.service.vgroup-mapping=default
|
||||
seata.service.grouplist=127.0.0.1:8091
|
||||
spring.cloud.alibaba.seata.tx-service-group=business-service
|
||||
seata.service.vgroup-mapping.business-service=default
|
||||
seata.service.grouplist.default=127.0.0.1:8091
|
||||
seata.service.disable-global-transaction=false
|
||||
|
||||
## if use registry center
|
||||
|
@ -5,7 +5,7 @@ spring.cloud.nacos.discovery.server-addr=localhost:8848
|
||||
spring.datasource.name="storageDataSource"
|
||||
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://xxx:3306/fescar?useSSL=false&serverTimezone=UTC
|
||||
spring.datasource.url=jdbc:mysql://xxx:3306/seata?useSSL=false&serverTimezone=UTC
|
||||
spring.datasource.username=xxx
|
||||
spring.datasource.password=xxx
|
||||
spring.datasource.druid.max-active=20
|
||||
@ -14,8 +14,9 @@ spring.datasource.druid.initial-size=2
|
||||
|
||||
|
||||
seata.enabled=true
|
||||
seata.service.vgroup-mapping=default
|
||||
seata.service.grouplist=127.0.0.1:8091
|
||||
spring.cloud.alibaba.seata.tx-service-group=business-service
|
||||
seata.service.vgroup-mapping.business-service=default
|
||||
seata.service.grouplist.default=127.0.0.1:8091
|
||||
seata.service.disable-global-transaction=false
|
||||
|
||||
## if use registry center
|
||||
|
@ -17,79 +17,29 @@
|
||||
package com.alibaba.cloud.seata.feign;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Request;
|
||||
import feign.Response;
|
||||
import io.seata.core.context.RootContext;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
|
||||
|
||||
/**
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
public class SeataFeignBlockingLoadBalancerClient implements Client {
|
||||
public class SeataFeignBlockingLoadBalancerClient
|
||||
extends FeignBlockingLoadBalancerClient {
|
||||
|
||||
private static final Log LOG = LogFactory
|
||||
.getLog(SeataFeignBlockingLoadBalancerClient.class);
|
||||
|
||||
private final Client delegate;
|
||||
|
||||
private final BlockingLoadBalancerClient loadBalancerClient;
|
||||
|
||||
SeataFeignBlockingLoadBalancerClient(Client delegate,
|
||||
BlockingLoadBalancerClient loadBalancerClient) {
|
||||
this.delegate = delegate;
|
||||
this.loadBalancerClient = loadBalancerClient;
|
||||
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 {
|
||||
final URI originalUri = URI.create(request.url());
|
||||
String serviceId = originalUri.getHost();
|
||||
Assert.state(serviceId != null,
|
||||
"Request URI does not contain a valid hostname: " + originalUri);
|
||||
ServiceInstance instance = loadBalancerClient.choose(serviceId);
|
||||
if (instance == null) {
|
||||
String message = "Load balancer does not contain an instance for the service "
|
||||
+ serviceId;
|
||||
if (LOG.isWarnEnabled()) {
|
||||
LOG.warn(message);
|
||||
}
|
||||
return Response.builder().request(request)
|
||||
.status(HttpStatus.SERVICE_UNAVAILABLE.value())
|
||||
.body(message, StandardCharsets.UTF_8).build();
|
||||
}
|
||||
String reconstructedUrl = loadBalancerClient.reconstructURI(instance, originalUri)
|
||||
.toString();
|
||||
Request newRequest = Request.create(request.httpMethod(), reconstructedUrl,
|
||||
enrichRequstHeader(request.headers()), request.requestBody());
|
||||
|
||||
return delegate.execute(newRequest, options);
|
||||
}
|
||||
|
||||
private Map<String, Collection<String>> enrichRequstHeader(
|
||||
Map<String, Collection<String>> headers) {
|
||||
String xid = RootContext.getXID();
|
||||
if (!StringUtils.isEmpty(xid)) {
|
||||
Map<String, Collection<String>> newHeaders = new HashMap<>();
|
||||
newHeaders.putAll(headers);
|
||||
newHeaders.put(RootContext.KEY_XID, Arrays.asList(xid));
|
||||
return newHeaders;
|
||||
}
|
||||
return headers;
|
||||
return super.execute(request, options);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package com.alibaba.cloud.seata.feign;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import feign.Client;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@ -25,6 +23,7 @@ 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;
|
||||
|
||||
@ -50,36 +49,18 @@ public class SeataFeignObjectWrapper {
|
||||
if (bean instanceof LoadBalancerFeignClient) {
|
||||
LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean);
|
||||
return new SeataLoadBalancerFeignClient(client.getDelegate(), factory(),
|
||||
clientFactory(), this.beanFactory);
|
||||
clientFactory(), this);
|
||||
}
|
||||
if (bean.getClass().getName().equals(
|
||||
"org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient")) {
|
||||
return new SeataFeignBlockingLoadBalancerClient(getClient(bean),
|
||||
beanFactory.getBean(BlockingLoadBalancerClient.class));
|
||||
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;
|
||||
}
|
||||
|
||||
private Client getClient(Object bean) {
|
||||
Field client = null;
|
||||
boolean oldAccessible = false;
|
||||
try {
|
||||
client = bean.getClass().getDeclaredField("delegate");
|
||||
oldAccessible = client.isAccessible();
|
||||
client.setAccessible(true);
|
||||
return (Client) client.get(bean);
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.error("get delegate client error", e);
|
||||
}
|
||||
finally {
|
||||
client.setAccessible(oldAccessible);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
CachingSpringLoadBalancerFactory factory() {
|
||||
if (this.cachingSpringLoadBalancerFactory == null) {
|
||||
this.cachingSpringLoadBalancerFactory = this.beanFactory
|
||||
|
@ -17,66 +17,32 @@
|
||||
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.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
|
||||
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
public class SeataLoadBalancerFeignClient extends LoadBalancerFeignClient {
|
||||
|
||||
private static final int MAP_SIZE = 16;
|
||||
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
SeataLoadBalancerFeignClient(Client delegate,
|
||||
CachingSpringLoadBalancerFactory lbClientFactory,
|
||||
SpringClientFactory clientFactory, BeanFactory beanFactory) {
|
||||
super(wrap(delegate, beanFactory), lbClientFactory, clientFactory);
|
||||
this.beanFactory = beanFactory;
|
||||
SpringClientFactory clientFactory,
|
||||
SeataFeignObjectWrapper seataFeignObjectWrapper) {
|
||||
super((Client) seataFeignObjectWrapper.wrap(delegate), lbClientFactory,
|
||||
clientFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response execute(Request request, Request.Options options) throws IOException {
|
||||
Request modifiedRequest = getModifyRequest(request);
|
||||
return super.execute(modifiedRequest, options);
|
||||
}
|
||||
|
||||
private static Client wrap(Client delegate, BeanFactory beanFactory) {
|
||||
return (Client) new SeataFeignObjectWrapper(beanFactory).wrap(delegate);
|
||||
}
|
||||
|
||||
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());
|
||||
return super.execute(request, options);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -45,8 +45,8 @@ public class SentinelContractHolder implements Contract {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) {
|
||||
List<MethodMetadata> metadatas = delegate.parseAndValidatateMetadata(targetType);
|
||||
public List<MethodMetadata> parseAndValidateMetadata(Class<?> targetType) {
|
||||
List<MethodMetadata> metadatas = delegate.parseAndValidateMetadata(targetType);
|
||||
metadatas.forEach(metadata -> METADATA_MAP
|
||||
.put(targetType.getName() + metadata.configKey(), metadata));
|
||||
return metadatas;
|
||||
|
@ -141,7 +141,7 @@ public class DubboServiceBeanMetadataResolver
|
||||
Class<?> targetType) {
|
||||
List<MethodMetadata> methodMetadataList = Collections.emptyList();
|
||||
try {
|
||||
methodMetadataList = contract.parseAndValidatateMetadata(targetType);
|
||||
methodMetadataList = contract.parseAndValidateMetadata(targetType);
|
||||
}
|
||||
catch (Throwable ignored) {
|
||||
// ignore
|
||||
@ -153,7 +153,7 @@ public class DubboServiceBeanMetadataResolver
|
||||
* Select feign contract methods
|
||||
* <p>
|
||||
* extract some code from
|
||||
* {@link Contract.BaseContract#parseAndValidatateMetadata(java.lang.Class)}.
|
||||
* {@link Contract.BaseContract#parseAndValidateMetadata(Class)}.
|
||||
* @param targetType class of type
|
||||
* @return non-null
|
||||
*/
|
||||
|
@ -90,7 +90,7 @@ public class DubboTransportedMethodMetadataResolver {
|
||||
|
||||
private Map<String, RestMethodMetadata> resolveRestRequestMetadataMap(
|
||||
Class<?> targetType) {
|
||||
return contract.parseAndValidatateMetadata(targetType).stream().collect(Collectors
|
||||
return contract.parseAndValidateMetadata(targetType).stream().collect(Collectors
|
||||
.toMap(feign.MethodMetadata::configKey, this::restMethodMetadata));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user