RequestContextHolder多线程获取不到request对象
RequestContextHolder多线程获取不到request对象,调用feign接口时,在Feign中的RequestInterceptor也获取不到HttpServletRequest问题解决方案。
1.RequestContextHolder多线程获取不到request对象
异常信息,报错如下:
2024-07-09 22:06:32.320 [pool-5-thread-2] ERROR com.mergeplus.handler.ObjectHandler:227 - class: interface org.jeecg.common.system.api.client.SysFeignClient, methodName=mergeRegion, error: {}
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.at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:313)at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:329)at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:324)at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:283)at jdk.proxy2/jdk.proxy2.$Proxy179.getHeaderNames(Unknown Source)at com.mergeplus.handler.ObjectHandler.lambda$doHandler$0(ObjectHandler.java:155)at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run$$$capture(CompletableFuture.java:1768)at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java)at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)at java.base/java.lang.Thread.run(Thread.java:833)
异常错误截图:

2.解决方案
// 设置子线程共享(重点)final RequestAttributes attributes = RequestContextHolder.getRequestAttributes();merges.stream().map(e -> CompletableFuture.supplyAsync(() -> {JSONObject returnMap = null;try {if (e.getClientBean() != null) {// 设置当前线程的 RequestAttributes(重点)RequestContextHolder.setRequestAttributes(attributes);Object returnValue = e.getMethod().invoke(e.getClientBean(), jsonObject.get(e.getSourceKey()));if (returnValue == null) {return e;}returnMap = JSON.parseObject(JSON.toJSONString(returnValue));if (returnMap == null || returnMap.isEmpty()) {return e;}result.put(e.getTargetKey(), returnMap.get(jsonObject.get(e.getSourceKey())));} else {if (e.getUrl() == null || e.getUrl().trim().length() == 0) {return e;}//设置Http的HeaderHttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);//设置访问参数HashMap<String, Object> params = new HashMap<>();if (e.getKey() == null || e.getKey().trim().length() == 0) {params.put(e.getSourceKey(), jsonObject.get(e.getSourceKey()));}//设置访问的EntityHttpEntity entity = new HttpEntity<>(params, headers);// todoResponseEntity<Object> exchange = restTemplate.exchange(e.getUrl(), HttpMethod.GET, entity, Object.class);if (exchange == null) {return e;}Object body = exchange.getBody();if (body == null) {return e;}returnMap = JSON.parseObject(JSON.toJSONString(body));if (returnMap == null || returnMap.isEmpty()) {return e;}result.put(e.getTargetKey(), returnMap.get(jsonObject.get(e.getSourceKey())));// if (body instanceof Map) {
// returnMap = (Map) body;
// if (returnMap == null || returnMap.isEmpty()) {
// return e;
// }
// result.put(e.getTargetKey(), returnMap.get(jsonObject.get(e.getSourceKey())));
// }}} catch (Exception ex) {log.error("class: {}, methodName={}, error: {}", e.getClientBeanClazz(), e.getMethod().getName(), ex);} finally {RequestContextHolder.resetRequestAttributes(); // 记得在最后重置请求属性(重点)}return e;}, executor)).toList().stream().map(CompletableFuture::join).collect(Collectors.toList());


