RabbitMQ. Bagian 2. Memahami Pertukaran

Exchange- Penukar atau titik pertukaran. Pesan dikirim ke sana. Exchange Sebarkan pesan ke dalam satu atau beberapa antrian. Ini merutekan pesan ke antrian berdasarkan koneksi yang dibuat ( bindings) antara itu dan antrian.


Exchangebukan proses Erlang . Untuk alasan skalabilitas exchange, ini adalah string (tautan ke modul dengan kode tempat logika perutean terletak) di basis data mnesia bawaan . 1.000 penukar hanya akan menggunakan memori 1MB.


Daftar Isi



Pertukaran langsung


Direct exchange โ€” , . , . โ€” . .


:


rabbitmq_4


rabbitmq . direct exchange . , .


Topic Exchange


Topic exchange โ€“ direct 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 :


trie-contoh


, .


:


  • , .. , 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 โ€”
  • durable โ€” true, 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