Can we use RabbitMQ and Mediatr together using masstransit?

Viewed 7179

I created a microservice application that microservices using MassTransit and RabbitMQ for communication.
Each microservice developed using clean architecture, so we have MediatR inside each microservice.
Is it possible to use MassTransit for inside communication as well? so I can use the same signature for all services and when I want to expose a service to be used inter-microservice, it will be doable with ease. So MediatR used for intra-communication and RabbitMQ used for inter-communication, and whole universe is on MassTransit system.
[Update] My question is how we can configure consumers so some can be used for inside communication (via MediatR) and some can be used for external communication (via RabbitMQ) and easily change them from inside to outside.
[Update2] for example here is my MassTransit registration:

services.AddMassTransit(x =>
        {
            x.AddConsumers(Assembly.GetExecutingAssembly());

            x.AddBus(provider =>
                Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    cfg.Host(new Uri(config.RabbitMQ.Address), h =>
                    {
                        h.Username(config.RabbitMQ.Username);
                        h.Password(config.RabbitMQ.Password);
                    });

                    cfg.ReceiveEndpoint("my-queue", ep => { ep.ConfigureConsumers(provider); });
                }));


            x.AddMediator((provider, cfg) => { cfg.ConfigureConsumers(provider); });
        });

How can I differ in internal communication and external communication? in other words, how can I register some consumers to MediatR and some to RabbitMQ?

2 Answers

They can be used together, and MassTransit has its own Mediator implementation as well so you can write your handlers once and use them either via the mediator or via a durable transport such as RabbitMQ.

There are videos available that take you through the capabilities, starting with mediator and moving to RabbitMQ.

I found that I should create a separate bus for each. then external services inherit from an interface like IExternalConsumer, so I can separate them form internal ones and add them to related bus: UPDATED for version 7

        // find consumers
        var types = AssemblyTypeCache.FindTypes(new[]{Assembly.GetExecutingAssembly()},TypeMetadataCache.IsConsumerOrDefinition).GetAwaiter().GetResult();
        var consumers = types.FindTypes(TypeClassification.Concrete | TypeClassification.Closed).ToArray();
        var internals = new List<Type>();
        var externals = new List<Type>();
        foreach (Type type in consumers)
        {
            if (type.HasInterface<IExternalConsumer>())
                externals.Add(type);
            else
                internals.Add(type);
        }

        services.AddMediator(x =>
        {
            x.AddConsumers(internals.ToArray());
            x.ConfigureMediator((provider, cfg) => cfg.UseFluentValidation());
        });
        services.AddMassTransit<IExternalBus>(x =>
        {
            x.AddConsumers(externals.ToArray());
            x.AddBus(provider =>
                Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    cfg.Host(new Uri(config.RabbitMQ.Address), h =>
                    {
                        h.Username(config.RabbitMQ.Username);
                        h.Password(config.RabbitMQ.Password);
                    });

                    cfg.ReceiveEndpoint(apiProviderName, ep => { ep.ConfigureConsumers(provider); });

                }));
        });

        services.AddMassTransitHostedService();
Related