[英]Google cloud run unable to connect MQTT
Currently I am developing a.Net6 application with a some controllers and a minimal MQTT Server.目前我正在开发带有一些控制器和最小 MQTT 服务器的 .Net6 应用程序。 So far I have everything working correctly on my local machine, however, when deploying to Google Cloud Run (GCR) I am unable to connect to the MQTT Server.
到目前为止,我的本地机器上一切正常,但是,当部署到 Google Cloud Run (GCR) 时,我无法连接到 MQTT 服务器。
I noticed that the GCR container wants you to map incoming traffic to a single port (8080 in my case), however I am running MQTT on port 1883 (default) and unable to connect to it.我注意到 GCR 容器希望您将 map 传入流量发送到单个端口(在我的例子中是 8080),但是我在端口 1883(默认)上运行 MQTT 并且无法连接到它。 The controllers running on port 8080 are accessible and work fine.
在端口 8080 上运行的控制器可以访问并且工作正常。
I need some direction toward a solution, preferably in a single container.我需要一些解决方案的方向,最好是在一个容器中。
Program.cs程序.cs
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseKestrel(o =>
{
o.ListenAnyIP(1883, l => l.UseMqtt());
o.ListenAnyIP(8080);
});
...
var app = builder.Build();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints
.MapConnectionHandler<MqttConnectionHandler>("/mqtt",
httpConnectionDispatcherOptions =>
httpConnectionDispatcherOptions.WebSockets.SubProtocolSelector = protocolList => protocolList.FirstOrDefault() ?? string.Empty);
});
app.UseMqttServer(server => server.StartAsync());
app.MapControllers();
app.Run();
A possible option is to not use 2 ports.一个可能的选择是不使用 2 个端口。
If you use MQTT over WebSockets then your broker can share a port with the HTTP server.如果您在 WebSockets 上使用 MQTT,那么您的代理可以与 HTTP 服务器共享一个端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.