RabbitMQ。第2部分。了解交流

Exchange-交换器或交换点。消息发送给它。Exchange 将邮件分发到一个或多个队列中。基于在队列和队列之间创建的连接(消息路由到bindings队列。


Exchange不是一个Erlang进程出于可伸缩性原因exchange,这是mnesia内置数据库中的字符串(指向路由代码所在的代码的模块的链接)一千个交换器将仅消耗1MB的内存。


目录



直接交换


Direct exchange — , . , . . .


:


rabbitmq_4


rabbitmq . direct exchange . , .


Topic Exchange


Topic exchangedirect exchange . , , . 0 ( AZ az 0-9), , * #.


  • *1
  • #0

:


rabbitmq_6


RabbitMQ 2.4.0 topic exchange 145 . trie implementation, . a.b.c, a.*.b.c, a.#.c b.b.c :


例子


, .


:


  • , .. , direct exchange fanout exchange
  • , * , , #.
  • topic exchange direct exchange

Fanout Exchange


Fanout exchange.


:


  • RabbitMQ . exchange;
  • ;

:


rabbitmq_5


Headers Exchange


Headers exchange — (, ) headers . headers Dictionary<, >.


x-match any, (, ). or.


var bindingArguments = new Dictinary<String, Object>();
bindingArguments.add("x-match", "any");

x-match all. , (, ). and.


:


rabbitmq_7_a


:


  • . (, ) headers . , exchange.

Consistent-Hashing Exchange


RabbitMQ.


Consistent-hashing exchange (exchange ) – , , , . ( 0 - n).


– , ( ). .


:


rabbitmq_7


Hash headers . headers, . headers.


, , .


(E2E)


Exchange-to-Exchange ( AMQP. RabbitMQ).


:


rabbitmq_8


E2E , , .


Exchange


RPC . Exchange.Declare, :



exchange RabbitMQ.Client:


//...
channel.ExchangeDeclare(
    exchange: "my_exchange",
    type: "direct",
    durable: "false",
    autoDelete: "false",
    arguments: null
);
//...

  • exchange — , .
  • type
  • durabletrue, exchange . /. false, exchange , /
  • autoDelete — . Exchange ,
  • arguments — . , alternative exchange ( ). , .

rabbitmq_9


exchange , RPC Exchange.DeclareOk. ( Exchange.Declare), Channel.Close OperationInterruptedException, .


. - — RabbitMQ .


Exchange


RabbitMQ guest (username: guest password: guest). , guest . Exchanges Add a new exchange. :


rabbitmq_18


. , Internal, E2E. Producer .



. , .. (). . .


, , fanout exchange, 1:1 . , fanout exchange .


, topic exchange , , direct exchnge, .. topic exchange.


exchange , , .


exchange .


Queues Bindings.


Code


C#, . .


public interface IExchange
{
    /// <summary>
    ///      ,    . 
    ///        .
    /// </summary>
    string Name { get; }

    /// <summary>
    ///      
    /// </summary>
    string Type { get; }

    /// <summary>
    ///       true,  exchange  
    ///      . 
    ///            
    ///       /. 
    ///       false,  exchange  
    ///        , 
    ///      /  
    /// </summary>
    bool IsDurable { get; }

    /// <summary>
    ///      . Exchange  , 
    ///            
    /// </summary>
    bool IsAutoDelete { get; }

    /// <summary>
    ///      
    /// </summary>
    IDictionary<string, object> Arguments { get; }
}

/// <summary>
///      
/// </summary>
public static class ExchangeType
{
    public const string Direct = "direct";
    public const string Topic = "topic";
    public const string Fanout = "fanout";
    public const string Header = "headers";
}

/// <summary>
///     
/// </summary>
public class Exchange : IExchange
{
    public Exchange(
           string name, 
           string type = ExchangeType.Direct, 
           bool durable = true, 
           bool autoDelete = false, 
           IDictionary<string, object> arguments = null)
    {
        Name = name ??
            throw new ArgumentNullException(name, $"{name} must not be null");

        Type = type;
        IsDurable = durable;
        IsAutoDelete = autoDelete;
        Arguments = arguments ?? new Dictionary<string, object>();
    }

    public string Name { get; }
    public string Type { get; }
    public bool IsDurable { get; }
    public bool IsAutoDelete { get; }
    public IDictionary<string, object> Arguments { get; }

    /// <summary>
    ///      Exchange  . 
    ///     Exchange   - direct exchnge  .
    ///         ,   
    ///         
    ///          .
    /// </summary>        
    public static IExchange GetDefault()
    {
        return new Exchange("");
    }
}



All Articles