1
0
mirror of synced 2026-05-22 14:43:15 +00:00

fix: 修复 sa-token-dubbo 模块,部分场景下无法正确处理上下文的问题

This commit is contained in:
click33
2025-04-16 15:31:01 +08:00
parent 95f4d62c8e
commit cc681672df
9 changed files with 68 additions and 45 deletions
@@ -10,12 +10,13 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.3.1.RELEASE</version>--> <!--<version>2.3.1.RELEASE</version>-->
<version>2.5.15</version> <!-- <version>2.5.15</version>-->
<version>3.4.3</version>
</parent> </parent>
<!-- 指定一些属性 --> <!-- 指定一些属性 -->
<properties> <properties>
<java.version>1.8</java.version> <java.version>17</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<sa-token.version>1.42.0</sa-token.version> <sa-token.version>1.42.0</sa-token.version>
<dubbo.version>3.2.2</dubbo.version> <dubbo.version>3.2.2</dubbo.version>
@@ -33,7 +34,7 @@
<!-- Sa-Token --> <!-- Sa-Token -->
<dependency> <dependency>
<groupId>cn.dev33</groupId> <groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId> <artifactId>sa-token-spring-boot3-starter</artifactId>
<version>${sa-token.version}</version> <version>${sa-token.version}</version>
</dependency> </dependency>
@@ -2,18 +2,19 @@ server:
# 端口号 # 端口号
port: 8081 port: 8081
spring: spring:
# redis配置 data:
redis: # redis配置
# Redis数据库索引(默认为0 redis:
database: 0 # Redis数据库索引(默认为0
# Redis服务器地址 database: 0
host: 127.0.0.1 # Redis服务器地址
# Redis服务器连接端口 host: 127.0.0.1
port: 6379 # Redis服务器连接端口
# Redis服务器连接密码(默认为空) port: 6379
password: # Redis服务器连接密码(默认为空)
# 连接超时时间 password:
# 连接超时时间
dubbo: dubbo:
application: application:
@@ -10,12 +10,13 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.3.1.RELEASE</version>--> <!--<version>2.3.1.RELEASE</version>-->
<version>2.5.15</version> <!-- <version>2.5.15</version>-->
<version>3.4.3</version>
</parent> </parent>
<!-- 指定一些属性 --> <!-- 指定一些属性 -->
<properties> <properties>
<java.version>1.8</java.version> <java.version>17</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<sa-token.version>1.42.0</sa-token.version> <sa-token.version>1.42.0</sa-token.version>
<dubbo.version>3.2.2</dubbo.version> <dubbo.version>3.2.2</dubbo.version>
@@ -33,7 +34,7 @@
<!-- Sa-Token --> <!-- Sa-Token -->
<dependency> <dependency>
<groupId>cn.dev33</groupId> <groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId> <artifactId>sa-token-spring-boot3-starter</artifactId>
<version>${sa-token.version}</version> <version>${sa-token.version}</version>
</dependency> </dependency>
@@ -14,9 +14,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class Dubbo3ProviderApplication { public class Dubbo3ProviderApplication {
public static void main(String[] args) { public static void main(String[] args) throws Exception {
SpringApplication.run(Dubbo3ProviderApplication.class, args); SpringApplication.run(Dubbo3ProviderApplication.class, args);
System.out.println("Dubbo3ProviderApplication 启动成功"); System.out.println("Dubbo3ProviderApplication 启动成功");
} }
} }
@@ -9,9 +9,18 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class TestController { public class TestController {
@Autowired
private DemoService demoService; // 如果把 @Autowired 改为 @DubboReference
// 则可能在首次调用 dubbo 服务时控制台出现以下异常(只打印异常信息,不影响调用):
// java.lang.reflect.InaccessibleObjectException: Unable to make field private byte java.lang.StackTraceElement.format accessible:
// module java.base does not "opens java.lang" to unnamed module @3a52dba3
//
// 在启动参数上加上如下即可解决:
// --add-opens java.base/java.math=ALL-UNNAMED
@Autowired
public DemoService demoService;
// test // test
@RequestMapping("test") @RequestMapping("test")
@@ -2,19 +2,20 @@ server:
# 端口号 # 端口号
port: 8080 port: 8080
spring: spring:
# redis配置 data:
redis: # redis配置
# Redis数据库索引(默认为0 redis:
database: 0 # Redis数据库索引(默认为0
# Redis服务器地址 database: 0
host: 127.0.0.1 # Redis服务器地址
# Redis服务器连接端口 host: 127.0.0.1
port: 6379 # Redis服务器连接端口
# Redis服务器连接密码(默认为空) port: 6379
password: # Redis服务器连接密码(默认为空)
# 连接超时时间 password:
timeout: 10s # 连接超时时间
timeout: 10s
# Dubbo # Dubbo
dubbo: dubbo:
@@ -16,5 +16,5 @@ public class SaTokenSpringBoot3Application {
SpringApplication.run(SaTokenSpringBoot3Application.class, args); SpringApplication.run(SaTokenSpringBoot3Application.class, args);
System.out.println("\n启动成功:Sa-Token配置如下:" + SaManager.getConfig()); System.out.println("\n启动成功:Sa-Token配置如下:" + SaManager.getConfig());
} }
} }
@@ -16,6 +16,7 @@
package cn.dev33.satoken.context.dubbo.filter; package cn.dev33.satoken.context.dubbo.filter;
import cn.dev33.satoken.SaManager; import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.dubbo.util.SaTokenContextDubboUtil; import cn.dev33.satoken.context.dubbo.util.SaTokenContextDubboUtil;
import cn.dev33.satoken.util.SaTokenConsts; import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.constants.CommonConstants;
@@ -33,11 +34,15 @@ public class SaTokenDubboContextFilter implements Filter {
@Override @Override
public Result invoke(Invoker<?> invoker, Invocation invocation) { public Result invoke(Invoker<?> invoker, Invocation invocation) {
try { if(SaHolder.getContext().isValid()) {
SaTokenContextDubboUtil.setContext(RpcContext.getContext());
return invoker.invoke(invocation); return invoker.invoke(invocation);
} finally { } else {
SaManager.getSaTokenContext().clearContext(); try {
SaTokenContextDubboUtil.setContext(RpcContext.getContext());
return invoker.invoke(invocation);
} finally {
SaManager.getSaTokenContext().clearContext();
}
} }
} }
@@ -16,6 +16,7 @@
package cn.dev33.satoken.context.dubbo3.filter; package cn.dev33.satoken.context.dubbo3.filter;
import cn.dev33.satoken.SaManager; import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.dubbo3.util.SaTokenContextDubbo3Util; import cn.dev33.satoken.context.dubbo3.util.SaTokenContextDubbo3Util;
import cn.dev33.satoken.util.SaTokenConsts; import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.constants.CommonConstants;
@@ -33,11 +34,15 @@ public class SaTokenDubbo3ContextFilter implements Filter {
@Override @Override
public Result invoke(Invoker<?> invoker, Invocation invocation) { public Result invoke(Invoker<?> invoker, Invocation invocation) {
try { if(SaHolder.getContext().isValid()) {
SaTokenContextDubbo3Util.setContext(RpcContext.getServiceContext());
return invoker.invoke(invocation); return invoker.invoke(invocation);
} finally { } else {
SaManager.getSaTokenContext().clearContext(); try {
SaTokenContextDubbo3Util.setContext(RpcContext.getServiceContext());
return invoker.invoke(invocation);
} finally {
SaManager.getSaTokenContext().clearContext();
}
} }
} }