A preliminary study of rabbitMQ - environment construction, erlang client example tutorial

A preliminary study of rabbitMQ - environment construction, erlang client example tutorial


1. Introduction to rabbitMQ

    Rabbitmq originated from the Advanced Message Queuing Protocol (AMQP, Advanced Message Queuing Protocol, see [ AMQP-wiki ]), AMQP has been designed as an open standard from the beginning to solve numerous message queuing requirements and topology problems. With openness, anyone can implement the standard, and anyone coding for the standard can interact with any MQ server provided by any AMQP vendor. AMQP solves communication problems in a distributed environment, and Erlang is the most suitable language for implementing AMQP proxy servers, because AMQP shows the architecture of each telephone switch, and Erlang is the programming language originally developed by Ericson (Ericsson) for telephone switches. , its distributed programming and robust failure recovery features are particularly suitable for AMQP. Because the core developers of rabbitmq used Erlang to write a prototype to test network latency, and found that the distributed computing library written in Erlang has the same latency as native sockets, so they decided to name it Rabbit: rabbits move very fast and breed crazy, especially It is suitable for the naming of distributed software, and it is also easy to remember. rabbitmq solves the problem of only a small budget and the need for message communication, and solves the problem of application integration and high-intensity transaction processing load. Nowadays, many technology companies and financial companies are using rabbitmq, the official page has quite rich documentation, githubThere are also many examples of rabbitmq above, and the client has various versions, such as lisp, .net, elixir, erlang, go, haskell, java, js, objective-c, perl, php, python, ruby, swift, etc. This article aims to help beginners build a rabbitmq environment, and then run an erlang client example, which is considered an introduction. (I followed the examples of other people on the Internet and the examples of the official website, and I failed to run normally. Even the python code copied from the book "RabbitMQ Practical Combat - Efficient Deployment of Distributed Message Queues" is very tortuous and cannot run normally. Get up. So I will record some of the pits I encountered, hoping to reduce the time for latecomers.)

    Rabbitmq is a subscription-published message queue. The producer generates data and sends it to rabbitmq. The consumer consumes the producer's message according to the subscription.


2. Environment Construction

    My rabbitmq is installed in the linux environment, and the erlang client can run under linux or under windows.

  •  The first step is to download and install rabbitmq, go to the download page of the official website and choose the version that suits you. I chose rabbitmq-server-generic-unix-3.6.12.tar.xz , and then decompress it,
    tar -xvf rabbitmq-server-generic-unix-3.6.12.tar.xz
    After decompression, the rabbitmq_server - 3.6.12 directory will be generated. rabbitmq-server is free of installation. After decompression, it can be used immediately, and it can be run by running the command directly.
    rabbitmq_server-3.6.12/sbin/rabbitmq-server
    If you want to start in the background, you can add the "-detached" parameter
    rabbitmq_server-3.6.12/sbin/rabbitmq-server -detached
    For rabbitmq commands, you can refer to this: RabbitMQ common commands . If the reader feels that it is very troublesome to enter the path every time, you can copy the executable file rabbitmq-server to the /usr/bin directory, so that you can directly enter the rabbitmq-server command. After starting rabbitmq-server, we need to write the client example. Since I am using the erlang example, there should be an erlang runtime environment.
  • The second step is the client's environment, including installing the erlang environment and installing rebar. The installation of the erlang environment is not included in the explanation of this article. Readers can refer to this article to install it by themselves: Install erlang from source code . For the installation of rebar, please refer to this article: Using rebar to build, compile, test, and publish Erlang applications is actually very simple, just a few lines:
    git clone git://github.com/rebar/rebar.git
    cd rebar
    ./bootstrap

    After the above is compiled, an independent erlang script (escript) named "rebar" will be generated in the current directory. Put it in the directory path where you want to create a standard Erlang/OTP project and use it, or put rebar in In the /usr/bin directory, just like the previous rabbitmq-server, you can run the rebar command directly in the shell. The reason why rebar is recommended is because rabar is really easy to use, it is very convenient to build erlang projects, and it can automatically generate code templates such as gen_server. After I used rebar once, I decided that all erlang projects in the future would be built using rebar (as if I could write how many erlang projects I could write ( ⊙ o ⊙ )), so I suggest readers who have never used rebar to take a look The reference article of the previous rebar. Finally, I recommend an erlang editor: IntelliJ IDEA, which is an integrated environment for java language development. IntelliJ is recognized as one of the best java development tools in the industry, but its support for erlang is also very complete, which is better than what I used before. It's much easier to write sublime for erlang. Readers can go to his official website to download the version of the corresponding environment, and then refer to Using IntelliJ IDEA to configure the Erlang development environment to configure the erlang environment. Intellij IDEA can choose the erlang environment, install the rebar plug-in, and also provide a shell to run the erl command directly, which is very convenient. If there is a problem with Chinese input in Intellij IDEA, you can refer to this article: Completely solve the problem that Intellij IDEA 2017.2 cannot input Chinese and cannot switch between Chinese and English freely . It does not matter if this software is not installed, the following examples will use the rebar command to complete the compilation.

