0

我以这种方式注册后发布了一条消息(评论行做了很多尝试)。我传递了一个 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;
        }

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

4

1 回答 1

1

原因是每个消息类型都有相同的主题名称,并且所有开发空间都订阅了相同的主题。您可以通过创建自己的来覆盖主题名称,类似于覆盖队列名称以包含前缀的方式IEntityNameFormatter,类似于在此问题/答案中的完成方式。

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

cfg.MessageTopology.SetEntityNameFormatter(
    new PrefixEntityNameFormatter(cfg.MessageTopology.EntityNameFormatter, "Frank-")); 
于 2020-10-17T14:51:06.220 回答