FeignInterceptorConfig代码
package org.jeecg.config;import java.io.IOException;
import java.util.*;import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.PathMatcherUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.config.sign.interceptor.SignAuthConfiguration;
import org.jeecg.config.sign.util.HttpUtils;
import org.jeecg.config.sign.util.SignUtil;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;import feign.Feign;
import feign.Logger;
import feign.RequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import lombok.extern.slf4j.Slf4j;/*** @Description: FeignConfig* @author: JeecgBoot*/
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
@Slf4j
@Configuration
public class FeignInterceptorConfig {@Beanpublic RequestInterceptor requestInterceptor() {return requestTemplate -> {ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (requestAttributes == null) {return;}//1.获取请求对象HttpServletRequest request = requestAttributes.getRequest();Enumeration<String> headerNames = request.getHeaderNames();if (headerNames == null || !headerNames.hasMoreElements()) {return;}//2.获取请求对象中的所有的头信息(网关传递过来的)while (headerNames.hasMoreElements()) {String name = headerNames.nextElement();//头的名称// 跳过content-length,不然可能会报too many bites written问题if ("content-length".equalsIgnoreCase(name)) {continue;}String value = request.getHeader(name);//头名称对应的值// System.out.println("name:" + name + "::::::::value:" + value);//3.将头信息传递给feign (restTemplate)requestTemplate.header(name,value);// if (CommonConstant.X_ACCESS_TOKEN.equals(name) && StringUtils.isBlank(value)) {
// String token = UserTokenContext.getToken();
// requestTemplate.header(CommonConstant.X_ACCESS_TOKEN, token);
// }}//================================================================================================================//针对特殊接口,进行加签验证 ——根据URL地址过滤请求 【字典表参数签名验证】// todoif (PathMatcherUtil.matches(Arrays.asList(PathMatcherUtil.SIGN_URL_LIST),requestTemplate.path())) {
// if (PathMatcherUtil.matches(Arrays.asList(SignAuthConfiguration.SIGN_URL_LIST),requestTemplate.path())) {try {log.info("============================ [begin] fegin api url ============================");log.info(requestTemplate.path());log.info(requestTemplate.method());String queryLine = requestTemplate.queryLine();String questionMark="?";if(queryLine!=null && queryLine.startsWith(questionMark)){queryLine = queryLine.substring(1);}log.info(queryLine);if(requestTemplate.body()!=null){log.info(new String(requestTemplate.body()));}SortedMap<String, String> allParams = HttpUtils.getAllParams(requestTemplate.path(),queryLine,requestTemplate.body(),requestTemplate.method());String sign = SignUtil.getParamsSign(allParams);log.info(" Feign request params sign: {}",sign);log.info("============================ [end] fegin api url ============================");requestTemplate.header(CommonConstant.X_SIGN, sign);requestTemplate.header(CommonConstant.X_TIMESTAMP, String.valueOf(System.currentTimeMillis()));} catch (IOException e) {e.printStackTrace();}}//================================================================================================================};}}
3.相关大数据学习demo地址:
https://github.com/carteryh/big-data
相关文章:
RequestContextHolder多线程获取不到request对象
RequestContextHolder多线程获取不到request对象,调用feign接口时,在Feign中的RequestInterceptor也获取不到HttpServletRequest问题解决方案。 1.RequestContextHolder多线程获取不到request对象 异常信息,报错如下: 2024-07-0…...
打造高效工作与生活质量的完美平衡
在快节奏的编程行业中,保持健康的工作与生活平衡是至关重要的。长时间坐在电脑前、面对紧凑的项目截止日期和频繁的加班文化,很容易导致身心健康问题,如眼睛疲劳、颈部和背部疼痛、压力累积、睡眠障碍乃至慢性疾病。因此,采取积极…...
【零基础】学JS之APIS第四天
💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 非常期待和您一起在这个小…...
走进linux
1、为什么要使用linux 稳定性和可靠性: Linux内核以其稳定性而闻名,能够持续运行数月甚至数年而不需要重新启动。这对于服务器来说至关重要,因为它们需要保持长时间的稳定运行,以提供持续的服务 安全性: Linux系统…...
智能家居开发新进展:乐鑫 ESP-ZeroCode 与亚马逊 ACK for Matter 实现集成
日前,乐鑫 ESP-ZeroCode 与亚马逊 Alexa Connect Kit (ACK) for Matter 实现了集成。这对智能家居设备制造商来说是一项重大进展。开发人员无需编写固件或开发移动应用程序,即可轻松设计符合 Matter 标准的产品。不仅如此,开发者还可以在短短…...
本地事务和分布式事务
一、本地事务 1、事务的基本特性 数据库事务的几个基本特性:原子性、一致性、隔离性、持久性。 原子性:一系列的操作整体不可拆分,要么同时成功,要么同时失败。 一致性:数据在事务的前后,业务整体一…...
昇思25天学习打卡营第14天|基于MindNLP的文本解码原理
基于MindNLP的文本解码原理 文本解码 文本解码是自然语言处理中的一个关键步骤,特别是在任务如机器翻译、文本摘要、自动回复生成等领域。解码过程涉及将编码器(如语言模型、翻译模型等)的输出转换为可读的文本序列。以下是一些常见的文本解码方法和原理: 1. 自回归解码:…...
Base64文件流查看下载PDF方法-CSDN
问题描述 数票通等接口返回的PDF类型发票是以Base64文件流的方式返回的,无法直接查看预览PDF发票, 处理方法 使用第三方在线工具:https://www.jyshare.com/front-end/61/ 在Html代码框中粘贴如下代码 <embed type"application/pd…...
基于TCP的在线词典系统(分阶段实现)(阻塞io和多路io复用(select)实现)
1.功能说明 一共四个功能: 注册 登录 查询单词 查询历史记录 单词和解释保存在文件中,单词和解释只占一行, 一行最多300个字节,单词和解释之间至少有一个空格。 2.功能演示 3、分阶段完成各个功能 3.1 完成服务器和客户端的连接 servic…...
设置DepthBufferBits和设置DepthStencilFormat的区别
1)设置DepthBufferBits和设置DepthStencilFormat的区别 2)Unity打包exe后,游戏内拉不起Steam的内购 3)Unity 2022以上Profiler.FlushMemoryCounters耗时要怎么关掉 4)用GoodSky资产包如何实现昼夜播发不同音乐功能 这是…...
MySQL零散拾遗
mysql中大小写敏感吗? MySQL数据库默认情况下是不区分大小写的,这意味着在查询时,字段名和值的大小写不会影响结果。然而,这种默认行为可能会根据操作系统和配置的不同而有所变化。 在某些操作系统上,比如Linux&…...
kali安装vulhub遇到的问题及解决方法(docker及docker镜像源更换)
kali安装vulhub: 提示:项目地址 https://github.com/vulhub/vulhub 项目安装: git clone https://github.com/vulhub/vulhub.git 安装docker 提示:普通用户请使用sudo: 首先安装 https 协议、CA 证书 apt-get in…...
开源数字人项目Hallo
硬件条件: gpu最低12G 软件: cuda需支持 Python选择3.10吧,我的版本3.11 源码: GitHub - fudan-generative-vision/hallo: Hallo: Hierarchical Audio-Driven Visual Synthesis for Portrait Image Animation models文件&…...
Linux 命令集
修改主机名/关机/重启 1)hostnamectl 命令 $ hostnamectl # 查看操作系统信息(内核、操作系统发行版本、主机名等) $ hostnamectl set-hostname redhatu8 # 修改主机名2)shutdown 关机 $ shutdown -h now # 马上关机3&#…...
QML 鼠标和键盘事件
学习目标:Qml 鼠标和键盘事件 学习内容 1、QML 鼠标事件处理QML 直接提供 MouseArea 来捕获鼠标事件,该操作必须配合Rectangle 获取指定区域内的鼠标事件, 2、QML 键盘事件处理,并且获取对OML直接通过键盘事件 Keys 监控键盘任意按键应的消…...
WPF引入多个控件库使用
目的 设计开发时有的控件库的一部分符合我们想要的UI样式,另一部分来自另一个控件库,想把两种库的样式做一个整合在同一个控件资源上。单纯通过引用的方式会导致原有样式被覆盖。这里通过设置全局样式的方式来实现。 1.安装控件库nuget包:H…...
【Linux】1w详解如何实现一个简单的shell
目录 实现思路 1. 交互 获取命令行 2. 子串分割 解析命令行 3. 指令的判断 内建命令 4. 普通命令的执行 补充:vim 文本替换 整体代码 重点思考 1.getenv和putenv是什么意思 2.代码extern char **environ; 3.内建命令是什么 4.lastcode WEXITSTATUS(sta…...
单目测距 单目相机测距 图片像素坐标转实际坐标的一种转换方案
需要相机位置固定 原图 红色的点是我们标注的像素点,这些红色的点我们知道它的像素坐标,以及以右下角相机位置为原点的x y 实际坐标数值 通过转换,可以得到整个图片内部其余像素点的实际坐标, 这些红色的点是通过转换关系生成的&…...
ensp防火墙综合实验作业+实验报告
实验目的要求及拓扑图: 我的拓扑: 更改防火墙和交换机: [USG6000V1-GigabitEthernet0/0/0]ip address 192.168.110.5 24 [USG6000V1-GigabitEthernet0/0/0]service-manage all permit [Huawei]vlan batch 10 20 [Huawei]int g0/0/2 [Huawei-…...
【大模型LLM面试合集】大语言模型基础_Word2Vec
Word2Vec 文章来源:Word2Vec详解 - 知乎 (zhihu.com) 1.Word2Vec概述 Word2Vec是google在2013年推出的一个NLP工具,它的特点是能够将单词转化为向量来表示,这样词与词之间就可以定量的去度量他们之间的关系,挖掘词之间的联系。 …...
挑战杯推荐项目
“人工智能”创意赛 - 智能艺术创作助手:借助大模型技术,开发能根据用户输入的主题、风格等要求,生成绘画、音乐、文学作品等多种形式艺术创作灵感或初稿的应用,帮助艺术家和创意爱好者激发创意、提高创作效率。 - 个性化梦境…...
边缘计算医疗风险自查APP开发方案
核心目标:在便携设备(智能手表/家用检测仪)部署轻量化疾病预测模型,实现低延迟、隐私安全的实时健康风险评估。 一、技术架构设计 #mermaid-svg-iuNaeeLK2YoFKfao {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg…...
线程同步:确保多线程程序的安全与高效!
全文目录: 开篇语前序前言第一部分:线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分:synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分ÿ…...
visual studio 2022更改主题为深色
visual studio 2022更改主题为深色 点击visual studio 上方的 工具-> 选项 在选项窗口中,选择 环境 -> 常规 ,将其中的颜色主题改成深色 点击确定,更改完成...
使用分级同态加密防御梯度泄漏
抽象 联邦学习 (FL) 支持跨分布式客户端进行协作模型训练,而无需共享原始数据,这使其成为在互联和自动驾驶汽车 (CAV) 等领域保护隐私的机器学习的一种很有前途的方法。然而,最近的研究表明&…...
iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版分享
平时用 iPhone 的时候,难免会碰到解锁的麻烦事。比如密码忘了、人脸识别 / 指纹识别突然不灵,或者买了二手 iPhone 却被原来的 iCloud 账号锁住,这时候就需要靠谱的解锁工具来帮忙了。Aiseesoft iPhone Unlocker 就是专门解决这些问题的软件&…...
渗透实战PortSwigger靶场-XSS Lab 14:大多数标签和属性被阻止
<script>标签被拦截 我们需要把全部可用的 tag 和 event 进行暴力破解 XSS cheat sheet: https://portswigger.net/web-security/cross-site-scripting/cheat-sheet 通过爆破发现body可以用 再把全部 events 放进去爆破 这些 event 全部可用 <body onres…...
智能在线客服平台:数字化时代企业连接用户的 AI 中枢
随着互联网技术的飞速发展,消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁,不仅优化了客户体验,还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用,并…...
leetcodeSQL解题:3564. 季节性销售分析
leetcodeSQL解题:3564. 季节性销售分析 题目: 表:sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...
R语言速释制剂QBD解决方案之三
本文是《Quality by Design for ANDAs: An Example for Immediate-Release Dosage Forms》第一个处方的R语言解决方案。 第一个处方研究评估原料药粒径分布、MCC/Lactose比例、崩解剂用量对制剂CQAs的影响。 第二处方研究用于理解颗粒外加硬脂酸镁和滑石粉对片剂质量和可生产…...
