Seata集成问题记录

报错信息

1
Caused by: com.netflix.client.ClientException: Load balancer does not have available server for client: ip

问题分析

  • 项目中同时引用了spring-cloud-alibaba-seataspring-cloud-starter-sleuth两个依赖。

  • Sleuth 的配置类TraceFeignClientAutoConfiguration 和 Seata 的配置类 SeataFeignClientAutoConfiguration 都创建了 FeignHystrixBuilder的bean,导致spring加载时冲突

解决方案

方案一

  • 在springboot启动类上排除SeataFeign配置类
1
@SpringBootApplication(exclude = {SeataFeignClientAutoConfiguration.class})
  • 使用自定义拦截器传递XID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Component
@ConditionalOnClass({RequestInterceptor.class, GlobalTransactional.class})
public class SeataFeignInterceptor implements RequestInterceptor {

@Override
public void apply(RequestTemplate template) {

String currentXid = RootContext.getXID();
if (!StringUtils.isEmpty(currentXid)) {

template.header(RootContext.KEY_XID, currentXid);
}
}
}

方案二

  • 如果没有用到zipkinsleuth,可以排除掉maven中的相关依赖

1.4.2版本问题

seata1.4.2中io.seata.spring.util.SpringProxyUtils#findTargetClass相关的代码影响了spring security oauth2的bean初始化

https://github.com/seata/seata/issues/3709

启动报错:

1
2
3
4
[datacenter:10.66.38.102:9011] 2022-02-09 15:21:06.228 ERROR 23440 [main] org.springframework.boot.SpringApplication 826 Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration$OAuth2ClientContextConfiguration': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accessTokenRequest': Post-processing of FactoryBean's singleton object failed; nested exception is java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.accessTokenRequest': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

查看当前develop版本已经修复了https://github.com/seata/seata/blob/develop/spring/src/main/java/io/seata/spring/util/SpringProxyUtils.java

参照issues上的建议,当前版本将修复后的文件覆盖原有文件

https://github.com/ls9527/seata/blob/ddaeaf7678368a8fad0f8c579446ca8ecde2b683/spring/src/main/java/io/seata/spring/util/SpringProxyUtils.java