`
sillycat
  • 浏览: 2552129 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

RabbitMQ(9)Have Working Queue on MAC with Scala in Cluster Mode

 
阅读更多

RabbitMQ(9)Have Working Queue on MAC with Scala in Cluster Mode

1. Install on my MAC laptop.
>su root
>port selfupdate
>sudo port install erlang +ssl

Download the source from URL
http://www.rabbitmq.com/releases/rabbitmq-server/v3.1.5/rabbitmq-server-generic-unix-3.1.5.tar.gz

>tar zxvf rabbitmq-server-generic-unix-3.1.5.tar.gz
>mv rabbitmq_server-3.1.5 /Users/carl/tool/
>sudo ln -s /Users/carl/tool/rabbitmq_server-3.1.5 /opt/rabbitmq-3.1.5
>sudo ln -s /opt/rabbitmq-3.1.5 /opt/rabbitmq

Start the Server
>sudo sbin/rabbitmq-server

Check and Stop the Server
>sudo sbin/rabbitmqctl status
>sudo sbin/rabbitmqctl stop

>sudo vi ~/.profile
export PATH=/opt/rabbitmq/sbin:$PATH
>. ~/.profile 

2. Example of Producer and Consumer
The sample project is in super duty .

Here is how we build the connection
package com.sillycat.superduty.service

import com.rabbitmq.client.ConnectionFactory
import com.rabbitmq.client.QueueingConsumer

object RabbitMQConnectionService {

  val TASK_QUEUE_NAME = "task_queue"

  val SERVER_HOST = "localhost"

  def connect = {
    val factory = new ConnectionFactory
    factory.setHost(SERVER_HOST)

    val conn = factory.newConnection()
    val channel = conn.createChannel()

    val durable = true
    channel.queueDeclare(TASK_QUEUE_NAME, durable, false, false, null)

    val prefetchCount = 1
    channel.basicQos(prefetchCount)

    val consumer = new QueueingConsumer(channel)
    val autoAck = false

    channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer)

    (conn, channel, consumer)
  }
}

Here is the Producer
package com.sillycat.superduty.jobs.producer

import com.sillycat.superduty.service.RabbitMQConnectionService
import com.rabbitmq.client.MessageProperties

object NewTaskRabbitMQ extends App {

  val (conn, channel, consumer) = RabbitMQConnectionService.connect

  val message = "campaign"

  Range(1, 10, 1).foreach { num =>
    channel.basicPublish("", RabbitMQConnectionService.TASK_QUEUE_NAME,
      MessageProperties.PERSISTENT_TEXT_PLAIN, (message + num).getBytes());
  }
  channel.close
  conn.close
}

Here is the Consumer

package com.sillycat.superduty.jobs.consumer

import com.sillycat.superduty.service.RabbitMQConnectionService
import com.rabbitmq.client.QueueingConsumer
import com.typesafe.scalalogging.slf4j.Logging

object WorkerRabbitMQ extends App with Logging {
  val (conn, channel, consumer) = RabbitMQConnectionService.connect

  while (true) {
    val delivery: QueueingConsumer.Delivery = consumer.nextDelivery()

    val message = new String(delivery.getBody())
    val deliverTag = delivery.getEnvelope.getDeliveryTag

    logger.debug("Worker get task=" + message + " delivery tag=" + deliverTag)
    channel.basicAck(deliverTag, false)
  }
}

Here is how we execute the command
>sudo sbin/rabbitmq-server


>sbt 'run-main com.sillycat.superduty.jobs.consumer.WorkerRabbitMQ'
>sbt 'run-main com.sillycat.superduty.jobs.consumer.WorkerRabbitMQ'


>sbt 'run-main com.sillycat.superduty.jobs.producer.NewTaskRabbitMQ'

And the build.sbt should add these dependencies.
        "com.rabbitmq"        %   "amqp-client"               % "3.1.4" 

3. Cluster Configuration
Should always follow this document
http://www.rabbitmq.com/clustering.html

>chmod 777 ~/.erlang.cookie
change the content of the file if you need
>chmod 400 ~/.erlang.cookie
>sudo chown -R carl ~/.erlang.cookie 
>sudo chgrp -R staff ~/.erlang.cookie

Enable the web UI management
>rabbitmq-plugins enable rabbitmq_management 

Start cluster on single machine
>RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit1 sbin/rabbitmq-server -detached
>RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}]" RABBITMQ_NODENAME=rabbit2 sbin/rabbitmq-server -detached

Check status of node1
>rabbitmqctl -n rabbit1 cluster_status
Cluster status of node rabbit1@sparkworker1 ...
[{nodes,[{disc,[rabbit1@sparkworker1]}]},
 {running_nodes,[rabbit1@sparkworker1]},
 {partitions,[]}]
…done.
 
>rabbitmqctl -n rabbit2 cluster_status 
Cluster status of node rabbit2@sparkworker1 ...
[{nodes,[{disc,[rabbit2@sparkworker1]}]},
 {running_nodes,[rabbit2@sparkworker1]},
 {partitions,[]}]
...done.

Stop the second node and make it join the first cluster
>rabbitmqctl -n rabbit2 stop_app
>rabbitmqctl -n rabbit2 join_cluster rabbit1@sparkworker1
>rabbitmqctl -n rabbit2 start_app

Check the status again
>rabbitmqctl -n rabbit1 cluster_status
Cluster status of node rabbit1@sparkworker1 ...
[{nodes,[{disc,[rabbit1@sparkworker1,rabbit2@sparkworker1]}]},
 {running_nodes,[rabbit2@sparkworker1,rabbit1@sparkworker1]},
 {partitions,[]}]
...done.

Visit the page
http://localhost:15672/

The default username and password should be guest/guest.

Changing the node Type
>rabbitmqctl -n rabbit2 stop_app
>rabbitmqctl -n rabbit2 change_cluster_node_type ram
>rabbitmqctl -n rabbit2 start_app

or 
>rabbitmqctl -n rabbit1 stop_app
>rabbitmqctl -n rabbit1 change_cluster_node_type disc
>rabbitmqctl -n rabbit1 start_app

The client will be something like this if connect to cluster
    val factory = new ConnectionFactory
    //factory.setHost(SERVER_HOST)
    //val conn = factory.newConnection()

    val addrArr = Array(new Address("localhost", 5672), new Address("", 5673))
    val conn = factory.newConnection(addrArr)

The source codes here is like this:
for (Address addr : addrs) {
            try {
                FrameHandler frameHandler = createFrameHandler(addr);
                AMQConnection conn =
                    new AMQConnection(username,
                                      password,
                                      frameHandler,
                                      executor,
                                      virtualHost,
                                      getClientProperties(),
                                      requestedFrameMax,
                                      requestedChannelMax,
                                      requestedHeartbeat,
                                      saslConfig);
                conn.start();
                return conn;
            } catch (IOException e) {
                lastException = e;
            }
        }

So, there is no load balance and we will try to get connection one by one till we get one, it is not good. We can consider put ha-proxy or nginx before that.

Break a Cluster
>rabbitmqctl -n rabbit2 stop_app
>rabbitmqctl -n rabbit2 reset 


References:
RabbitMQ 1 ~ 8
http://sillycat.iteye.com/blog/1565771
http://sillycat.iteye.com/blog/1567052
http://sillycat.iteye.com/blog/1575002
http://sillycat.iteye.com/blog/1575314
http://sillycat.iteye.com/blog/1575816
http://sillycat.iteye.com/blog/1578635
http://sillycat.iteye.com/blog/1579464
http://sillycat.iteye.com/blog/1582971
https://github.com/koeninger/rabbit-example

http://www.rabbitmq.com/install-macports.html
http://www.rabbitmq.com/getstarted.html

Message Working Queue
http://sillycat.iteye.com/blog/1575314
https://github.com/koeninger/rabbit-example

Cluster
http://www.rabbitmq.com/clustering.html
http://www.rabbitmq.com/partitions.html
http://www.rabbitmq.com/nettick.html

management page
http://www.rabbitmq.com/management.html

All the Client
http://www.rabbitmq.com/devtools.html

Tips 
http://fengchangjian.com/?p=1896

分享到:
评论

相关推荐

    rabbitmq-dump-queue:将消息从 RabbitMQ 队列转储到文件,而不影响队列

    rabbitmq-转储队列 将消息从 RabbitMQ 队列转储到文件,而不影响队列。 安装 下载一个版本 可以在页面上找到预编译的二进制包。 从源代码编译 如果您安装了 ,则可以通过运行以下命令从源代码安装 rabbitmq-dump-...

    RabbitMQ_Backing_Queue结构1

    RabbitMQ 的 Backing Queue 结构 RabbitMQ 的 Backing Queue 结構是一個复杂的消息队列系统,分为 Exchange 和 MQ 两部分。Exchange 类似于路由器,根据自身类型和 binding 信息决定消息的去向,而 MQ 负责暂时存储...

    RabbitMQ Work Queue实例

    RabbitMQ作为一款广泛使用的开源消息代理,是实现工作队列(Work Queue)模式的理想选择。本文将深入探讨RabbitMQ工作队列的实现,包括消息确认机制、消息持久化以及公平调度等关键知识点。 首先,让我们理解工作...

    RabbitMQ in Depth.pdf

    don’t have a working setup with Python and RabbitMQ, or you’d just like to experi- ment without setting up the whole environment, we’ve included instructions on set- ting up a Vagrant box with ...

    RabbitMQ-in-Action-Distributed-Messaging-for-Everyone.pdf

    Writing this book has been like discovering RabbitMQ itself—encountering a prob- lem that needed solving, but not knowing what the solution looked like. Until May 2010, we didn’t even know each ...

    RabbitMQ in action

    RabbitMQ is an efficient, highly scalable, and easy-to-deploy queue that makes handling this message traffic virtually effortless. Offered under an open source license and language neutral, RabbitMQ ...

    rabbitmq-server-mac-standalone-3.5.7.tar.gz

    这个压缩包“rabbitmq-server-mac-standalone-3.5.7.tar.gz”是专为MAC OS X操作系统设计的RabbitMQ服务器版本,支持10.6.4或更高版本。在本文中,我们将深入探讨RabbitMQ的核心概念、安装过程以及如何在MAC OS X上...

    RabbitMQ消息模式之Confirm确认消息

    理解Confirm消息确认机制 消息的确认,是指生产者投递消息后,如果Broker收到消息,则会给我们生产者一个应答。生产者进行接收应答,用来确定这条消息是否正常的发送到Broker,...import com.rabbitmq.client.Queuein

    RabbitMQ Cluster.docx

    RabbitMQ 集群是将多个RabbitMQ节点组织成一个逻辑单元,它们共同分享并管理用户、虚拟主机、队列、交换机、绑定、运行时参数等分布式状态。集群化提供了高可用性,当单个节点故障时,其他节点能够接管服务,确保...

    rabbitmq-server-windows-3.6.12.zip

    RabbitMQ 3.6.12 is a maintenance release. Upgrades and Compatibility See the "Upgrading clusters" section of the ...To upgrade a RabbitMQ cluster, follow the instructions in RabbitMQ documentation.

    RabbitMQ in Action高清版

    ### RabbitMQ in Action 高清版 - 分布式消息传递为所有人 #### 一、书籍简介与背景 《RabbitMQ in Action》是一本全面介绍RabbitMQ分布式消息队列系统的书籍,由Alvaro Videla和Jason J. W. Williams共同撰写,...

    docker-rabbitmq-cluster集群搭建

    docker-rabbitmq-cluster集群搭建docker-rabbitmq-cluster集群搭建

    laravel-queue-rabbitmq, 用于 Laravel 队列的RabbitMQ驱动程序.zip

    laravel-queue-rabbitmq, 用于 Laravel 队列的RabbitMQ驱动程序 用于 Laravel的 RabbitMQ队列驱动程序 安装通过 Composer 安装这里软件包,使用:composer require vladimir-yuldashev/laravel-queue-

    RabbitMQ in depth 样章(chapter 1)

    RabbitMQ in depth的内容覆盖了从RabbitMQ的基础知识到深层次的应用,适合作为中级水平的读者使用,特别是那些正在编写使用RabbitMQ的应用程序,或者是在生产环境中负责管理RabbitMQ的开发者或系统管理员。...

    rabbitmq代理配置和编码1

    在RabbitMQ中,代理(或称Exchange)配置和编码是消息传递的核心组成部分。RabbitMQ是一个开源的消息队列系统,它使用代理来路由消息到正确的队列,从而实现不同应用之间的异步通信。以下是对标题和描述中提到的知识...

    rabbitmq-server-mac-standalone-3.5.3

    **RabbitMQ for Mac 安装指南** RabbitMQ 是一个开源的消息代理和队列服务器,广泛用于微服务架构中的消息传递。它基于AMQP(Advanced Message Queuing Protocol)协议,提供高可用性、可靠性和可扩展性。在这个...

    rabbitmq集群环境搭建

    ### RabbitMQ 集群环境搭建知识点 #### 一、RabbitMQ集群环境搭建概述 在分布式系统中,为了提高消息处理能力与系统的可靠性,通常会采用RabbitMQ集群架构。RabbitMQ集群允许消息发布者将消息发送到任意一个集群...

    k8s部署rabbitmq-cluster集群配置文件和docker镜像文件

    k8s部署rabbitmq-cluster集群配置文件和docker镜像文件,配合文章学习用,有需要可以下载,无需修改直接部署即可

    RabbitMQ消息中间件面试专题.pdf

    cluster是由多个运行RabbitMQ的Erlang节点组成,它们之间共享队列(queue)、交换机(exchange)和绑定(binding)等元数据信息。 元数据是关于数据的数据,在RabbitMQ中,主要分为Queue元数据、Exchange元数据、...

Global site tag (gtag.js) - Google Analytics