mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
fix loadbalance feignclient seata info
This commit is contained in:
parent
24c8ddee8b
commit
4626e229dd
@ -53,12 +53,7 @@ public class SeataFeignClient implements Client {
|
|||||||
public Response execute(Request request, Request.Options options) throws IOException {
|
public Response execute(Request request, Request.Options options) throws IOException {
|
||||||
|
|
||||||
Request modifiedRequest = getModifyRequest(request);
|
Request modifiedRequest = getModifyRequest(request);
|
||||||
|
|
||||||
try {
|
|
||||||
return this.delegate.execute(modifiedRequest, options);
|
return this.delegate.execute(modifiedRequest, options);
|
||||||
} finally {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Request getModifyRequest(Request request) {
|
private Request getModifyRequest(Request request) {
|
||||||
|
@ -17,7 +17,13 @@
|
|||||||
package org.springframework.cloud.alibaba.seata.feign;
|
package org.springframework.cloud.alibaba.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 io.seata.core.context.RootContext;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||||
|
|
||||||
@ -26,12 +32,15 @@ import feign.Request;
|
|||||||
import feign.Response;
|
import feign.Response;
|
||||||
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
|
||||||
*/
|
*/
|
||||||
public class SeataLoadBalancerFeignClient extends LoadBalancerFeignClient {
|
public class SeataLoadBalancerFeignClient extends LoadBalancerFeignClient {
|
||||||
|
|
||||||
|
private static final int MAP_SIZE = 16;
|
||||||
|
|
||||||
private final BeanFactory beanFactory;
|
private final BeanFactory beanFactory;
|
||||||
|
|
||||||
SeataLoadBalancerFeignClient(Client delegate,
|
SeataLoadBalancerFeignClient(Client delegate,
|
||||||
@ -43,6 +52,7 @@ public class SeataLoadBalancerFeignClient extends LoadBalancerFeignClient {
|
|||||||
|
|
||||||
@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(request, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,4 +60,22 @@ public class SeataLoadBalancerFeignClient extends LoadBalancerFeignClient {
|
|||||||
return (Client) new SeataFeignObjectWrapper(beanFactory).wrap(delegate);
|
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> fescarXid = new ArrayList<>();
|
||||||
|
fescarXid.add(xid);
|
||||||
|
headers.put(RootContext.KEY_XID, fescarXid);
|
||||||
|
|
||||||
|
return Request.create(request.method(), request.url(), headers, request.body(),
|
||||||
|
request.charset());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user