Rabbit_mq简单入门

 

 1.      需要安装erlang,安装步骤http://www.linuxidc.com/Linux/2013-06/85964.htm

Erlang安装过程中需要安装较多插件,安装步骤依次进行

2.      下载

http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.4/rabbitmq-server-generic-unix-3.4.4.tar.gz

安装,启动过程忽略

 

 

3.       关注点

启动过程中遇到关注的问题

RABBITMQ_NODENAME=master@abcd.com

RABBITMQ_NODE_IP_ADDRESS=192.168.11.184

 

红色部分认为是个域名或计算机名,启动的时候会有检查这个域名在局域网中是否存在

需要将他写到hosts文件中将ip和名称绑定

Vi /etc/hosts

添加红色部分

127.0.0.1   abcd.com

或者

192.168.11.184  abcd.com

 

4.        启动管理插件

Cd  $rabbitmq_server-3.4.3/sbin

./rabbitmq-pluginsenable rabbitmq_management

 

5.       启动服务 

./rabbitmq-server –detached 后台启动

 

打开管理界面

http://server-name:15672/ ,3.0以前段端口是55672

 

6.      管理配置(查阅http://www.rabbitmq.com/management.html

 

安装最新版本的rabbitmq(3.3.1),并启用managementplugin后,使用默认的账号guest登陆管理控制台,却提示登陆失败。

 

翻看官方的release文档后,得知由于账号guest具有所有的操作权限,并且又是默认账号,出于安全因素的考虑,guest用户只能通过localhost登陆使用,并建议修改guest用户的密码以及新建其他账号管理使用rabbitmq(该功能是在3.3.0版本引入的)。

 

虽然可以以比较猥琐的方式:将ebin目录下rabbit.app中loopback_users里的<<"guest">>删除,并重启rabbitmq,可通过任意IP使用guest账号登陆管理控制台,但始终是违背了设计者的初衷,再加上以前对这一块了解也不多,因此有必要总结一下。

 

添加用户,用户角色设置,用户权限设置,需要用到#rabbitmq_home/sbin/rabbitmqctl 工具

 

1)  添加用户

add_user {username} {password}

./rabbitmqctl add_user video video

 

2) 查看所有用户

./rabbitmqctl list_users

 

[root@abcdsbin]# ./rabbitmqctl list_users

Listing users...

admin   []

guest   [administrator]

video   []

 

3) 刚添加用户没有角色

set_user_tags {username} {tag ...}

rabbitmqctl set_user_tags  video administrator

再次查看用户

[root@abcdsbin]# ./rabbitmqctl list_users

Listing users...

admin   []

guest   [administrator]

video   [administrator]

 

4) 用户权限设置

用户权限指的是用户对exchange,queue的操作权限,包括配置权限,读写权限。配置权限会影响到exchange,queue的创建和删除。读写权限影响到从queue里取消息,向exchange发送消息以及queue和exchange的绑定(bind)操作。

 

例如: 将queue绑定到某exchange上,需要具有queue的可写权限,以及exchange的可读权限;向exchange发送消息需要具有exchange的可写权限;从queue里取数据需要具有queue的可读权限。详细请参考官方文档中"How permissions work"部分。

 

a.  建立虚拟路径

add_vhost {vhostpath}

vhostpath

The name of the virtual host for whichto list the users that have been granted access to it, and their permissions.Defaults to /.

默认跟路径是 /

 

b. 设置用户权限

set_permissions [-p vhostpath] {user} {conf} {write} {read}

 

rabbitmqctl set_permissions -p /myvhost tonyg "^tonyg-.*" ".*" ".*

This command instructs the RabbitMQ broker to grant the usernamed tonyg access tothe virtual host called /myvhost, with configurepermissions on all resources whose names starts with "tonyg-", andwrite and read permissions on all resources.

 

./rabbitmqctlset_permissions -p / video ".*" ".*" ".*

这是讲所有访问权限给用户video

 

c.  查看用户权限

./rabbitmqctllist_user_permissions video

Listingpermissions for user "video" ...

/       .*     .*      .*

 

关于rabbitmqctl 工具有及其丰富的操作,详情查阅(http://www.rabbitmq.com/man/rabbitmqctl.1.man.html)

 

7.      通过管理界面创建交换机exchange,队列queue,绑定交换机和队列

以上可以手动创建绑定,也可以通过客户端程序创建版本的.

 

  

8.       客户端使用

Spring-amqp

 

Maven依赖

</pre><pre name="code" class="java"><dependencies>
    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>1.4.3.RELEASE</version>
    </dependency>
</dependencies>


 

Just Java

 

public staticvoid main(final String... args) throws Exception {
 
    ConnectionFactory cf = newCachingConnectionFactory();
 
    // set up the queue, exchange, binding onthe broker
    RabbitAdmin admin = new RabbitAdmin(cf);
    Queue queue = newQueue("myQueue");
    admin.declareQueue(queue);
    TopicExchange exchange = newTopicExchange("myExchange");
    admin.declareExchange(exchange);
    admin.declareBinding(
        BindingBuilder.bind(queue).to(exchange).with("foo.*"));
 
    // set up the listener and container
    SimpleMessageListenerContainer container =
            newSimpleMessageListenerContainer(cf);
    Object listener = new Object() {
        public void handleMessage(String foo) {
            System.out.println(foo);
        }
    };
    MessageListenerAdapter adapter = newMessageListenerAdapter(listener);
    container.setMessageListener(adapter);
   container.setQueueNames("myQueue");
    container.start();
 
    //send something
    RabbitTemplate template = newRabbitTemplate(cf);
   template.convertAndSend("myExchange", "foo.bar","Hello, world!");
    Thread.sleep(1000);
    container.stop();
}


 

 

 

Spring 测试

public staticvoid main(final String... args) throws Exception {
 
    AbstractApplicationContext ctx =
        newClassPathXmlApplicationContext("context.xml");
    RabbitTemplate template =ctx.getBean(RabbitTemplate.class);
    template.convertAndSend("Hello,world!");
    Thread.sleep(1000);
    ctx.destroy();
}


 

Spring 配置

 

<rabbit:connection-factoryid="connectionFactory" />
 
<rabbit:templateid="amqpTemplate" connection-factory="connectionFactory"
    exchange="myExchange"routing-key="foo.bar"/>
 
<rabbit:adminconnection-factory="connectionFactory" />
 
<rabbit:queuename="myQueue" />
 
<rabbit:topic-exchangename="myExchange">
    <rabbit:bindings>
        <rabbit:bindingqueue="myQueue" pattern="foo.*" />
    </rabbit:bindings>
</rabbit:topic-exchange>
 
 
<rabbit:listener-containerconnection-factory="connectionFactory">
    <rabbit:listener ref="foo"method="listen" queue-names="myQueue" />
</rabbit:listener-container>
 
<beanid="foo" class="foo.Foo" />


 

 

例子参照:

http://projects.spring.io/spring-amqp/#quick-start

https://github.com/SpringSource/spring-amqp-samples

https://github.com/SpringSource/spring-integration-samples/tree/master/basic/amqp

 

自己调通的例子

http://pan.baidu.com/s/1eQJ7L0A

 

 

 

本文是简单入门级操作,深入学习请查阅官网文档

http://www.rabbitmq.com/documentation.html

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值