1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Merge pull request #10 from alibaba/master

update
This commit is contained in:
yuhuangbin 2020-03-16 11:24:14 +08:00 committed by GitHub
commit 6095930326
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 51 additions and 227 deletions

20
pom.xml
View File

@ -8,7 +8,7 @@
<parent> <parent>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build</artifactId> <artifactId>spring-cloud-build</artifactId>
<version>2.2.0.RELEASE</version> <version>2.2.2.RELEASE</version>
<relativePath/> <relativePath/>
</parent> </parent>
@ -73,15 +73,15 @@
<properties> <properties>
<!-- Dependency Versions --> <!-- Dependency Versions -->
<spring-cloud-commons.version>2.2.0.RELEASE</spring-cloud-commons.version> <spring-cloud-commons.version>2.2.2.RELEASE</spring-cloud-commons.version>
<spring-cloud-netflix.version>2.2.0.RELEASE</spring-cloud-netflix.version> <spring-cloud-netflix.version>2.2.2.RELEASE</spring-cloud-netflix.version>
<spring-cloud-openfeign.version>2.2.0.RELEASE</spring-cloud-openfeign.version> <spring-cloud-openfeign.version>2.2.2.RELEASE</spring-cloud-openfeign.version>
<spring-cloud-bus.version>2.2.0.RELEASE</spring-cloud-bus.version> <spring-cloud-bus.version>2.2.1.RELEASE</spring-cloud-bus.version>
<spring-cloud-gateway.version>2.2.0.RELEASE</spring-cloud-gateway.version> <spring-cloud-gateway.version>2.2.2.RELEASE</spring-cloud-gateway.version>
<spring-cloud-stream.version>Horsham.RELEASE</spring-cloud-stream.version> <spring-cloud-stream.version>Horsham.SR3</spring-cloud-stream.version>
<spring-cloud-consul.version>2.2.0.RELEASE</spring-cloud-consul.version> <spring-cloud-consul.version>2.2.2.RELEASE</spring-cloud-consul.version>
<spring-cloud-config.version>2.2.0.RELEASE</spring-cloud-config.version> <spring-cloud-config.version>2.2.2.RELEASE</spring-cloud-config.version>
<spring-cloud-zookeeper.version>2.2.0.RELEASE</spring-cloud-zookeeper.version> <spring-cloud-zookeeper.version>2.2.1.RELEASE</spring-cloud-zookeeper.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
<javax-servlet-api>3.0</javax-servlet-api> <javax-servlet-api>3.0</javax-servlet-api>

View File

@ -20,7 +20,7 @@
<properties> <properties>
<sentinel.version>1.7.1</sentinel.version> <sentinel.version>1.7.1</sentinel.version>
<oss.version>3.1.0</oss.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.client.version>1.2.0</nacos.client.version>
<nacos.config.version>0.8.0</nacos.config.version> <nacos.config.version>0.8.0</nacos.config.version>
<acm.version>1.0.9</acm.version> <acm.version>1.0.9</acm.version>

View File

@ -5,7 +5,7 @@ spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.datasource.name="accountDataSource" spring.datasource.name="accountDataSource"
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 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.username=xxx
spring.datasource.password=xxx spring.datasource.password=xxx
spring.datasource.druid.max-active=20 spring.datasource.druid.max-active=20
@ -13,8 +13,9 @@ spring.datasource.druid.min-idle=2
spring.datasource.druid.initial-size=2 spring.datasource.druid.initial-size=2
seata.enabled=true seata.enabled=true
seata.service.vgroup-mapping=default spring.cloud.alibaba.seata.tx-service-group=account-service
seata.service.grouplist=127.0.0.1:8091 seata.service.vgroup-mapping.account-service=default
seata.service.grouplist.default=127.0.0.1:8091
seata.service.disable-global-transaction=false seata.service.disable-global-transaction=false
## if use registry center ## if use registry center

View File

@ -9,8 +9,9 @@ spring.cloud.nacos.discovery.server-addr=localhost:8848
logging.level.io.seata=debug logging.level.io.seata=debug
seata.enabled=true seata.enabled=true
seata.service.vgroup-mapping=default spring.cloud.alibaba.seata.tx-service-group=business-service
seata.service.grouplist=127.0.0.1:8091 seata.service.vgroup-mapping.business-service=default
seata.service.grouplist.default=127.0.0.1:8091
seata.service.disable-global-transaction=false seata.service.disable-global-transaction=false
spring.cloud.loadbalancer.ribbon.enabled=true spring.cloud.loadbalancer.ribbon.enabled=true

