RabbitMQ四種Exchange類型之Direct (Erlang)

我的個人博客網站雲諾說上線啦!所有文章都搬到新地址了,點擊圍觀吧!

Direct類型的Exchanges是處理路由鍵的,需要將一個隊列綁定到交換機上,要求該消息與一個特定的路由鍵完全匹配。這是一個完整的匹配。如果一個隊列綁定到該交換機上要求路由鍵爲 “logs”,則只有路由鍵爲“logs”的消息才被轉發,不會轉發路由鍵爲"logs.error",只會轉發路由鍵爲"logs"。 

如下圖:

下面是代碼實現!!!

消費者:

-module(mod_direct_receive).

-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-export([start_link/1]).
-include("common.hrl").

-record(state, {routing_key = <<"">>}).


start_link(RoutingKey) ->
	Server1 = lists:concat([?MODULE,erlang:binary_to_list(RoutingKey)]),
	Server2 = erlang:list_to_atom(Server1),
	gen_server:start_link({local,Server2}, ?MODULE, [RoutingKey], []).


init([RoutingKey]) ->
	start(RoutingKey),
    {ok, #state{routing_key=RoutingKey}}.

handle_call(_Request, _From, State) ->
    Reply = ok,
    {reply, Reply, State}.


handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info({'basic.consume_ok',_}, State) ->
	{noreply, State};
handle_info({#'basic.deliver'{},#amqp_msg{payload=Msg}}, State) ->
	io:format(" [routing_key = ~p] receive  messages is ~p~n",[State#state.routing_key,Msg]),
	{noreply, State};

handle_info(Info, State) ->
	io:format("[routing_key = ~p] unknown messages is ~p~n", [State#state.routing_key,Info]),
    {noreply, State}.


terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

start(RoutingKey) ->
	Params = #amqp_params_network{host=?HOST,username=?USER_NAME,password=?PASSWORD},
	case amqp_connection:start(Params) of
		{ok,ConnectionPid} ->
			{ok, Channel} = amqp_connection:open_channel(ConnectionPid),
			%%生成隊列名稱
			Queue = lists:concat([fanout_queue,now_time()]),
			QueueName = erlang:list_to_binary(Queue),
			%%聲明隊列
			amqp_channel:call(Channel, #'queue.declare'{queue = QueueName,auto_delete=true}),
			%%聲明exchange
			amqp_channel:call(Channel, #'exchange.declare'{ auto_delete =true,exchange = <<"direct">>, type = ?EXCHANGE_TYPE_DIRECT}),
			%%隊列綁定到exchange
			amqp_channel:call(Channel, #'queue.bind'{queue = QueueName, exchange = <<"direct">>,routing_key = RoutingKey}),
			io:format(" [routing_key = ~p] Waiting for messages......~n",[RoutingKey]),
			amqp_channel:subscribe(Channel, #'basic.consume'{queue = QueueName,no_ack = true}, self());
		{error,Resaon} ->
			io:format("[routing_key = ~p] connection rabbit error: ~p~n", [RoutingKey,Resaon]),
			Resaon
	end.

now_time()->
	{A, B, _} = os:timestamp(),
    A * 1000000 + B.

生產者:

-module(mod_direct_send).
-export([send/2]).
-include("common.hrl").
send(Msg,RoutingKey) ->
	Params = #amqp_params_network{host=?HOST,username=?USER_NAME,password=?PASSWORD},
	case amqp_connection:start(Params) of
		{ok,ConnectionPid} ->
			{ok, Channel} = amqp_connection:open_channel(ConnectionPid),
			amqp_channel:cast(Channel,
							  #'basic.publish'{routing_key = RoutingKey, 
											   exchange = <<"direct">>},
							  #amqp_msg{payload = Msg}),
			io:format("Sent '~p'~n",[Msg]),
			amqp_channel:close(Channel),
			amqp_connection:close(ConnectionPid);
		{error,Reason} ->Reason
	end.

common頭文件:

-include("amqp_client_internal.hrl").

-define(USER_NAME 	, <<"test">>).
-define(PASSWORD 	, <<"test">>).
-define(HOST	 	, "192.168.249.128").
-define(PORT	 	, 5672).


%%exchanges type
-define(EXCHANGE_TYPE_FANOUT	, <<"fanout">> ).
-define(EXCHANGE_TYPE_DIRECT	, <<"direct">> ).

運行結果:



祝生活愉快!!!

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章