Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:seata for webflux support #3395

Open
wants to merge 1 commit into
base: 2022.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.alibaba.cloud.seata.web.reactive;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author yangfengwei
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class ReactiveSeataHandlerAutoConfiguration {

@Bean
public ReactiveSeataHandlerFilter seataHandlerReactiveFilter() {
return new ReactiveSeataHandlerFilter();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.alibaba.cloud.seata.web.reactive;

import io.seata.common.util.StringUtils;
import io.seata.core.context.RootContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
*
* @author yangfengwei
*/
public class ReactiveSeataHandlerFilter implements WebFilter {

private static final Logger log = LoggerFactory.getLogger(ReactiveSeataHandlerFilter.class);

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
String xid = RootContext.getXID();
String rpcXid = exchange.getRequest().getHeaders().getFirst(RootContext.KEY_XID);
if (log.isDebugEnabled()) {
log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid);
}
if (StringUtils.isBlank(xid) && rpcXid != null) {
RootContext.bind(rpcXid);
if (log.isDebugEnabled()) {
log.debug("bind {} to RootContext", rpcXid);
}
}
Mono<Void> mono = chain.filter(exchange);
return mono.then(Mono.defer(() -> {
if (StringUtils.isNotBlank(RootContext.getXID())) {
if (StringUtils.isNotEmpty(rpcXid)) {
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);
}
}
}
}
return Mono.empty();
}));

}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
com.alibaba.cloud.seata.rest.SeataRestTemplateAutoConfiguration
com.alibaba.cloud.seata.web.SeataHandlerInterceptorConfiguration
com.alibaba.cloud.seata.feign.SeataFeignClientAutoConfiguration
com.alibaba.cloud.seata.web.reactive.ReactiveSeataHandlerAutoConfiguration