View File

@ -1,4 +0,0 @@
service {
#disable
disableGlobalTransaction = false
}

View File

@ -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"
}
}

View File

@ -5,7 +5,7 @@ spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.datasource.name="orderDataSource" spring.datasource.name="orderDataSource"
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 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.username=xxx
spring.datasource.password=xxx spring.datasource.password=xxx
spring.datasource.druid.max-active=20 spring.datasource.druid.max-active=20
@ -13,8 +13,9 @@ spring.datasource.druid.min-idle=2
spring.datasource.druid.initial-size=2 spring.datasource.druid.initial-size=2
seata.enabled=true seata.enabled=true
seata.service.vgroup-mapping=default spring.cloud.alibaba.seata.tx-service-group=business-service
seata.service.grouplist=127.0.0.1:8091 seata.service.vgroup-mapping.business-service=default
seata.service.grouplist.default=127.0.0.1:8091
seata.service.disable-global-transaction=false seata.service.disable-global-transaction=false
## if use registry center ## if use registry center

View File

@ -5,7 +5,7 @@ spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.datasource.name="storageDataSource" spring.datasource.name="storageDataSource"
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 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.username=xxx
spring.datasource.password=xxx spring.datasource.password=xxx
spring.datasource.druid.max-active=20 spring.datasource.druid.max-active=20
@ -14,8 +14,9 @@ spring.datasource.druid.initial-size=2
seata.enabled=true seata.enabled=true
seata.service.vgroup-mapping=default spring.cloud.alibaba.seata.tx-service-group=business-service
seata.service.grouplist=127.0.0.1:8091 seata.service.vgroup-mapping.business-service=default
seata.service.grouplist.default=127.0.0.1:8091
seata.service.disable-global-transaction=false seata.service.disable-global-transaction=false
## if use registry center ## if use registry center

View File

@ -17,79 +17,29 @@
package com.alibaba.cloud.seata.feign; package com.alibaba.cloud.seata.feign;
import java.io.IOException; 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.Client;
import feign.Request; import feign.Request;
import feign.Response; 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.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.http.HttpStatus; import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* @author yuhuangbin * @author yuhuangbin
*/ */
public class SeataFeignBlockingLoadBalancerClient implements Client { public class SeataFeignBlockingLoadBalancerClient
extends FeignBlockingLoadBalancerClient {
private static final Log LOG = LogFactory public SeataFeignBlockingLoadBalancerClient(Client delegate,
.getLog(SeataFeignBlockingLoadBalancerClient.class); BlockingLoadBalancerClient loadBalancerClient,
SeataFeignObjectWrapper seataFeignObjectWrapper) {
private final Client delegate; super((Client) seataFeignObjectWrapper.wrap(delegate), loadBalancerClient);
private final BlockingLoadBalancerClient loadBalancerClient;
SeataFeignBlockingLoadBalancerClient(Client delegate,
BlockingLoadBalancerClient loadBalancerClient) {
this.delegate = delegate;
this.loadBalancerClient = loadBalancerClient;
} }
@Override @Override
public Response execute(Request request, Request.Options options) throws IOException { public Response execute(Request request, Request.Options options) throws IOException {
final URI originalUri = URI.create(request.url()); return super.execute(request, options);
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;
} }
} }

View File

