Search code examples
rabbitmqerlang

Erlang Direct (Erlang Distribution) Client using #amqp_params_direct{} ?


Here I can connect the rabbit node directly:

(app@centos7-mq)1> net_kernel:connect_node('rabbit@centos7-mq').
true
(app@centos7-mq)2> nodes().
['rabbit@centos7-mq']

But it show error when using program.

Erlang RabbitMQ Client library

    RabbitParams=#amqp_params_direct{username=?RABBIT_USERNAME,
                      password=?RABBIT_PASSWORD, virtual_host=?VHOST, node='rabbit@centos7-mq'},
    Connection = amqp_connection:start(RabbitParams),
    io:format("amqp_connection:start result: ~p~n", [Connection]).

The error reads:

amqp_connection:start result: {error,{nodedown,'rabbit@centos7-mq'}}

So, what's wrong with it?

============ This is the example

-module(amqp_direct).
-include_lib("amqp_client/include/amqp_client.hrl").
-compile([export_all]).
-compile(nowarn_export_all).
-define(RABBIT_USERNAME, <<"your user">>).
-define(RABBIT_PASSWORD, <<"your password">>).
-define(VHOST, <<"your vhost">>).
-define(NODE, 'your server node').


connect_amqp() ->
    RabbitParams=#amqp_params_direct{username=?RABBIT_USERNAME,
                      password=?RABBIT_PASSWORD, virtual_host=?VHOST, node=?NODE},
    io:format("amqp_connection:start begin ~n"),
    Connection = amqp_connection:start(RabbitParams),
    io:format("amqp_connection:start result: ~p~n", [Connection]),
    Connection.


start() ->
   Connection=amqp_example:connect_amqp(),
   "Finish".


Solution

  • I try to using the option --sname test, then It works.

    rebar3 shell --sname test
    ===> Verifying dependencies...
    .....
    
    (test@centos7-mq)1> amqp_direct:start().
    amqp_connection:start begin
    =WARNING REPORT==== 21-Jan-2023::00:37:40.121828 ===
    AMQP 0-9-1 client call timeout was 70000 ms, is updated to a safe effective value of 130000 ms
    amqp_connection:start result: {ok,<0.310.0>}
    "Finish"
    

    I read the source code file amqp_client/src/amqp_connection.erl, and it make me have such try.

    connect(Params = #amqp_params_direct{username     = Username,
                                         password     = Password,
                                         node         = Node,
                                         adapter_info = Info,
                                         virtual_host = VHost},
            SIF, _TypeSup, State) ->
    ....
        case rpc:call(Node, rabbit_direct, connect,
                      [{Username, DecryptedPassword}, VHost, ?PROTOCOL, self(),
                       connection_info(State1)], ?DIRECT_OPERATION_TIMEOUT) of