Mass Transit 和 Azure 服务总线:消息被不同的开发空间多次使用

Mass Transit and Azure service bus: message consumed multiple times by different dev spaces

我通过这种方式注册后发布了一条消息(评论行已经做了很多尝试)。 我传递了一个 queuePrefix 以便使用不同的开发空间,每个开发人员一个,因为该应用程序是基于微服务的。 Masstransit 解耦了不同微服务之间的信息交换

           this IServiceBusBusFactoryConfigurator cfg, IServiceProvider provider, string queuePrefix = null,
           Action<IConsumerConfigurator<TConsumer>> configure = null)
           where TConsumer : class, IConsumer<TCommand>
           where TCommand : class
        {

            string queueName = $"{queuePrefix}-{typeof(TCommand).Name}";
            cfg.Message<TCommand>(configureTopology => {
                //configureTopology.SetEntityName(queueName);
                //configureTopology.UsePartitionKeyFormatter(context => context.Message.CustomerId);
            });
            cfg.Publish<TCommand>(configureTopology => {
                configureTopology.EnablePartitioning = true;
                //configureTopology.UsePartitionKeyFormatter(context => context.Message.CustomerId);
            });
            cfg.Send<TCommand>(x =>
            {
                //x.UsePartitionKeyFormatter(context => context..Message.CustomerId);
            });
            cfg.ReceiveEndpoint(queueName, endpointCfg =>
            {                
                endpointCfg.RemoveSubscriptions = true;                
                endpointCfg.EnablePartitioning = true;
                endpointCfg.Consumer<TConsumer>(provider, configure);
            });
            return cfg;
        }

虽然前缀已经足够了,但是如果我发布一条消息,所有的开发空间都会消耗这条消息。 我应该怎么办?我试过订阅但没有成功。看来我走错了路

原因是每种消息类型都有相同的主题名称,并且所有开发空间都订阅了相同的主题。您可以通过创建自己的 IEntityNameFormatter 覆盖主题名称,类似于覆盖队列名称以包含前缀的方式,类似于 .

中的做法

MassTransit 包括一个实体名称格式化程序,PrefixEntityNameFormatter,它是在 v7 中添加的,它做同样的事情,您只需要在消息拓扑的总线配置中指定它。

cfg.MessageTopology.SetEntityNameFormatter(
    new PrefixEntityNameFormatter(cfg.MessageTopology.EntityNameFormatter, "Frank-"));