简体   繁体   中英

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:

Cloud to On-Premise API

Basically, I have to deploy a public API in the cloud (Azure), but the data are located in on-premise machines. The on-premise machines expose a Swagger API that provides the data. 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:

Secure Internet Access to an On-Premise 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.

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 ..

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).

Obviously, in this scenario, each ApiController in the on-premise API should provide support for web sockets and http at the same time.

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.

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.

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?

  2. How can I provide support in the on-premise API for both protocols HTTP and WebSockets. The on-premise API should be able to process HTTP requests but also WebSocket requests.

  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.

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

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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