zoukankan      html  css  js  c++  java
  • Fedora RabbitMQ 部署笔记

    1 安装erlang

    sudo dnf install erlang

    2 安装RabbitMQ

    sudo dnf install rabbitmq-server

    3 启动 RabbitMQ 服务

    service rabbitmq-server start

    4 开放5672端口

    firewall-cmd --zone=public --add-port=5672/tcp --permanent
    firewall-cmd --reload

    5 开启管理UI

    sudo rabbitmq-plugins enable rabbitmq_management
    firewall-cmd --zone=public --add-port=15672/tcp --permanent
    firewall-cmd --reload

    RabbitMQ安装完成接下来我们使用.net core写一个测试程序。

    新建项目

    dotnet new console

    切到项目目录下安装 RabbitMQ.Client

    dotnet add package RabbitMQ.Client

    发送端代码

    using System;
    using RabbitMQ.Client;
    using System.Text;
    
    class Send
    {
        public static void Main()
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using(var connection = factory.CreateConnection())
            using(var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "hello",
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);
    
                string message = "Hello World!";
                var body = Encoding.UTF8.GetBytes(message);
    
                channel.BasicPublish(exchange: "",
                                     routingKey: "hello",
                                     basicProperties: null,
                                     body: body);
                Console.WriteLine(" [x] Sent {0}", message);
            }
    
            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }
    }

    接收端代码

    using RabbitMQ.Client;
    using RabbitMQ.Client.Events;
    using System;
    using System.Text;
    
    class Receive
    {
        public static void Main()
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using(var connection = factory.CreateConnection())
            using(var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "hello",
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);
    
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] Received {0}", message);
                };
                channel.BasicConsume(queue: "hello",
                                     noAck: true,
                                     consumer: consumer);
    
                Console.WriteLine(" Press [enter] to exit.");
                Console.ReadLine();
            }
        }
    }

    参考资料:

    在CentOS7上安装RabbitMQ

    https://www.cnblogs.com/uptothesky/p/6094357.html

    RabbitMQ tutorial - "Hello World!" — RabbitMQ

    http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

    RabbitMQ 官网

    http://www.rabbitmq.com

  • 相关阅读:
    fast incremental backup failed on standby database
    How to find error message from OMS repository
    Examine 11g automatic block Corruption recovery
    C#继承Control实用自定义控件
    手把手教你写SHELL CODE
    编写C#控件的3种方式
    Android中跨越ACTIVITY的全局线程
    DevExpress 皮肤使用方法
    PHP讨论之什么是HOOK?
    C#制作WinForm控件
  • 原文地址:https://www.cnblogs.com/maruko/p/10219383.html
Copyright © 2011-2022 走看看