2025-03-03
105
在实现一个Excel异步导入功能需要异步解析excel,使用@Async注解启用了解析方法的异步功能。但在解析方法内有Feign远程调用,该接口涉及接口权限,而@Async标注的方法与web请求不在同一个线程中,获取不到request。因此需要从上传接口传递request至异步方法。了解到RequestContextHolder.setRequestAttributes(RequestAttributes attributes, boolean inheritable)方法该方法是 Spring 框架提供的一个工具方法,用于在当前线程中手动设置请求上下文(RequestAttributes)。它主要用于跨线程传递和管理请求的上下文。我们需要在开启新线程前通过该方法手动设置当前的请求,以在多线程中访问到当前的request请求对象。但是我在设置后,获取到的request为空。最后发现是excel解析完成时,之前的导入接口已经完成响应,request对象已经被销毁回收了。后又了解到AsyncContext,它用于控制异步请求的生命周期在HttpServletRequest中提供了一个startAsync()方法用于启动异步处理,它会返回 AsyncContext 对象。此时request在标记异步请求完成前不会被回收。在后台任务完成后,调用 asyncContext.complete() 来标记异步请求的完成。示例代码:private AsyncContext asyncContext = null;public void import(MultipartFile file){ ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); RequestContextHolder.setRequestAttributes(sra, true); if (sra != null) { asyncContext = sra.getRequest().startAsync(); }// 调用异步解析方法parseFile(file)}@Asyncpublic void parseFile(MultipartFile file){// 一些耗时内容ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); RequestContextHolder.setRequestAttributes(sra, true);// 此时能够正常完成Feign调用(已自定义RequestInterceptor拦截器,将所需信息传递)xxFeignClient.xxxxx();// 完成异步处理 asyncContext.complete();}参考:记一次springboot @Async处理导致后续request请求参数获取为空的坑
继续阅读»
2023-06-17
901
m1芯片使用docker不方便,嫌弃占用高,因此使用vps的docker daemon,客户端只需要安装docker-cli服务端1.生成CA私钥文件ca-key.pemopenssl genrsa -aes256 -out ca-key.pem 40962.生成CA公钥文件ca.pemopenssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem3.基于CA文件生成server-key.pem文件openssl genrsa -out server-key.pem 40964.基于server-key.pem文件生成server.csr文件openssl req -subj "/CN=192.168.22.65" -sha256 -new -key server-key.pem -out server.csr注意:生成过程中Common Name填写主机IP:192.168.22.655.输出subjectAltName属性到extfile.cnf文件2选1echo subjectAltName = DNS:$HOST,IP:192.168.22.65,IP:127.0.0.1 extfile.cnfecho subjectAltName = IP:192.168.22.65,IP:0.0.0.0 extfile.cnf注意:TLS连接可以通过域名或IP建立,所以这里DNS:$HOST中的$HOST应填写你的域名。但我的需求是docker主机本机和客户端主机能够访问就可以了192.168.22.65为服务端ip6.输出extendedKeyUsage属性到extfile.cnf文件echo extendedKeyUsage = serverAuth extfile.cnf7.生成签名证书server-cert.pem文件openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf8.创建客户端私钥文件key.pemopenssl genrsa -out key.pem 40969.基于key.pem文件生成client.csr文件openssl req -subj '/CN=client' -new -key key.pem -out client.csr10.输出extendedKeyUsage属性到extfile-client.cnf文件echo extendedKeyUsage = clientAuth extfile-client.cnf11.生成签名证书cert.pem文件openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile-client.cnf12.删除两个csr文件和extfile文件rm -v client.csr server.csr extfile.cnf extfile-client.cnf13.修改**文件权限为只允许所有者读取chmod -v 0400 ca-key.pem key.pem server-key.pem14.修改证书文件权限为只读chmod -v 0444 ca.pem server-cert.pem cert.pem15.将CA证书、服务端证书、服务端**文件拷贝到/etc/docker目录cp -v ca.pem server-cert.pem server-key.pem /etc/docker16.配置/etc/docker/daemon.json文件{ "tlsverify": true, "tlscacert": "/etc/docker/ca.pem", "tlscert": "/etc/docker/server-cert.pem", "tlskey": "/etc/docker/server-key.pem", "hosts": ["tcp://0.0.0.0:2376","unix:///var/run/docker.sock"], "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]}17.修改/lib/systemd/system/docker.service文件中的配置ExecStart = /usr/bin/dockerd最后重启dockersystemctl daemon-reload systemctl restart docker客户端将CA证书、客户端证书、客户端**文件拷贝到/root/.docker目录cp -v ca.pem cert.pem key.pem /root/.docker设置环境变变量export DOCKER_HOST=tcp://192.168.22.65:2376export DOCKER_TLS_VERIFY=1转载自:https://blog.csdn.net/wendrewshay/article/details/88255002?spm=1001.2014.3001.5502
继续阅读»
2022-12-08
940
1.下载Zipkin服务端(jar包)https://search.maven.org/remote_content?g=io.zipkina=zipkin-serverv=LATESTc=exec2.运行Zipkin服务端,看到如下提示就对了,默认将记录数据存在内存中。zxq@ZXQdeMacBook-Air Desktop % java -jar zipkin-server-2.23.19-exec.jar oo oooo oooooo oooooooo oooooooooo oooooooooooo ooooooo ooooooo oooooo ooooooo oooooo ooooooo oooooo o o oooooo oooooo oo oo oooooo ooooooo oooo oooo ooooooo oooooo ooooo ooooo ooooooo oooooo oooooo oooooo ooooooo oooooooo oo oo oooooooo ooooooooooooo oo oo ooooooooooooo oooooooooooo oooooooooooo oooooooo oooooooo oooo oooo ________ ____ _ _____ _ _ |__ /_ _| _ \| |/ /_ _| \ | | / / | || |_) | ' / | || \| | / /_ | || __/| . \ | || |\ | |____|___|_| |_|\_\___|_| \_|:: version 2.23.19 :: commit 0831f9b ::2022-12-08 14:42:57.168 INFO [/] 3936 --- [oss-http-*:9411] c.l.a.s.Server : Serving HTTP at /0:0:0:0:0:0:0:0:9411 - http://127.0.0.1:9411/3.在要记录调用链的服务中 properties spring-cloud.version2021.0.5/spring-cloud.version /properties dependencies dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-sleuth/artifactId /dependency !-- zipkin -- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-sleuth-zipkin/artifactId /dependency /dependencies dependencyManagement dependencies dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-dependencies/artifactId version${spring-cloud.version}/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagementyml:spring: sleuth: sampler: probability: 1.0 zipkin: base-url: http://127.0.0.1:9411 sender: type: web遇到个yml文件读取不到pom.xml中的变量问题,因为将mapper放在java目录下,配置了 resources resource directorysrc/main/java/directory includes include**/*.xml/include /includes /resource resource directorysrc/main/resources/directory /resourceresources导致读取失败,在src/main/resources下配置filtering即可 resource directorysrc/main/resources/directory filteringtrue/filtering /resource参考:https://spring.io/projects/spring-cloud-sleuth#overviewhttps://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
继续阅读»
2021-10-16
1077
首先下载解压版Tomcat,解压多个tomcat然后修改文件夹名:[图片]每个tomcat做如下操作:1. 环境变量(不需要)设置CATALINA_HOME1为tomcat1的目录设置CATALINA_BASE1为tomcat1的目录2. 进入conf文件夹,修改server.xml中的8080,8005,8009端口3. 替换CATALINA_HOME及CATALINA_BASE进入tomcat1的bin目录,将catalina.bat,startup.bat,service.bat进行如下替换替换CATALINA_HOME为CATALINA_HOME1替换CATALINA_BASE为CATALINA_BASE1替换完成后,在bin目录下执行cmd命令./service.bat install你的tomcat服务名如下提示代表安装成功,执行netstarttomcat服务名 即可启动tomcat[图片]在服务列表中可以看到刚刚安装的服务:[图片]记录: jenkins部署至tomcat默认项目时,需将默认的ROOT文件夹删掉,context path填/而不是/ROOT,否则报错:java.net.SocketException: Software caused connection abort: recv failed[图片]
继续阅读»