简体   繁体   English

如何基于Web套接字将HTTP Web请求中继到本地api

[英]How to relay http web request to on premise api based on web sockets

I have to implement an arquitecture for sorting out the problem represented by the following picture: 我必须实现一个arquitecture来解决下图所示的问题:

Cloud to On-Premise API 云到本地API

Basically, I have to deploy a public API in the cloud (Azure), but the data are located in on-premise machines. 基本上,我必须在云(Azure)中部署公共API,但是数据位于本地计算机中。 The on-premise machines expose a Swagger API that provides the data. 本地计算机公开提供数据的Swagger API。 Each on-premise machine has a unique identifier (installation identifier) that allow us to route the requests. 每台本地计算机都有一个唯一的标识符(安装标识符),使我们可以路由请求。

I have developed a solution based on the Relay module of Azure Service Bus following the approach suggested in the following link: 我已按照以下链接中建议的方法开发了基于Azure Service Bus中继模块的解决方案:

Secure Internet Access to an On-Premise API 安全地访问本地API

The solution based on the Relay module of Azure Service Bus sort out my problem and fits most of my needs, but it has one problem, the latency is awful. 基于Azure Service Bus中继模块的解决方案可以解决我的问题并满足我的大多数需求,但是它有一个问题,延迟很糟糕。

I have deployed the public API and the service bus endpoint in the same geographic region and a simple request GET /companies takes 1.5 seconds on average, the processing time in the on-premise API is very low, just a couple of miliseconds .. 我已经在同一地理区域中部署了公共API和服务总线终结点,并且简单的请求GET / companies平均需要1.5秒,本地API中的处理时间非常短,只有几毫秒。

In order to keep the latency low, I have been thinking in using web sockets, but I don't know if is the best approach bearing in mind the scenario because for each request to the public API i would have to open a web socket channel to the on-premise API inside the ApiController and take care of the lifecycle of it (close it before give back the response). 为了保持较低的延迟,我一直在考虑使用Web套接字,但是我不知道这种情况是否是最好的方法,因为对于公共API的每个请求,我都必须打开Web套接字通道到ApiController内部的本地API,并注意其生命周期(在返回响应之前将其关闭)。

Obviously, in this scenario, each ApiController in the on-premise API should provide support for web sockets and http at the same time. 显然,在这种情况下,本地API中的每个ApiController应该同时提供对Web套接字和http的支持。

Another solution, it would be to implement a http proxy that relay the original request from the public API to the on-premise API but, from my point of view, the best latency possible is achieved using a communication P2P without connectivity managers in the middle, for that reason my focus is now on web sockets. 另一个解决方案是实现一个http代理,该代理将原始请求从公共API中继到本地API,但从我的角度来看,使用中间没有连接管理器的通信P2P可以实现最佳的延迟,因此,我现在的重点是Web套接字。

I want to meet the standards as much as possible, for this reason I want to use "pure" web sockets, this means not use Signalr. 我想尽可能地满足标准,因此我要使用“纯” Web套接字,这意味着不使用Signalr。

Once, I have exposed my problem, the solution and the alternatives, my questions are as follows: 一次,我暴露了我的问题,解决方案和替代方案,我的问题如下:

  1. How can I open a web socket inside an action method of a ApiController class and relay the original http request from the public API to the on-premise API? 如何在ApiController类的action方法内打开Web套接字,并将原始的HTTP请求从公共API中继到本地API?

  2. How can I provide support in the on-premise API for both protocols HTTP and WebSockets. 如何在本地API中为协议HTTP和WebSockets提供支持。 The on-premise API should be able to process HTTP requests but also WebSocket requests. 本地API应该能够处理HTTP请求,但也可以处理WebSocket请求。

  3. My first bet is using System.Net.WebSockets, but I would like to know if exists any websockets library that shines when is integrated with ASP.NET Web API. 我的第一个选择是使用System.Net.WebSockets,但我想知道是否存在与ASP.NET Web API集成时会发光的任何websockets库。

a simple request GET /companies takes 1.5 seconds on average, the processing time in the on-premise API is very low, just a couple of miliseconds 一个简单的请求GET / companies平均需要1.5秒,本地API中的处理时间非常短,只有几毫秒

You are using Azure Relay service to securely expose services to the public cloud, as far as I know, we would not achieve the same latency as an on-premise server due to many factors affecting (for example the client has to first authenticate to the Service Bus Relay). 据我所知,您正在使用Azure Relay服务将服务安全地公开到公共云,由于许多因素,我们将无法获得与本地服务器相同的延迟(例如,客户端必须先向客户端身份验证服务总线中继)。

Besides, this article explained with example how to create custom HTTP handler to accept client WebSocket requests, you could refer to it to modify your API app to handle WebSocket requests. 此外, 本文还以示例方式说明了如何创建自定义HTTP处理程序以接受客户端WebSocket请求,您可以引用它来修改API应用程序以处理WebSocket请求。

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

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