RabbitMQ. Partie 2. Comprendre les échanges

Exchange- échangeur ou point d'échange. Des messages lui sont envoyés. Exchange Distribue le message dans une ou plusieurs files d'attente. Il achemine les messages vers la file d'attente en fonction des connexions créées ( bindings) entre celui-ci et la file d'attente.


Exchangepas un processus Erlang . Pour des raisons d'évolutivité exchange, il s'agit d'une chaîne (un lien vers un module avec le code où se trouve la logique de routage) dans la base de données intégrée mnesia . 1 000 échangeurs ne consommeront que 1 Mo de mémoire.


Table des matières



Échange direct


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 :


exemple de tri


, .


:


  • , .. , 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