Ocelot没有将WebSocket传递给微服务 [英] Ocelot not passing websockets to microservice

查看:134
本文介绍了Ocelot没有将WebSocket传递给微服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ocelot作为代理连接到SignalR集线器.SignalR插入网关通过websockets流量传递的微服务中.通过HTTP请求进行的协商已成功执行,但似乎无法通过websockets进行进一步的通信.我不知道发生了什么,特别是当在另一个环境上使用Azure SignalR时,具有相同配置的通信可以完美地工作.下面,我介绍我的网关配置:

I'm trying to connect to SignalR hubs using Ocelot as proxy. SignalR is plugged in microservice that gateway passing through websockets traffic. Negotation via HTTP request is executed successfully, but further communication via websockets seems to be lost. I don't have an idea what is going on, especially that communication with the same configuration works perfectly when Azure SignalR on another environment is used. Below I present my configuration for gateway:

ocelot.json

{
  "DownstreamPathTemplate": "/{anyHub}/negotiate",
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
    {
      "Host": "communication",
      "Port": 80
    }
  ],
  "UpstreamHttpMethod": [ "POST" ],
  "UpstreamPathTemplate": "/{anyHub}/negotiate",
  "ReRouteIsCaseSensitive": false,
  "AuthenticationOptions": {
    "AuthenticationProviderKey": "Bearer",
    "AllowedScopes": []
  },
  "DelegatingHandlers": [
    "IdentityInQuery"
  ]
},
{
  "DownstreamPathTemplate": "/{anyHub}",
  "ReRouteIsCaseSensitive": false,
  "DownstreamScheme": "ws",
  "DownstreamHostAndPorts": [
    {
      "Host": "communication",
      "Port": 80
    }
  ],
  "UpstreamPathTemplate": "/{anyHub}",
  "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE", "OPTIONS" ]
},

网关的Program.cs的一部分

.Configure(async app =>
{
await app
    .UseCors(cors =>
{
    cors.AllowAnyHeader()
        .AllowAnyMethod()
        .SetIsOriginAllowed(x => true)
        .AllowCredentials();
}).UseOcelot();

if (turnOnWebsockets)
    app.UseWebSockets();

特定的微服务集合扩展:

public static ISignalRBuilder AddSignalRConfiguration(this IServiceCollection services, bool isDevelopment)
{
    var newServices = services.AddSignalR();
    if (!isDevelopment) newServices.AddAzureSignalR(Config.AzureSignalROptions.ConnectionString);

    return newServices;
}

public static IServiceCollection AddSignalRCors(this IServiceCollection services)
{
    services.AddCors(options => options.AddPolicy("CorsPolicy",
        builder =>
        {
            builder
                .AllowAnyHeader()
                .AllowAnyMethod()
                .SetIsOriginAllowed(x => true)
                .AllowCredentials();
        }));

    return services;
}

IApplicationBuilder扩展的一部分,特别是微服务:

public static IApplicationBuilder AddSignalR(this IApplicationBuilder app, bool isDevelopment)
{
    app.UseRouting()
        .UseCors("CorsPolicy");

    if (isDevelopment)
    {
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<UserHub>("/userHub");
            endpoints.MapHub<ConversationHub>("/conversationHub");
            endpoints.MapHub<DiscussionHub>("/discussionHub");
        });
    }
    ....


    return app;
}

如何将Websocket与Ocelot和SignalR一起使用?网关当前能够与SignalR通信的唯一传输方法是长轮询,但对我而言,这并不完全令人满意.预先感谢您的帮助!

How can I use websockets with Ocelot and SignalR? The only transport method which gateway currently is able to communicate with SignalR is long polling, but for me it is not fully satisfactioning. Thank you in advance for any help!

推荐答案

中间件顺序很重要.

if (turnOnWebsockets)
    app.UseWebSockets();

需要在 UseOcelot 调用之前发生.

类似的事情应该对您有用

Something like this should work for you

.Configure(async app =>
{
  app.UseCors(cors =>
  {
    cors.AllowAnyHeader()
        .AllowAnyMethod()
        .SetIsOriginAllowed(x => true)
        .AllowCredentials();
  });

  if (turnOnWebsockets)
    app.UseWebSockets();

  app.UseOcelot().Wait();

注意

到目前为止,ASP.NET Core不支持

AFAIK async Configure .通常不习惯使用 .Wait(),但在这种情况下是必需的,这是

Note

AFAIK async Configure is not supported as of now in ASP.NET Core. Using .Wait() is generally frowned upon but in this case it is needed, and is the way encouraged by the Ocelot documentation as well.

这篇关于Ocelot没有将WebSocket传递给微服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