@ -16,8 +16,6 @@
package com.alibaba.cloud.seata.feign; package com.alibaba.cloud.seata.feign;
import java.lang.reflect.Field;
import feign.Client; import feign.Client;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.beans.factory.BeanFactory;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient; import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; 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.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
@ -50,36 +49,18 @@ public class SeataFeignObjectWrapper {
if (bean instanceof LoadBalancerFeignClient) { if (bean instanceof LoadBalancerFeignClient) {
LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean); LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean);
return new SeataLoadBalancerFeignClient(client.getDelegate(), factory(), return new SeataLoadBalancerFeignClient(client.getDelegate(), factory(),
clientFactory(), this.beanFactory); clientFactory(), this);
} }
if (bean.getClass().getName().equals( if (bean instanceof FeignBlockingLoadBalancerClient) {
"org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient")) { FeignBlockingLoadBalancerClient client = (FeignBlockingLoadBalancerClient) bean;
return new SeataFeignBlockingLoadBalancerClient(getClient(bean), return new SeataFeignBlockingLoadBalancerClient(client.getDelegate(),
beanFactory.getBean(BlockingLoadBalancerClient.class)); beanFactory.getBean(BlockingLoadBalancerClient.class), this);
} }
return new SeataFeignClient(this.beanFactory, (Client) bean); return new SeataFeignClient(this.beanFactory, (Client) bean);
} }
return 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() { CachingSpringLoadBalancerFactory factory() {
if (this.cachingSpringLoadBalancerFactory == null) { if (this.cachingSpringLoadBalancerFactory == null) {
this.cachingSpringLoadBalancerFactory = this.beanFactory this.cachingSpringLoadBalancerFactory = this.beanFactory

View File

@ -17,66 +17,32 @@
package com.alibaba.cloud.seata.feign; package com.alibaba.cloud.seata.feign;
import java.io.IOException; 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.Client;
import feign.Request; import feign.Request;
import feign.Response; 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.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory; import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import org.springframework.util.StringUtils;
/** /**
* @author xiaojing * @author xiaojing
* @author yuhuangbin
*/ */
public class SeataLoadBalancerFeignClient extends LoadBalancerFeignClient { public class SeataLoadBalancerFeignClient extends LoadBalancerFeignClient {
private static final int MAP_SIZE = 16;
private final BeanFactory beanFactory;
SeataLoadBalancerFeignClient(Client delegate, SeataLoadBalancerFeignClient(Client delegate,
CachingSpringLoadBalancerFactory lbClientFactory, CachingSpringLoadBalancerFactory lbClientFactory,
SpringClientFactory clientFactory, BeanFactory beanFactory) { SpringClientFactory clientFactory,
super(wrap(delegate, beanFactory), lbClientFactory, clientFactory); SeataFeignObjectWrapper seataFeignObjectWrapper) {
this.beanFactory = beanFactory; super((Client) seataFeignObjectWrapper.wrap(delegate), lbClientFactory,
clientFactory);
} }
@Override @Override
public Response execute(Request request, Request.Options options) throws IOException { public Response execute(Request request, Request.Options options) throws IOException {
Request modifiedRequest = getModifyRequest(request); return super.execute(request, options);
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());
} }
} }

View File

@ -45,8 +45,8 @@ public class SentinelContractHolder implements Contract {
} }
@Override @Override
public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) { public List<MethodMetadata> parseAndValidateMetadata(Class<?> targetType) {
List<MethodMetadata> metadatas = delegate.parseAndValidatateMetadata(targetType); List<MethodMetadata> metadatas = delegate.parseAndValidateMetadata(targetType);
metadatas.forEach(metadata -> METADATA_MAP metadatas.forEach(metadata -> METADATA_MAP
.put(targetType.getName() + metadata.configKey(), metadata)); .put(targetType.getName() + metadata.configKey(), metadata));
return metadatas; return metadatas;

View File

@ -141,7 +141,7 @@ public class DubboServiceBeanMetadataResolver
Class<?> targetType) { Class<?> targetType) {
List<MethodMetadata> methodMetadataList = Collections.emptyList(); List<MethodMetadata> methodMetadataList = Collections.emptyList();
try { try {
methodMetadataList = contract.parseAndValidatateMetadata(targetType); methodMetadataList = contract.parseAndValidateMetadata(targetType);
} }
catch (Throwable ignored) { catch (Throwable ignored) {
// ignore // ignore
@ -153,7 +153,7 @@ public class DubboServiceBeanMetadataResolver
* Select feign contract methods * Select feign contract methods
* <p> * <p>
* extract some code from * extract some code from
* {@link Contract.BaseContract#parseAndValidatateMetadata(java.lang.Class)}. * {@link Contract.BaseContract#parseAndValidateMetadata(Class)}.
* @param targetType class of type * @param targetType class of type
* @return non-null * @return non-null
*/ */

View File

@ -90,7 +90,7 @@ public class DubboTransportedMethodMetadataResolver {
private Map<String, RestMethodMetadata> resolveRestRequestMetadataMap( private Map<String, RestMethodMetadata> resolveRestRequestMetadataMap(
Class<?> targetType) { Class<?> targetType) {
return contract.parseAndValidatateMetadata(targetType).stream().collect(Collectors return contract.parseAndValidateMetadata(targetType).stream().collect(Collectors
.toMap(feign.MethodMetadata::configKey, this::restMethodMetadata)); .toMap(feign.MethodMetadata::configKey, this::restMethodMetadata));
} }