股权众筹网站开发,云指建站平台,巩义机械厂网站建设,无锡网站优化图片上传是很常见的功能#xff0c;里面有些固定的操作也可以沉淀下来。本文记录使用Abp vNext做图片上传的姿势。目标上传图片-----预览图片-----确定保存支持集群部署实现思路#xff1a;1. 上传图片要使用WebAPI特定媒体类型#xff1a;multipart/form-data;2. 因… 图片上传是很常见的功能里面有些固定的操作也可以沉淀下来。本文记录使用Abp vNext做图片上传的姿势。目标上传图片-----预览图片-----确定保存支持集群部署实现思路1. 上传图片要使用WebAPI特定媒体类型multipart/form-data;2. 因为要做图片预览故在上传时利用AbpCache做一个临时缓存返回图片Id3. 前端利用FileReader渲染预览图4. [确定]: 发起持久化WebAPI利用第2步返回的图片Id为什么强调支持集群部署就这个功能而言[上传预览]和[确定保存]是两次Http WebAPI请求。如果服务端使用的是Redis等进程外缓存: 那这正好是一个Stateless应用功能集群环境无惧如果服务端使用的是进程内缓存:在集群环境前后两次请求有可能打到不同的App服务后置的[确定保存]WebAPI因此可能报错 此处需要做 [会话亲和性] Session affinity实践利用Abp做图片上传IFormFile能力如下红框下面将图片二进制流转化为 base64字符串注入Abp缓存组件IDistributedCachestring缓存图片字符串1小时。[上传预览], [确定保存]的API完整代码如下/// summary/// 上传预览 返回待上传的图片idContent-Typemultipart/form-data/// /summary/// returns/returns[Consumes(multipart/form-data)][Route(upload/preview)][ProducesResponseType(typeof(Guid),200)][HttpPost]public async TaskGuid UploadPicPreviewAsync(IFormFile uploadedFile){var formFileName uploadedFile.FileName;if (!new[] { .png, .jpg, .bmp }.Any((item) formFileName.EndsWith(item))){throw new AbpValidationException(您上传的文件格式必须为png、jpg、bmp中的一种);}byte[] bytes;using (var bodyStream uploadedFile.OpenReadStream()){using (var m new MemoryStream()){await bodyStream.CopyToAsync(m);bytes m.ToArray();}}string base64 Convert.ToBase64String(bytes);var bgId Guid.NewGuid();_cache.Set(${CurrentUser.TenantId}:bg:{bgId}, base64, new DistributedCacheEntryOptions { SlidingExpiration new TimeSpan(1, 0, 0) });return bgId;}/// summary/// 保存图片要使用到前置API的预览图片id/// /summary/// param namecreatePictureInput/param/// returns/returns[Route(upload/)][HttpPost]public async Taskbool UploadPicAsync([FromBody] CreatePictureInput createPictureInput){var based64 await _cache.GetAsync(${CurrentUser.TenantId}:bg:{createPictureInput.PreviewPicId});if (string.IsNullOrEmpty(based64))throw new AbpException(Cache Hotmap Picture do not find);var model ObjectMapper.MapCreatePictureInput, Picture(createPictureInput);model.ProfileId CurrentUser.TenantId;model.BlobStorage Convert.FromBase64String(based64);return await _pictures.InsertAsync(model)! null;}
Default implementation of the IDistributedCache interface is the MemoryDistributedCache which works in-memory. The Distributed Memory Cache (AddDistributedMemoryCache) is a framework-provided implementation of IDistributedCache that stores items in memory. The Distributed Memory Cache isnt an actual distributed cache. Cached items are stored by the app instance on the server where the app is running.以上两段文字来自 Abp和ASP.NET Core官方文档Abp默认的IDistributedCache实现是分布式内存缓存ASP.NETCore 分布式内存缓存是框架内置的是一个假的分布式缓存,实际是单纯的内存缓存。在没有使用真实分布式缓存的情况下 需要对前后两个API配置会话亲和性。会话亲和性下面从nginx、Azure、k8s ingress 三角度配置[会话亲和性](全站生效) 会话亲和性的实现原理是在接受客户端首次请求时响应某个cookie服务器会认定使用同一个cookie的请求为一个会话。1. nginx属于nginx负载均衡的范畴https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/示例如下upstream backend {server backend1.example.com;server backend2.example.com;sticky cookie srv_id expires1h domain.example.com path/;
}
2. Azure App ServiceAzure pp Service是Azure云平台提供的App托管服务具备多实例自动缩放的能力 其有关会话亲和性的配置如图3. K8S nginx-ingress注解nginx.ingress.kubernetes.io/affinity在入口的所有上游中启用和设置亲和性类型。这样请求将总是被定向到相同的上游服务器。https://kubernetes.github.io/ingress-nginx/examples/affinity/cookie/Thats All本文以常见的图片上传功能为例实战演练了Abp的缓存和持久化能力引申出对有状态应用(集群)配置会话亲和性。部署配置要结合业务功能希望对大家有所帮助关注并星标我们更多干货及最佳实践分享