简体   繁体   English

使用 Azure 服务总线发布/订阅

[英]Publish/subscribe with Azure Service Bus

I have a Microservice (Web API), that publish messages to a Topic (Topic A).我有一个微服务(Web API),它将消息发布到一个主题(主题 A)。

Now I have another Microservice (Web API) that should subscribe to this topic and act upon the messages.现在我有另一个微服务(Web API)应该订阅这个主题并对消息采取行动。

My question is simply: How should I do this since my Microservice that should subscribe to the Topic is a WebApi?我的问题很简单:我应该怎么做,因为我应该订阅 Topic 的微服务是一个 WebApi? In my web api, I want in somehow instantly know when a new message is available in the Topic.在我的 web api 中,我想以某种方式立即知道主题中何时有新消息可用。 Should I poll the service bus via an endpoint?我应该通过端点轮询服务总线吗?

I'm uncertain about the best practices about this.我不确定这方面的最佳实践。

All examples that I have seen using console applications to subscribe.我见过的所有使用控制台应用程序订阅的示例。 But that's not my case since I have an web api.但这不是我的情况,因为我有一个 web api。

There are different ways of doing this.有不同的方法可以做到这一点。

1. Using Azure Functions 1. 使用Azure 函数

This way you create two applications.这样您就可以创建两个应用程序。 Your standard web api, and separately you create an Azure Function that will handle the messages from the queue.您的标准 Web api,并单独创建一个 Azure 函数来处理来自队列的消息。 There multiple benefits of this approach, on of them is that you are isolating the code handling the queue, so if you have many messages, it will not affect the performance of your API这种方法有很多好处,其中之一就是你隔离了处理队列的代码,所以如果你有很多消息,它不会影响你的 API 的性能

2. Using a Singleton service inside your web application 2.在 Web 应用程序中使用Singleton 服务

The idea here is that your API application is handling queue messages in the background.这里的想法是您的 API 应用程序在后台处理队列消息。 This has the advantage that you have only one application doing everything, simpler to maintain for example.这样做的好处是您只有一个应用程序可以完成所有工作,例如易于维护。 It has the disadvantage that a very big inflow of messages will slow down your APIs.它的缺点是大量的消息流入会减慢您的 API。

(Note, in the link above look for Consuming messaging from the Queue (注意,在上面的链接中查找从队列中使用消息

Whether is a WebAPI or a console , it is the responsibility of the consumer to communicates and collect records.无论是WebAPI还是console ,沟通和收集记录都是消费者的责任。 Being a WebAPI doesn't mean that it should only have public endpoints.作为一个 WebAPI 并不意味着它应该只有公共端点。 Typical WebAPI, might have public endpoint (for external world) or can have private endpoints (for internal communications) or can have a combination of both.典型的 WebAPI,可能具有公共端点(用于外部世界)或可以具有私有端点(用于内部通信)或可以具有两者的组合。 The responsibility of private endpoints could be reading data from service data-store, consuming external services via adapter services etc. In your case, upon initialization of your WebAPI, you might want to create a consumer object and start reading data and process as you want.私有端点的职责可能是从服务数据存储中读取数据,通过适配器服务使用外部服务等。在您的情况下,在您的 WebAPI 初始化时,您可能希望创建一个使用者对象并开始读取数据并根据需要进行处理. Hope this help.希望这有帮助。

You can poll in a Web Job or background task.您可以在 Web 作业或后台任务中进行轮询。 But the built-in way to do this is with an Azure Function triggered from the Topic, or with Azure Event Grid .但内置的方法是使用从主题触发的Azure 函数,或使用Azure 事件网格

For listening in the background you can use IHostedService .要在后台收听,您可以使用IHostedService Inside Method StartAsync内部方法StartAsync

you can register message processors;您可以注册消息处理器;

  queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions)

And on StopAsync you can stop processing messages and close the client.StopAsync您可以停止处理消息并关闭客户端。

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

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