rabbitmq基础命令
用户管理
1.添加用户
# rabbitmqctl add_user username password
# rabbitmqctl add_user admin admin
2.删除用户
# rabbitmqctl delete_user username
3.修改密码
# rabbitmqctl change_password username newpassword
4.列出所有用户
# rabbitmqctl list_users
权限控制 1.创建虚拟主机
# rabbitmqctl add_vhost vhostpath
rabbitmqctl add_vhost myvhost
2.删除虚拟主机
# rabbitmqctl delete_vhost myvhost
3.列出所有虚拟主机
# rabbitmqctl list_vhosts
4.设置用户权限
# rabbitmqctl set_permissions [-p vhostpath] username regexp regexp regexp
eg: rabbitmqctl set_permissions -p myvhost guest ".*" ".*" ".*"
5.清除用户权限
# rabbitmqctl clear_permissions [-p vhostpath] username
6.列出用户权限
# rabbitmqctl list_user_permissions username
rabbitmqctl list_user_permissions user_admin
7.列出虚拟主机上的所有权限
# rabbitmqctl list_permissions [-p vhostpath]
例子:
rabbitmqctl set_permissions -p myvhost admin ".*" ".*" ".*"
rabbitmq使用配置
<!-- RabbitMQ配置-开始 -->
<!-- 创建connectionFactory -->
<rabbit:connection-factory id="rabbitConnectionFactory" addresses="${rabbitmq_addresses}" username="${rabbitmq_username}" password="${rabbitmq_password}" virtual-host="${rabbitmq_virtual_host}"/>
<!-- 创建rabbitTemplate 消息模板类 -->
<rabbit:template id="smsRabbitTemplate" connection-factory="rabbitConnectionFactory" exchange="test-direct-exchange" routing-key="test-routing-key"/>
<!-- 创建rabbitAdmin 代理类 -->
<rabbit:admin id="RabbitAdmins" connection-factory="rabbitConnectionFactory"/>
<!-- 声明Queue并设定Queue的名称 -->
<rabbit:queue name="test-queue" declared-by="RabbitAdmins"/>
<!-- 声明Exchange的类型为topic并设定Exchange的名称 -->
<rabbit:direct-exchange name="test-direct-exchange" declared-by="RabbitAdmins">
<rabbit:bindings>
<rabbit:binding queue="test-queue" key="test-routing-key"/>
</rabbit:bindings>
</rabbit:direct-exchange>
<!-- 监听生产者发送的消息开始 -->
<rabbit:listener-container connection-factory="rabbitConnectionFactory" concurrency="20">
<rabbit:listener ref="TestConsumerHandle" method="testRabbitMq" queue-names="test-queue"/>
</rabbit:listener-container>
<!-- RabbitMQ配置-结束 -->
rabbitmq调用,如何使用队列
RabbitTemplate 使用org.springframework.amqp.rabbit.core.RabbitTemplate,调用convertAndSend提交队列。
public class a{
@Autowired
private RabbitTemplate testRabbitTemplate;
//生产
pubic void test(){
testRabbitTemplate.convertAndSend(object); // 提交队列
}
}
这里演示一个实现类
@Component("TestConsumerHandle")
public class TestConsumerHandleImpl implements TestConsumerHandle{
//消费
@Override
@Transactional(readOnly=false)
public void testRabbit(Object object) {
//....balabalabalab
}
}