编辑
2024-11-14
学习记录
00

前提

如果被调用服务方法有HttpServletResponse,会导致调用失败,因为Feign不能处理HttpServletResponse

使用返回值ResponseEntity<org.springframework.core.io.Resource>进行处理

方法

调用方

java
@GetMapping("/download") public void download(@RequestParam(value = "fileUrl") String fileUrl, HttpServletResponse response){ try { ResponseEntity<org.springframework.core.io.Resource> responseEntity = remoteFileService.downloadByRes(fileUrl);; if (responseEntity.getStatusCode().is2xxSuccessful()) { org.springframework.core.io.Resource resource = responseEntity.getBody(); if (resource != null) { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + resource.getFilename()); try (OutputStream outputStream = response.getOutputStream()) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = resource.getInputStream().read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); } } } else { response.setStatus(responseEntity.getStatusCodeValue()); } } catch (IOException e) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
编辑
2024-11-14
实用工具
00

说明

Seata 是阿里巴巴开源的分布式事务中间件,以高效并且对业务 0 侵入的方式,解决微服务场景下面临的分布式事务问题。 Seata 将为用户提供了 AT、TCC、SAGA 和XA 事务模式,为用户打造一站式的分布式解决方案。AT模式是阿里首推的模式,阿里云上有商用版本的GTS(Global Transaction Service 全局事务服务)。

github地址

AT模式

在 AT 模式下,用户只需关注自己的“业务 SQL”,用户的 “业务 SQL” 作为一阶段,Seata 框架会自动生成事务的二阶段提交和回滚操作。

Server 端存储模式(store.mode)支持三种方式:

file:单机模式(默认为此模式),全局事务会话信息存储在内存中,读写并持久化至本地文件 root.data (bin\sessionStore\root.data) 中,性能较高。 db:高可用模式(Mysql 5.7+),全局事务会话信息通过db共享,相应性能差些。 redis:Seata-Server 1.3及以上版本支持,性能较高,但存在事务信息丢失风险,请提前配置适合当前场景的redis持久化配置。

创建 undo_log 表

Seata AT 模式 需要使用到 undo_log 表。

sql
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log CREATE TABLE `undo_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `branch_id` bigint(20) NOT NULL, `xid` varchar(100) NOT NULL, `context` varchar(128) NOT NULL, `rollback_info` longblob NOT NULL, `log_status` int(11) NOT NULL, `log_created` datetime NOT NULL, `log_modified` datetime NOT NULL, `ext` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
编辑
2024-11-14
实用工具
00

Service Interface

IService 是 MyBatis-Plus 提供的一个通用 Service 层接口,它封装了常见的 CRUD 操作,包括插入、删除、查询和分页等。通过继承 IService 接口,可以快速实现对数据库的基本操作,同时保持代码的简洁性和可维护性。

IService 接口中的方法命名遵循了一定的规范,如 get 用于查询单行,remove 用于删除,list 用于查询集合,page 用于分页查询,这样可以避免与 Mapper 层的方法混淆。

保存

  • save
  • saveBatch
  • saveOrUpdate
  • saveOrUpdateBatch

删除

  • remove
  • removeById
  • removeByIds
  • removeByMap

更新

  • update
  • updateById
  • updateBatchById

查询

  • get
  • getOne
  • getById
  • getObj
  • list
  • listByIds
  • listByMap
  • listMaps
  • page
  • count
编辑
2024-11-12
实用工具
00

前提

wsl中不配置互通,一个docker服务不能通过localhost地址访问另外的服务

配置

打开或创建位于 %UserProfile% 目录下的 .wslconfig 文件(例如 C:\Users\你的用户名.wslconfig)。

[wsl2] memory=4GB # 分配给 WSL 2 的内存大小 processors=2 # 分配给 WSL 2 的 CPU 核心数 localhostForwarding=true # 是否启用 localhost 转发 [experimental] autoMemoryReclaim=gradual # 开启自动回收内存,可在 gradual, dropcache, disabled 之间选择 networkingMode=mirrored # 开启镜像网络 dnsTunneling=true # 开启 DNS Tunneling firewall=true # 开启 Windows 防火墙 autoProxy=true # 开启自动同步代理 sparseVhd=true # 开启自动释放 WSL2 虚拟硬盘空间

重启服务

管理员身份运行PowerShell:

停止WSL: wsl --shutdown

启动WSL: wsl

编辑
2024-11-12
遇到的问题
00

提示

org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.

这个错误信息表明你上传的文件大小超过了Tomcat服务器允许的最大限制。默认情况下,Tomcat对文件上传的大小有限制,通常是1MB(1048576字节)。

解决方法

application.yml增加文件大小的配置

yml
spring: servlet: multipart: max-file-size: 20MB max-request-size: 20MB

java配置类

java
import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.unit.DataSize; import javax.servlet.MultipartConfigElement; @Configuration public class FileUploadConfig { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // 设置单个文件最大值 factory.setMaxFileSize(DataSize.ofMegabytes(2)); // 2MB // 设置总上传数据总大小 factory.setMaxRequestSize(DataSize.ofMegabytes(2)); // 2MB return factory.createMultipartConfig(); } }