简体   繁体   中英

Azure Service Bus pub/sub to multiple azure web app instances

I am trying to achieve a pub/sub with azure service bus. I started with this tutorial and it worked so far: https://azure.microsoft.com/en-gb/documentation/articles/service-bus-dotnet-how-to-use-topics-subscriptions/

But in my dedicated case i am not exactly sure how i should do it exactly:

I have a web api running on azure web app that is scaled to three instances. I have a console application client that triggers some messages to a dedicated topic.

What i want to achieve is, that all three instances of the web api get the messages delivered that is send to the message bus. So it is a fire forget action:

  • Client sends message to topic
  • Every subscriber that is CURRENTLY subscribing to this topic should get the message

I am not interested in older messages that were sent when the subscriber was inactive/offline. I am just syncing an in memory cache over these instances so it is really a short living info when i need to know which keys i have to invalidate. but it is important that every subscriber gets the information to avoid stale data.

I am not exactly sure if i have to create a subscription dynamically in the startup code of the web api so that every instance has its own subscription or if i can subscribe all web app instances to the same subscription? I would like not to dynamically create subscriptions since i don't know when to remove them again (eg scaled down to 2 instances instead of three). but i was unable to find any documentation how to do this and if it is okey that multiple clients subscribe to the same subscription or if i need to create a subscription per client.

I am not exactly sure if i have to create a subscription dynamically in the startup code of the web api so that every instance has its own subscription or if i can subscribe all web app instances to the same subscription?

Service Bus subscribers adopt the Competing Consumer pattern by default. You must create a unique subscription for each Web API instance in order each instance to receive a copy of the message. It will be easiest to do this when the Web API instance starts up.

I would like not to dynamically create subscriptions since i don't know when to remove them again (eg scaled down to 2 instances instead of three).

You can configure the subscription to be auto-deleted after being idle for some period of time. "Idle" in this case would mean that the Web API instance has spun down and is no longer attempting to receive messages on the subscription. When creating the subscription set the AutoDeleteOnIdle time span for a brief duration, currently a minimum of 5 minutes. Now you can create a new subscription when the Web API instance starts and know that it will be automatically deleted soon after the Web API instance stops.

I am not interested in older messages that were sent when the subscriber was inactive/offline.

When creating the topic , set the DefaultMessageTimeToLive for a brief duration eg 5 seconds. This will ensure that new subscribers don't see old messages.

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