消息队列rabbitMQ初次使用

个人对消息队列的理解

rabbitMQ不同进程或系统间通信,有利于不同系统模块之间的解耦,类似于分布式中的ribbton和feign远程服务调用,前者依靠rabbitMQ中间件隔离不同系统模块,后者依靠熔断机制(隔离)实现服务间互不影响。

服务器搭建

下载Erlang客户端 : rabbitmq基于Erlang平台运行
下载rabbitmq客户端: 作为rabbitmq服务器
已下载好的文件
安装完成后
http://127.0.0.1:15672/
访问rabbitmq管理页面,默认用户guest/guest

这里主要讲下用spring项目链接rabbitmq

一、搭建消息生产者服务

1、pom引入包

 <dependency>
         <groupId>org.springframework.amqp</groupId>
         <artifactId>spring-rabbit</artifactId>
         <version>1.4.0.RELEASE</version>
      </dependency>
<!-- 用于序列化消息,便于传输-->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>1.2.47</version>
      </dependency>

2、消息生产者spring XML配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xsi:schemaLocation="http://www.springframework.org/schema/rabbit
   http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

	<!-- 定义RabbitMQ的连接工厂 -->
	<rabbit:connection-factory
		id="connectionFactory" host="127.0.0.1" port="5672" username="admin"
		password="admin" virtual-host="testhost" />

	<!-- 定义Rabbit模板,指定连接工厂以及定义exchange -->
	<rabbit:template id="amqpTemplate"
		connection-factory="connectionFactory" exchange="fanoutExchange" />
	<!-- <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" 
		exchange="fanoutExchange" routing-key="foo.bar" /> -->

	<!-- MQ的管理,包括队列、交换器等 -->
	<rabbit:admin connection-factory="connectionFactory" />

	<!-- 定义队列,自动声明 -->
	<rabbit:queue name="zpcQueue" auto-declare="true" />

	<!-- 定义交换器(共四种可选择),把Q绑定到交换机,自动声明 此处为发布-订阅 广播模式交换机 -->
	<!-- 
	1、在配置文件中将队列和交换机完成绑定
	2、可以在管理界面中完成绑定 :
	a)绑定关系如果发生变化,需要修改配置文件,并且服务需要重启。
	b)管理更加灵活。
	c)更容易对绑定关系的权限管理,流程管理
	 -->
	<rabbit:fanout-exchange name="fanoutExchange"
		auto-declare="true">
		<rabbit:bindings>
			<rabbit:binding queue="zpcQueue" />
		</rabbit:bindings>
	</rabbit:fanout-exchange>
</beans>

3、java代码

package com.tycho.rabbitmq;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSON;

/**
 * 消息生产者
 * 
 */
public class MsgSender {
    public static void main(String[] args) throws Exception {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:rabbitmq-context.xml");
        //RabbitMQ模板
        RabbitTemplate template = (RabbitTemplate) ctx.getBean(RabbitTemplate.class);

        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//24小时制
        //发送消息
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("type", "1");
        msg.put("date", date);
        msg.put("test", "这是一条测试消息");
        /**消息内容
        方案:
        1、消息内容使用对象做json序列化发送  alibaba   json工具
        a)数据大
        b)有些数据其他人是可能用不到的
        2、发送特定的业务字段,如id、操作类型
        */
        template.convertAndSend("type2", JSON.toJSONString(msg));
        Thread.sleep(1000);// 休眠1秒
        ctx.destroy(); //容器销毁
//        ctx.close();
    }
}

二、搭建消息消费者服务

1、pom配置

<!-- spring集成RabbitMQ -->
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit</artifactId>
			<version>2.1.4.RELEASE</version>
		</dependency>

		<!-- json序列化工具 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.47</version>
		</dependency>
        

2、spring配置文件

<!-- 定义RabbitMQ的连接工厂 -->
	<rabbit:connection-factory
		id="connectionFactory" host="127.0.0.1" port="5672" username="admin"
		password="admin" virtual-host="testhost" />

	<!-- MQ的管理,包括队列、交换器等 -->
	<rabbit:admin connection-factory="connectionFactory" />

	<!-- 定义B系统需要监听的队列,自动声明 -->
	<rabbit:queue name="zpcQueue" auto-declare="true" />

	<!-- 队列监听,监听方法listen -->
 	<rabbit:listener-container
		connection-factory="connectionFactory">
		<rabbit:listener ref="myMQlistener" method="listen"
			queue-names="zpcQueue" />
	</rabbit:listener-container> 
	<!-- 注入监听对象 -->
   <bean id="myMQlistener" class="com.tycho.rabbitmq.Listener" />

3、java代码

package com.tycho.rabbitmq;

/**
 * 消息 消费者
 * @author admin
 *
 */
public class Listener {
	public void listen(String msg) {
        System.out.println("\n消费者B开始处理消息: " + msg + "\n");
    }
}

测试

启动消息消费者(作为web项目)
消费者服务
运行消息生产者服务(作为java项目)
可在消费者服务控制台看到如下信息
消费生产者发来的消息

至此,rabbitmq使用成功,接下来打算集成到公司项目中去。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值