springMvc-spring-cloud-Stream通過rabbitMq交互

springMvc-spring-cloud-Stream通過rabbitMq交互

這裏就不贅述了,網上一搜一大把

rabbitMq安裝

首先需要裝rabbitmq,這裏請參考官網資料。這裏只給出Windows和linux 的rpm方式的安裝鏈接。 Windows鏈接linuxRPM鏈接

生產者(springMVC)項目接入rabbitmq

  1. 新建xml配置
    文件命名spring-rabbitmq.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
	<!-- rabbitmq begin -->
 	<!-- 創建connectionFactory -->
	<bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
	    <constructor-arg value="127.0.0.1"/>
	    <property name="host" value="127.0.0.1" />
	    <property name="port" value="5672" />
	    <property name="username" value="guest"/>
	    <property name="password" value="guest"/>
	    <property name="channelCacheSize" value="100" />  
	</bean>

	<rabbit:admin id="amqpAdmin" connection-factory="rabbitConnectionFactory"/>
	 <!--聲明隊列queue-->
    <rabbit:queue id="queue1" name="queue1" durable="true" auto-delete="false" exclusive="false" />
    <rabbit:queue id="queue2" name="queue2 durable="true" auto-delete="false" exclusive="false" />
    <rabbit:queue id="queue3" name="queue3" durable="true" auto-delete="false" exclusive="false" />
    
    <!-- 交換器與隊列綁定exchange queue binging key 綁定 -->	
    <rabbit:topic-exchange name="queue.queue1" durable="true" auto-delete="false">	
    	<rabbit:bindings>
        <rabbit:binding queue="queue1" pattern="*.*.queue1Consumers" />
      </rabbit:bindings>
    </rabbit:topic-exchange>
    <rabbit:topic-exchange name="queue.queue2" durable="true" auto-delete="false">	
    	<rabbit:bindings>
        <rabbit:binding queue="queue2" pattern="*.*.queue2Consumers" />
      </rabbit:bindings>
    </rabbit:topic-exchange>
    <rabbit:topic-exchange name="queue.queue3" durable="true" auto-delete="false">	
    	<rabbit:bindings>
        <rabbit:binding queue="queue3" pattern="*.*.queue3Consumers" />
      </rabbit:bindings>
    </rabbit:topic-exchange>
	<!-- 消息轉換器 -->
    <bean id="messageConverter"  class="org.springframework.amqp.support.converter.JsonMessageConverter" />  
<!-- spring template聲明	 -->
	<rabbit:template id="amqpTemplate" connection-factory="rabbitConnectionFactory" message-converter="messageConverter" />

	<!-- rabbitmq end -->
</beans>

connectionFactory 連接工廠

  1. web.xml配置;
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-rabbitmq.xml
        </param-value>
  </context-param>
  1. 生產者代碼
   @Autowired
     private AmqpTemplate amqpTemplate;

    @Override
    public void produce(final Order order) {
        logger.info("回調隊列destination: queue.queue1.queue1Consumers");
        logger.info("order: " + order);
        String json = JSON.toJSONString(order);
        amqpTemplate.convertAndSend("queue.queue1.queue1Consumers",order);
    }

消費者(spring-cloud-stream)項目接入rabbitmq

  1. 配置文件 application.yml
spring:
  cloud:
    stream:
      bindings:
        queue1:                             # 這個名字是一個通道的名稱
          destination: queue.queue1  # 目的,對應 MQ 是 exchange, 生成臨時隊列
          content-type: text/plain
          binder: local_rabbit             # 綁定器名稱
          group: queue1Consumers       # 進行操作的分組,實際上就表示持久化--消費端配置
        queue2:                             # 這個名字是一個通道的名稱
          destination: queue.queue2 # 目的,對應 MQ 是 exchange, 生成臨時隊列
          binder: local_rabbit             # 綁定器名稱
          group: queue2Consumers       # 進行操作的分組,實際上就表示持久化--消費端配置
        queue3:                             # 這個名字是一個通道的名稱
          destination: queue.queue3 # 目的,對應 MQ 是 exchange, 生成臨時隊列
          content-type: application/json # 設置消息類型,本次爲對象json,如果是文本則設置“text/plain”
          binder: local_rabbit             # 綁定器名稱
          group: queue3Consumers       # 進行操作的分組,實際上就表示持久化--消費端配置
       --消費端配置
      binders:       # 在此處配置要綁定的rabbitmq的服務信息;     
        local_rabbit:   # rabbitmq的服務名稱  
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: 127.0.0.1
                port: 5672
                username: guest
                password: guest
                virtual-host: /
  1. 自定義通道
public interface ReceviceQueueProcess {

    public static final String queue1= "queue1"; // 輸入通道名稱
    public static final String queue2= "queue2"; // 輸入通道名稱
    public static final String queue3= "queue3"; // 輸入通道名稱


    @Input(ReceviceQueueProcess.queue1)
    public SubscribableChannel queue1();

    @Input(ReceviceQueueProcess.queue2)
    public SubscribableChannel queue2();
    
    @Input(ReceviceQueueProcess.queue3)
    public SubscribableChannel queue3();
}

  1. ribbitmq監聽綁定器
@EnableBinding(ReceviceQueueProcess.class)
public class ReceiveMsgHandler  {
	
	private Logger logger = Logger.getLogger(getClass());
	
	@StreamListener(ReceviceQueueProcess.queue1)
	public void queue1(Message message) {
		logger.info("發送知監聽queue1");
		try {
			Object body = message.getPayload();
			String json = body.toString();
			logger.info("消息推送 body:" +json);
			logger.info("get message from queue1: " + json);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@StreamListener(ReceviceQueueProcess.queue2)
	public void queue2(Message message) {
		logger.info("發送通知監聽queue2");
		try {
			logger.info("發送通知監聽queue2");
			Object body = message.getPayload();
			String json = body.toString();;
			logger.info("推送 json:" + json);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@StreamListener(ReceviceQueueProcess.queue3)
	public void queue3(Message message) {
		logger.info("發送通知監聽queue3");
		try {
			logger.info("發送通知監聽queue3");
			Object body = message.getPayload();
			String json = body.toString();;
			logger.info("推送json:" + json);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章