init
This commit is contained in:
56
ruoyi-common/ruoyi-common-seata/pom.xml
Normal file
56
ruoyi-common/ruoyi-common-seata/pom.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-common-seata</artifactId>
|
||||
|
||||
<description>
|
||||
ruoyi-common-seata 分布式事务
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo.extensions</groupId>
|
||||
<artifactId>dubbo-filter-seata</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Seata -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>*</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.dubbo.extensions</groupId>
|
||||
<artifactId>dubbo-filter-seata</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>io.seata</groupId>
|
||||
<artifactId>seata-spring-boot-starter</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.seata</groupId>
|
||||
<artifactId>seata-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2013-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.seata.rest;
|
||||
|
||||
import org.apache.seata.core.context.RootContext;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.client.support.HttpRequestWrapper;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
public class SeataRestTemplateInterceptor implements ClientHttpRequestInterceptor {
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes,
|
||||
ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
|
||||
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(httpRequest);
|
||||
|
||||
String xid = RootContext.getXID();
|
||||
|
||||
if (StringUtils.hasLength(xid)) {
|
||||
requestWrapper.getHeaders().add(RootContext.KEY_XID, xid);
|
||||
}
|
||||
return clientHttpRequestExecution.execute(requestWrapper, bytes);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2013-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.seata.web;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.apache.seata.common.util.StringUtils;
|
||||
import org.apache.seata.core.context.RootContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*
|
||||
* Seata HandlerInterceptor, Convert Seata information into
|
||||
* @see io.seata.core.context.RootContext from http request's header in
|
||||
* {@link org.springframework.web.servlet.HandlerInterceptor#preHandle(HttpServletRequest, HttpServletResponse, Object)},
|
||||
* And clean up Seata information after servlet method invocation in
|
||||
* {@link org.springframework.web.servlet.HandlerInterceptor#afterCompletion(HttpServletRequest, HttpServletResponse, Object, Exception)}
|
||||
*/
|
||||
public class SeataHandlerInterceptor implements HandlerInterceptor {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(SeataHandlerInterceptor.class);
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler) {
|
||||
String xid = RootContext.getXID();
|
||||
String rpcXid = request.getHeader(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);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler, Exception e) {
|
||||
if (StringUtils.isNotBlank(RootContext.getXID())) {
|
||||
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
||||
|
||||
if (StringUtils.isEmpty(rpcXid)) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package org.dromara.common.seata.config;
|
||||
|
||||
import org.dromara.common.core.factory.YmlPropertySourceFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
* seata 配置
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@PropertySource(value = "classpath:common-seata.yml", factory = YmlPropertySourceFactory.class)
|
||||
public class SeataConfiguration {
|
||||
|
||||
}
|
@@ -0,0 +1 @@
|
||||
org.dromara.common.seata.config.SeataConfiguration
|
@@ -0,0 +1,23 @@
|
||||
# 内置配置 不允许修改 如需修改请在 nacos 上写相同配置覆盖
|
||||
# seata配置
|
||||
seata:
|
||||
config:
|
||||
type: nacos
|
||||
nacos:
|
||||
server-addr: ${spring.cloud.nacos.server-addr}
|
||||
group: ${spring.cloud.nacos.config.group}
|
||||
namespace: ${spring.profiles.active}
|
||||
username: ${spring.cloud.nacos.username}
|
||||
password: ${spring.cloud.nacos.password}
|
||||
data-id: seata-server.properties
|
||||
registry:
|
||||
type: nacos
|
||||
nacos:
|
||||
application: ruoyi-seata-server
|
||||
server-addr: ${spring.cloud.nacos.server-addr}
|
||||
group: ${spring.cloud.nacos.discovery.group}
|
||||
username: ${spring.cloud.nacos.username}
|
||||
password: ${spring.cloud.nacos.password}
|
||||
namespace: ${spring.profiles.active}
|
||||
# 关闭自动代理
|
||||
enable-auto-data-source-proxy: false
|
Reference in New Issue
Block a user