RabbitMQ精講7:與SpringBoot、Spring Cloud Stream整合實戰

目錄

與SpringBoot整合實戰

1. 生產端

2. 消費端

消費端核心配置:

@RabbitListener註解使用

3. 代碼示例

3.1 pom文件 和消息實體

3.2 生產者

3.3 消費者

與Spring Cloud Stream整合實戰

1. 架構介紹

2. 核心概念:

3. 代碼示例

3.1 pom文件 和 消息實體

 3.2 生產者

3.3 消費者


與SpringBoot整合實戰

1. 生產端

SpringBoot整合配置詳解-生產端
  • publisher-confirms,實現一個監聽器用於監聽Broker端給我們返回的確認請求:RabbitTemplate.ConfirmCallback
  • publisher-returns,保證消息對Broker端是可達的,如果出現路由鍵不可達的情況,則使用監聽器對不可達的消息進行後續的處理,保證消息的路由成功:RabbitTemplate.ReturnCallback
  • 注意一點,在發送消息的時候對template進行配置mandatory=true保證監聽有效
  • 生產端還可以配置其他屬性,比如發送重試,超時時間,次數,間隔等
     

2. 消費端

消費端核心配置:

SpringBoot整合配置詳解-消費端

##簽收模式-手工簽收
spring.rabbitmq.listener.simple.acknowledge-mode=manual
##設置監聽限制:最大10,默認5
spring.rabbitmq.listener.simple.concurrency=5
spring.rabbitmq.listener.simple.max-concurrency=10 

SpringBoot整合配置詳解-消費端
  • 首先配置手工確認模式,用於ACK的手工處理,這樣我們可以保證消息的可靠性送達,或者再消費端消費失敗的時候可以做到重回隊列(不建議)、根據業務記錄日誌等處理。
  • 可以設置消費端的監聽個數和最大個數,用於監控消費端的併發情況

@RabbitListener註解使用

@RabbitListener註解使用
@RabbitListener註解使用
  •  消費端監聽@RabbitListener註解,這個對於在實際工作中非常的好用
  • @RabbitListener是一個組合註解,裏面可以註解配置
  • @QueueBinding、@Queue、@Exchange直接通過這個組合註解一次性搞定消費端交換機、隊列、綁定、路由、並且配置監聽功能等。

3. 代碼示例

3.1 pom文件 和消息實體

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
	</dependencies>
import java.io.Serializable;

public class Order implements Serializable {

	private String id;
	private String name;
	
	public Order() {
	}
	public Order(String id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}

3.2 生產者

application.properties

spring.rabbitmq.addresses=192.168.11.76:5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=15000

spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
spring.rabbitmq.template.mandatory=true
  •  RabbitSender
import java.util.Map;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import com.bfxy.springboot.entity.Order;

@Component
public class RabbitSender {

	//自動注入RabbitTemplate模板類
	@Autowired
	private RabbitTemplate rabbitTemplate;  
	
	//回調函數: confirm確認
	final ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
		@Override
		public void confirm(CorrelationData correlationData, boolean ack, String cause) {
			System.err.println("correlationData: " + correlationData);
			System.err.println("ack: " + ack);
			if(!ack){
				System.err.println("異常處理....");
			}
		}
	};
	
	//回調函數: return返回
	final ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
		@Override
		public void returnedMessage(org.springframework.amqp.core.Message message, int replyCode, String replyText,
				String exchange, String routingKey) {
			System.err.println("return exchange: " + exchange + ", routingKey: " 
				+ routingKey + ", replyCode: " + replyCode + ", replyText: " + replyText);
		}
	};
	
	//發送消息方法調用: 構建Message消息
	public void send(Object message, Map<String, Object> properties) throws Exception {
		MessageHeaders mhs = new MessageHeaders(properties);
		Message msg = MessageBuilder.createMessage(message, mhs);
		rabbitTemplate.setConfirmCallback(confirmCallback);
		rabbitTemplate.setReturnCallback(returnCallback);
		//id + 時間戳 全局唯一 
		CorrelationData correlationData = new CorrelationData("1234567890");
		rabbitTemplate.convertAndSend("exchange-1", "springboot.abc", msg, correlationData);
	}
	
	//發送消息方法調用: 構建自定義對象消息
	public void sendOrder(Order order) throws Exception {
		rabbitTemplate.setConfirmCallback(confirmCallback);
		rabbitTemplate.setReturnCallback(returnCallback);
		//id + 時間戳 全局唯一 
		CorrelationData correlationData = new CorrelationData("0987654321");
		rabbitTemplate.convertAndSend("exchange-2", "springboot.def", order, correlationData);
	}
	
}
  • 測試類
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.bfxy.springboot.entity.Order;
import com.bfxy.springboot.producer.RabbitSender;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

	@Test
	public void contextLoads() {
	}
	
	@Autowired
	private RabbitSender rabbitSender;

	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	
	@Test
	public void testSender1() throws Exception {
		 Map<String, Object> properties = new HashMap<>();
		 properties.put("number", "12345");
		 properties.put("send_time", simpleDateFormat.format(new Date()));
		 rabbitSender.send("Hello RabbitMQ For Spring Boot!", properties);
	}
	
	@Test
	public void testSender2() throws Exception {
		 Order order = new Order("001", "第一個訂單");
		 rabbitSender.sendOrder(order);
	}
	
	
	
}

3.3 消費者

application.properties

spring.rabbitmq.addresses=192.168.11.76:5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=15000

spring.rabbitmq.listener.simple.acknowledge-mode=manual
spring.rabbitmq.listener.simple.concurrency=5
spring.rabbitmq.listener.simple.max-concurrency=10

spring.rabbitmq.listener.order.queue.name=queue-2
spring.rabbitmq.listener.order.queue.durable=true
spring.rabbitmq.listener.order.exchange.name=exchange-2
spring.rabbitmq.listener.order.exchange.durable=true
spring.rabbitmq.listener.order.exchange.type=topic
spring.rabbitmq.listener.order.exchange.ignoreDeclarationExceptions=true
spring.rabbitmq.listener.order.key=springboot.*
  •  RabbitReceiver 
package com.bfxy.springboot.conusmer;

import java.util.Map;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

@Component
public class RabbitReceiver {

	
	@RabbitListener(bindings = @QueueBinding(
			value = @Queue(value = "queue-1", 
			durable="true"),
			exchange = @Exchange(value = "exchange-1", 
			durable="true", 
			type= "topic", 
			ignoreDeclarationExceptions = "true"),
			key = "springboot.*"
			)
	)
	@RabbitHandler
	public void onMessage(Message message, Channel channel) throws Exception {
		System.err.println("--------------------------------------");
		System.err.println("消費端Payload: " + message.getPayload());
		Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
		//手工ACK
		channel.basicAck(deliveryTag, false);
	}
	
	
	/**
	 * 
	 * 	spring.rabbitmq.listener.order.queue.name=queue-2
		spring.rabbitmq.listener.order.queue.durable=true
		spring.rabbitmq.listener.order.exchange.name=exchange-1
		spring.rabbitmq.listener.order.exchange.durable=true
		spring.rabbitmq.listener.order.exchange.type=topic
		spring.rabbitmq.listener.order.exchange.ignoreDeclarationExceptions=true
		spring.rabbitmq.listener.order.key=springboot.*
	 * @param order
	 * @param channel
	 * @param headers
	 * @throws Exception
	 */
	@RabbitListener(bindings = @QueueBinding(
			value = @Queue(value = "${spring.rabbitmq.listener.order.queue.name}", 
			durable="${spring.rabbitmq.listener.order.queue.durable}"),
			exchange = @Exchange(value = "${spring.rabbitmq.listener.order.exchange.name}", 
			durable="${spring.rabbitmq.listener.order.exchange.durable}", 
			type= "${spring.rabbitmq.listener.order.exchange.type}", 
			ignoreDeclarationExceptions = "${spring.rabbitmq.listener.order.exchange.ignoreDeclarationExceptions}"),
			key = "${spring.rabbitmq.listener.order.key}"
			)
	)
	@RabbitHandler
	public void onOrderMessage(@Payload com.bfxy.springboot.entity.Order order, 
			Channel channel, 
			@Headers Map<String, Object> headers) throws Exception {
		System.err.println("--------------------------------------");
		System.err.println("消費端order: " + order.getId());
		Long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
		//手工ACK
		channel.basicAck(deliveryTag, false);
	}
	
	
}

與Spring Cloud Stream整合實戰

與Spring Cloud Stream整合

Spring Cloud,這個全家桶框架在整個中小型互聯網公司異常的火爆,那麼相對應的Spring Cloud Stream 就漸漸的被大家所重視起來,這一節課主要來介紹Spring Cloud Stream如何與RabbitMQ進行集成。

1. 架構介紹

 

  • Destination Binder:包含自己的應用Application
  • 左邊是RabbitMQ、右邊是Kafka。表示消息的生產與發送,可以是不同的消息中間件。這是Spring Cloud Stream 最上層的一個抽象。非常好的地方。

  • 兩個比較重要的地方:inputs(輸入)消息接收端、outputs(輸出)消息發送端
  • 一個 Spring Cloud Stream 應用以消息中間件爲核心,應用通過Spring Cloud Stream注入的輸入/輸出通道 channels 與外部進行通信。channels 通過特定的Binder實現與外部消息中間件進行通信。

  • 黃色:表示RabbitMQ
  • 綠色:插件,消息的輸入輸出都套了一層插件,插件可以用於各種各樣不同的消息,也可以用於消息中間件的替換。

2. 核心概念:

  • Barista [bəˈri:stə] 接口:Barista接口是定義來作爲後面類的參數,這一接口定義來通道類型和通道名稱
    • 通道名稱是作爲配置用,
    • 通道類型則決定了app會使用這一通道進行發送消息還是從中接收消息。

通道接口如何定義:

  • @Output:輸出註解,用於定義發送消息接口
  • @Input:輸入註解,用於定義消息的消費者接口
  • @StreamListener:用於定義監聽方法的註解

使用Spring Cloud Stream 非常簡單,只需要使用好這3個註解即可,在實現高性能消息的生產和消費的場景非常合適,但是使用SpringCloudStream框架有一個非常大的問題,就是不能實現可靠性的投遞,也就是沒法保證消息的100%可靠性,會存在少量消息丟失的問題。

  • 目前SpringCloudStream整合了RabbitMQ與Kafka,我們都知道Kafka是無法進行消息可靠性投遞的,這個原因是因爲SpringCloudStream框架爲了和Kafka兼顧所以在實際工作中使用它的目的就是針對高性能的消息通信的!這點就是在當前版本SpringCloudStream的定位。
  • 因此在實際的工作中,可以採用SpringCloudStream,如果需要保證可靠性投遞,也可以單獨採用RabbitMQ,也是可以的。

3. 代碼示例

3.1 pom文件 和 消息實體

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-autoconfigure</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!--spring-cloud-starter-stream-->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
		    <version>1.3.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
	</dependencies>

 3.2 生產者

  •   application.properties
server.port=8001
server.servlet.context-path=/producer

spring.application.name=producer
spring.cloud.stream.bindings.output_channel.destination=exchange-3
spring.cloud.stream.bindings.output_channel.group=queue-3
spring.cloud.stream.bindings.output_channel.binder=rabbit_cluster

spring.cloud.stream.binders.rabbit_cluster.type=rabbit
spring.cloud.stream.binders.rabbit_cluster.environment.spring.rabbitmq.addresses=192.168.11.76:5672
spring.cloud.stream.binders.rabbit_cluster.environment.spring.rabbitmq.username=guest
spring.cloud.stream.binders.rabbit_cluster.environment.spring.rabbitmq.password=guest
spring.cloud.stream.binders.rabbit_cluster.environment.spring.rabbitmq.virtual-host=/
  • Barista接口
package com.bfxy.rabbitmq.stream;

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

/**
 * <B>中文類名:</B><BR>
 * <B>概要說明:</B><BR>
 * 這裏的Barista接口是定義來作爲後面類的參數,這一接口定義來通道類型和通道名稱。
 * 通道名稱是作爲配置用,通道類型則決定了app會使用這一通道進行發送消息還是從中接收消息。
 */
public interface Barista {
	  
    //String INPUT_CHANNEL = "input_channel";  
    String OUTPUT_CHANNEL = "output_channel";  

    //註解@Input聲明瞭它是一個輸入類型的通道,名字是Barista.INPUT_CHANNEL,也就是position3的input_channel。這一名字與上述配置app2的配置文件中position1應該一致,表明注入了一個名字叫做input_channel的通道,它的類型是input,訂閱的主題是position2處聲明的mydest這個主題  
//    @Input(Barista.INPUT_CHANNEL)  
//    SubscribableChannel loginput();  
    //註解@Output聲明瞭它是一個輸出類型的通道,名字是output_channel。這一名字與app1中通道名一致,表明注入了一個名字爲output_channel的通道,類型是output,發佈的主題名爲mydest。  
    @Output(Barista.OUTPUT_CHANNEL)
    MessageChannel logoutput();  

//	String INPUT_BASE = "queue-1";  
//	String OUTPUT_BASE = "queue-1";  
//	@Input(Barista.INPUT_BASE)  
//	SubscribableChannel input1();  
//	MessageChannel output1();  
      
}  
  •  RabbitmqSender 
package com.bfxy.rabbitmq.stream;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@EnableBinding(Barista.class)
@Service  
public class RabbitmqSender {  
  
    @Autowired  
    private Barista barista;  
    
    // 發送消息
    public String sendMessage(Object message, Map<String, Object> properties) throws Exception {  
        try{
        	MessageHeaders mhs = new MessageHeaders(properties);
        	Message msg = MessageBuilder.createMessage(message, mhs);
            boolean sendStatus = barista.logoutput().send(msg);
            System.err.println("--------------sending -------------------");
            System.out.println("發送數據:" + message + ",sendStatus: " + sendStatus);
        }catch (Exception e){  
        	System.err.println("-------------error-------------");
        	e.printStackTrace();
            throw new RuntimeException(e.getMessage());
           
        }  
        return null;
    }  
    
}  
  • 測試類
package com.bfxy.rabbitmq;

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

import org.apache.http.client.utils.DateUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.bfxy.rabbitmq.stream.RabbitmqSender;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

	@Autowired
	private RabbitmqSender rabbitmqSender;
	
	
	@Test
	public void sendMessageTest1() {
       for(int i = 0; i < 1; i ++){
    	   try {
		       Map<String, Object> properties = new HashMap<String, Object>();
		       properties.put("SERIAL_NUMBER", "12345");
		       properties.put("BANK_NUMBER", "abc");
		       properties.put("PLAT_SEND_TIME", DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
	    	   rabbitmqSender.sendMessage("Hello, I am amqp sender num :" + i, properties);
              
           } catch (Exception e) {
        	   System.out.println("--------error-------");
               e.printStackTrace(); 
           }
       }
       //TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
	}
	
}

3.3 消費者

  • application.properties
server.port=8002
server.context-path=/consumer

spring.application.name=consumer
spring.cloud.stream.bindings.input_channel.destination=exchange-3
spring.cloud.stream.bindings.input_channel.group=queue-3
spring.cloud.stream.bindings.input_channel.binder=rabbit_cluster
spring.cloud.stream.bindings.input_channel.consumer.concurrency=1
spring.cloud.stream.rabbit.bindings.input_channel.consumer.requeue-rejected=false
spring.cloud.stream.rabbit.bindings.input_channel.consumer.acknowledge-mode=MANUAL
spring.cloud.stream.rabbit.bindings.input_channel.consumer.recovery-interval=3000
spring.cloud.stream.rabbit.bindings.input_channel.consumer.durable-subscription=true
spring.cloud.stream.rabbit.bindings.input_channel.consumer.max-concurrency=5

spring.cloud.stream.binders.rabbit_cluster.type=rabbit
spring.cloud.stream.binders.rabbit_cluster.environment.spring.rabbitmq.addresses=192.168.11.76:5672
spring.cloud.stream.binders.rabbit_cluster.environment.spring.rabbitmq.username=guest
spring.cloud.stream.binders.rabbit_cluster.environment.spring.rabbitmq.password=guest
spring.cloud.stream.binders.rabbit_cluster.environment.spring.rabbitmq.virtual-host=/
  • Barista接口
package com.bfxy.rabbitmq.stream;

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;

/**
 * <B>中文類名:</B><BR>
 * <B>概要說明:</B><BR>
 * 這裏的Barista接口是定義來作爲後面類的參數,這一接口定義來通道類型和通道名稱。
 * 通道名稱是作爲配置用,通道類型則決定了app會使用這一通道進行發送消息還是從中接收消息。
 * @author ashen(Alienware)
 * @since 2016年7月22日
 */

public interface Barista {
	  
    String INPUT_CHANNEL = "input_channel";  

    //註解@Input聲明瞭它是一個輸入類型的通道,名字是Barista.INPUT_CHANNEL,也就是position3的input_channel。這一名字與上述配置app2的配置文件中position1應該一致,表明注入了一個名字叫做input_channel的通道,它的類型是input,訂閱的主題是position2處聲明的mydest這個主題  
    @Input(Barista.INPUT_CHANNEL)  
    SubscribableChannel loginput();  
    
      
}  
  • RabbitmqReceiver
  • package com.bfxy.rabbitmq.stream;
    
    import org.springframework.amqp.support.AmqpHeaders;
    import org.springframework.cloud.stream.annotation.EnableBinding;
    import org.springframework.cloud.stream.annotation.StreamListener;
    import org.springframework.messaging.Message;
    import org.springframework.stereotype.Service;
    
    import com.rabbitmq.client.Channel;
    
    
    @EnableBinding(Barista.class)
    @Service
    public class RabbitmqReceiver {  
    
        @StreamListener(Barista.INPUT_CHANNEL)  
        public void receiver(Message message) throws Exception {  
    		Channel channel = (com.rabbitmq.client.Channel) message.getHeaders().get(AmqpHeaders.CHANNEL);
    		Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        	System.out.println("Input Stream 1 接受數據:" + message);
        	System.out.println("消費完畢------------");
        	channel.basicAck(deliveryTag, false);
        }  
    }  
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章