rebus-org / Rebus

:bus: Simple and lean service bus implementation for .NET
https://mookid.dk/category/rebus
Other
2.31k stars 360 forks source link

Help on how to setup two handlers in two different services, subscribe to the same message without a shared library #792

Closed KorpEduardoSikora closed 5 years ago

KorpEduardoSikora commented 5 years ago

I'm trying to setup my microsservices applications to be indepedent, even of a shared Message library.

I've added a small project to the following repository, describing what I'm trying to accomplish: https://github.com/KorpEduardoSikora/DemoRebusHandlerSameMessage

I guess i'm trying publish a message and subscribe to it's queue by name (the same name as the Type). All the examples I found uses a shared message type library.

Thanks in advance.

mookid8000 commented 5 years ago

Hi @KorpEduardoSikora , great question 😄 it's fairly easy to bind two processes together solely by their knowledge of queue names and/or topics – but as you've probably discovered, Rebus encourages you to share a message DLL between your processes, which might not always be what you want.

It's not hard to remove this dependency though, the crux is just to

To show how that can be done, I just created the SharedNothing demo, where a publisher and a subscriber communicate without sharing anything but their knowledge of a topic name and a message schema.

I hope you find the demo useful 😄 I'll close this issue for now, but please don't hesitate to write any questions/comments you might have in here.

EduSikora commented 5 years ago

Thanks for your help @mookid8000!

We've implemented this after your answer and example (: Thanks!!

Basically we get all MessageTypes on the Handlers, and subscribe to a Endpoint. If the MessageType has an custom Endpoint attribute, we use that value, otherwise we get the string from the MessageType, and adjust to a IFailed<> if needed.

image

On the serialization method we basically do the same thing:

image

Works as a charm (: (for now, on initial tests) hahaha

Now we came across a situation where we need a request/reply... But when we use bus.Send(message, header), we get a error that we did not map the classes to a endpoint..

I didn't quite understand how to implement this Mapping along side the custom subscribe/serialization..

mookid8000 commented 5 years ago

When you

await bus.Send(new MyMessage());

Rebus will look through its "endpoint mappings" for a destination queue for messages of type MyMessage.

An endpoint mapping for MyMessage can be configured like this:

Configure.With(...)
    .(...)
    .Routing(r => r.TypeBased().Map<MyMessage>("some-queue"))
    .Start();

The builder returned from TypeBased() can be used to map multiple specific message types to destination queues, and/or be used to map entire assemblies to destination queues.

mookid8000 commented 5 years ago

I suggest you read the wiki page about routing – it also describes the difference between send and publish.