3. hello world example

    After the environment is configured, we can start writing hello world examples. The test environment on my side is on linux, and both the client and server of rabbitmq are done on it (the client can be done with Intellij IDEA + rebar under windows). As mentioned earlier, running sbin/rabbitmq-server -detached directly will start the server. The rest of the work is the work of the erlang client.

  • First, create a new "my_rabbit_test" folder and copy the executable file rebar generated earlier into it.
  • We use rebar to build the skeleton of the project and execute the command:
    ./rebar create-app appid=myapp

    The src folder can be generated, myapp.app.src in the src folder is the resource file of the OTP application, myapp_app.erl is the Application behaviour of the OTP application, and myapp_sup.erl is the Supervisor behaviour of the OTP application.

  • Next, continue to execute ./rebar compile, you can see that the ebin directory is generated, and the myapp.app file in ebin specifies that the starting module is myapp_app. These are also specified by "myapp.app.src" in the src directory. If we enter the ebin directory, and then execute erl to enter the erl shell interface, enter application:start(myapp) to start the myapp_app module. So all we have to do is plug code into myapp_sup.erl and myapp_app.erl.
  • We add a simple test code to myapp_app.erl
  • -module(myapp_app).
    
    -behaviour(application).
    -include_lib("amqp_client/include/amqp_client.hrl").
    
    %% Application callbacks
    -export([start/2, stop/1]).
    -export([test/0]).
    
    %% ===================================================================
    %% Application callbacks
    %% ===================================================================
    
    start(_StartType, _StartArgs) ->
        myapp_sup:start_link().
    
    stop(_State) ->
        ok.
    
    
    test() ->
        %% Start a network connection
        {ok, Connection} =
        amqp_connection:start(
            #amqp_params_network{
                host = "192.168.3.142",
                username = <<"test_user">>,
                password = <<"123456">>,
                virtual_host = <<"mytest">>
            }),
        io:format("connection"),
        %% Open a channel on the connection
        {ok, Channel} = amqp_connection:open_channel(Connection),
        %% Declare a queue
        #'queue.declare_ok'{queue = Q}
        = amqp_channel:call(Channel, #'queue.declare'{}),
        %% Publish a message
        Payload = <<"foobar">>,
        Publish = #'basic.publish'{exchange = <<>>, routing_key = Q},
        amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}),
        %% Get the message back from the queue
        Get = #'basic.get'{queue = Q},
        {#'basic.get_ok'{delivery_tag = Tag}, Content}
        = amqp_channel:call(Channel, Get),
        #amqp_msg{payload = Body} = Content,
        io:format("Body:~p~n",[Body]),
        %% Do something with the message payload
        %% (some work here)
    
        %% Ack the message
        amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}),
    
        %% Close the channel
        amqp_channel:close(Channel),
        %% Close the connection
        amqp_connection:close(Connection),
    
        ok.

    It can be seen from the code that amqp_client is used, so we should download rabbitmq's client dependency for erlang. We will talk about this below. Here we pay attention to the amqp_connection:start() function. Its parameter is a structure. Among the variables in the body, username, password, and virtual_host are all in binary format. It should be noted that I wrote it in string format before, but I did not convert it into binary format, and always reported an error . In addition, since these values ​​in the code are all customized by me, I first need to create these users and vhosts on rabbitmq-server, that is, on my linux virtual machine 192.168.3.142 (that is, my rabbitmq-server server) , execute the following command:

  • #新增一个vhost
    sbin/rabbitmqctl add_vhost mytest
    #新增一个user
    sbin/rabbitmqctl add_user test_user 123456
    #为用户设置vhost上面的权限
    sbin/rabbitmqctl set_permissions -p mytest test_user ".*" ".*" ".*"

  • Since the code depends on the rabbitmq client, we need to download this dependency. We first create a new libs directory in the code directory, and then download the two dependencies amqp_client and rabbit_common that need to be downloaded to this directory, and then unzip
  • unzip ./amqp_client-3.6.12.ez 
    unzip ./rabbit_common-3.6.12.ez

    After decompression, you can see two directories, both of which have ebin directories, which are already compiled codes and can be used directly. Create a new rebar.config file in the code directory and add the following code to it:

  • {lib_dirs, ["libs"]}.
  • Then we can execute the ./rebar compile command in the code directory to compile, the compiled beam file is in the ebin directory, enter the ebin directory, and execute the erl -pa command to start the test
  • ./rebar compile
    cd ebin/
    erl -pa ../libs/amqp_client-3.6.12/ebin/ -pa ../libs/rabbit_common-3.6.12/ebin/

    The -pa option must be added here. The option specifies the ebin directory of the client that loads rabbitmq, otherwise it will not run. I checked a lot of information and found that no one said this, maybe this is a normal thing for erlang programmers, but for a novice like me, it really took a lot of time to explore.

    You can see the effect, this "test" example has been successfully run. If you use Intellij IDEA + rebar under Windows, after compiling, you also need to add the -pa option when running the shell with the erl command, and add the dependent ebin directory.
    In addition, there is another way, that is, executing the ./rebar shell command can also achieve the effect of -pa (taught by a colleague, an old erlang driver).
     

    ./rebar shell

    Written here, an example of a rabbitmq erlang client has been written. In fact, this article has spent a lot of effort on the environment construction, and the real code is actually very short. If you don't want to build so many environments and want to quickly experience the installation and examples of rabbitmq, you can refer to this article: rabbitmq install and run erlang examples in rabbitmq-tutorials . The next blog will explain the use of rabbitmq, and then introduce the use of rebar to generate a gen_server template to generate a service for easy invocation. My knowledge and writing skills are limited, and I would like to point out the inappropriateness. Thank you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325449831&siteId=291194637