大众运输和Azure服务总线:消息被不同的开发人员空间多次使用

问题描述

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

           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来覆盖主题名称,类似于覆盖队列名称以包含前缀的方式,类似于在this question/answer中进行的操作。

MassTransit包含一个实体名称格式器PrefixEntityNameFormatter,它是在v7中添加的,其作用相同,您只需在总线配置上为消息拓扑指定它即可。

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