简体   繁体   中英

How to trigger to multiple eventhubs in azure webjob or function

I have a multiple Azure Event Hubs located in different Azure Regions. I want to write a webjob/function that can receive messages from all of these Event Hubs without me having to hard code which EHs to listen to.

So, normally you would have something like this, one function that can receive messages from one EH as defined in config:

public void Func([EventHubTrigger("%eventhubInName%")] string data)

In my case I have 6 different EventHubs spread out in 6 azure regions. What I want is to somehow at startup setup my function to listen on several Event Hubs so that I dont need to hard code 6 identical functions (Func1, Func2..) or host my webjob 6 times.

Is this possible somehow? For example by doing something during startup of the webjob?

A single Azure Function can't be linked to multiple triggers, and to my knowledge WebJob SDK can't do that either. Obviously, you could write your own Web Job (not-SDK), but I guess that's not what you are asking for.

The easiest way is probably to write a helper method:

public void Impl(string data) { ... }

and then define 6 identical Functions with different Event Hubs:

public void Func1([EventHubTrigger("%hub1%", Connection = "Conn1")] string data) 
    => Impl(data);

public void Func6([EventHubTrigger("%hub6%", Connection = "Conn6")] string data) 
    => Impl(data);

You can also create 6 function.json files in 6 folders manually, and make them point to the very same Impl function, but with different Event Hub settings.

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