简体   繁体   English

用于Netty和socket.io的图像服务器

[英]Image server for Netty and socket.io

I'm looking for a solution for the following problem in my architecture: 我正在为我的体系结构中的以下问题寻找解决方案:

I have a rich front-end application (React, RxJS, socket.io etc) and a web service layer written on top of netty-socketio, which I currently run just as Netty app. 我有一个丰富的前端应用程序(React,RxJS,socket.io等)和一个写在netty-socketio之上的Web服务层,我目前正作为Netty应用程序运行。 The idea is that the server acts as an API layer that any client implementing socket.io protocol could consume, my front-end application being one of them. 这个想法是服务器充当任何实现socket.io协议的客户端都可以使用的API层,我的前端应用程序就是其中之一。 I could host it anywhere really, probably a cdn. 我可以在任何地方托管它,可能是CDN。

The problem I've ran into is file, more specifically image handling. 我遇到的问题是文件,更具体地说是图像处理。 The classic use-case is uploading user avatars. 经典的用例是上传用户头像。 My plain socket.io protocol doesn't support it, so I've come up with a couple of theoretical solutions: 我的普通socket.io协议不支持它,因此我提出了一些理论上的解决方案:

  1. Upload images as binary through the socket.io API, store images on the server's file system, access them as binaries as well. 通过socket.io API将图像上传为二进制文件,将图像存储在服务器的文件系统中,也将它们作为二进制文件进行访问。 My problem with this one is the serialization/deserialization I'd have to do, and it seems error prone with different file extensions and such. 我与此有关的问题是我必须要做的序列化/反序列化,而且使用不同的文件扩展名等似乎容易出错。

  2. Implement an HTTP parser in Netty, like this example , run it in the same instance on a different port. 此示例一样 ,在Netty中实现HTTP解析器,并在同一实例中的不同端口上运行它。 I've tested this and it works, but it's really low level and I'm no expert in Netty. 我已经对此进行了测试,但它确实很低,我也不是Netty的专家。

  3. Create a separate HTTP file server, maybe using servlets and use it to store and reference images, have the front-end upload to this directly and just send a reference to the socket.io API to persist in the DB. 创建一个单独的HTTP文件服务器,也许使用servlet,并使用它存储和引用图像,将前端直接上传到该服务器,然后仅发送对socket.io API的引用以保留在数据库中。 The thing I'm not sure on is the fact that this way the API basically expects the UI-s to handle their own image storage and just report references, and that seems unsafe and uncontrollable. 我不确定的是,API基本上希望UI能够处理自己的图像存储并仅报告引用,这似乎是不安全和不可控制的。

  4. Use a CDN instead for the behavior described in (3). 使用CDN代替(3)中描述的行为。 This would be a nice production-ready solution but maybe overkill in my non-production system. 这将是一个很好的生产就绪解决方案,但在我的非生产系统中可能会适得其反。

Note, in cases (3) and (4) I could host the front-end in the same place, right now I'm using a local node.js http-server for that. 请注意,在情况(3)和(4)中,我可以将前端托管在同一位置,现在我正在使用本地node.js http服务器。

Any advice, opinion, solution I didn't consider? 我没有考虑任何建议,意见和解决方案?

For knowledge sharing purposes I'll answer my question. 为了共享知识,我将回答我的问题。 I went with option two because I didn't want to separate the image storing logic from the rest of the application. 我选择了第二个选项,因为我不想将图像存储逻辑与应用程序的其余部分分开。 Image upload is now a 2-stage process: the user initiates a HTTP POST request with the image body, to the same ip, on a different port. 图像上载现在是一个两阶段的过程:用户在不同端口上将具有图像主体的HTTP POST请求发起到同一ip。 A Netty handler catches the message, and parses the HTTP POST, much like the referenced example. Netty处理程序捕获消息,并解析HTTP POST,这与引用的示例非常相似。 The image is then stored in memory as a byte buffer (for a limited amount of time), and a unique UUID is returned in the HTTP response. 然后将图像作为字节缓冲区(在有限的时间内)存储在内存中,并在HTTP响应中返回唯一的UUID。 After this, it's the client's responsability to call the relevant API through the websocket connection, send the relevant text data and include the aforementioned UUID as a reference to the uploaded data. 此后,客户有责任通过websocket连接调用相关的API,发送相关的文本数据, 包含上述UUID作为对上载数据的引用。 The socket handler than validates this request, reads the byte buffer based on the hash from temporary store, writes it to disk. 然后,套接字处理程序将验证此请求,根据临时存储中的哈希读取字节缓冲区,然后将其写入磁盘。 The nice thing about this is the temporary buffer, so requests can be properly validated before writing to disk. 这样做的好处是临时缓冲区,因此可以在写入磁盘之前正确验证请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM