我们如何强制MassTransit使用HTTPS而不是AMQP连接到Azure Service Bus?
我们的应用程序位于企业防火墙后面。它需要从Azure主题发送/接收消息。我们宁愿在AMQP上使用HTTPS,以便更轻松地用于安全管理。
该应用具有以下设置
根据Azure Service Bus文档,我们应该能够将传输类型设置为AmqpWebSockets,该传输类型应使用HTTPS进行通信。连接字符串如下所示:
Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=secret;TransportType=AmqpWebSockets
(ref:https://github.com/MicrosoftDocs/azure-docs/issues/14133)
如果使用TopicClient
中的Microsoft.Azure.ServiceBus 4.1.1
,我们可以看到流量通过端口443。示例代码为
class Program
{
public static async Task Main(string[] args)
{
var serviceBus = new ServiceBus();
await serviceBus.SendMessageAsync();
Console.ReadKey();
}
}
public class ServiceBus
{
public async Task SendMessageAsync()
{
const int numberOfMessages = 10;
topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
// Send messages.
await SendMessagesAsync(numberOfMessages);
Console.ReadKey();
await topicClient.CloseAsync();
}
public async Task SendMessagesAsync(int numberOfMessagesToSend)
{
try
{
for (var i = 0; i < numberOfMessagesToSend; i++)
{
// Create a new message to send to the topic.
string messageBody = $"Message {i}";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
// Write the body of the message to the console.
Console.WriteLine($"Sending message: {messageBody}");
// Send the message to the topic.
await topicClient.SendAsync(message);
}
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
}
}
}
但是,当我们使用具有TransportType=AmqpWebSockets
的相同连接字符串时,MassTransit仍然使用AMQP端口5671
:
public class Order : IModel
{
public int Id { get; set; }
public double Amount { get; set; }
public string Description { get; set; }
}
public static async Task Main(string[] args)
{
var bus = Bus.Factory.CreateUsingAzureServiceBus(cfg =>
{
cfg.Host(ConnectionString);
});
await bus.StartAsync();
for (int i = 0; i < 100; i++)
{
var order = new Order() { Id = i, Amount = 20, Description = "Test order" };
await bus.Publish(order);
}
}
有趣的是它使用端口443
来处理前几个数据包,然后更改为端口5671