rabbitmq的安裝方法網上有很多教程,這里就不重復了。
在springboot上使用rabbitmq傳輸字符串和對象,本文所給出的例子是在兩個不同的項目之間進行對象和和字符串的傳輸。
rabbitmq的依賴(在兩個項目中一樣的配置):
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-amqp</artifactid> </dependency> |
pom配置文件(在兩個項目中一樣的配置):
1
2
3
4
5
6
7
8
9
|
spring.application.name: demo1 //項目名 spring.rabbitmq.host: 192.168 . 1.111 //寫自己的ip spring.rabbitmq.port: 5672 spring.rabbitmq.username: guest spring.rabbitmq.password: guest spring.rabbitmq.virtual-host: / spring.rabbitmq.publisher-confirms: true spring.rabbitmq.publisher-returns: true spring.rabbitmq.template.mandatory: true |
字符轉的相互傳輸(本例使用的topic類型)
1>. 首先,在生產者(項目a)中寫配置文件,其中生成隊列queue,交換機exchange并且進行綁定binding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import org.springframework.amqp.core.binding; import org.springframework.amqp.core.bindingbuilder; import org.springframework.amqp.core.queue; import org.springframework.amqp.core.topicexchange; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; /** * @author:fdh * @description: * @date: create in 16:13 2017/12/22 */ @configuration public class senderconfigration { /** *@description: 新建隊列 topic.messages *@data:16:14 2017/12/22 */ @bean (name = "messages" ) public queue queuemessages(){ return new queue( "topic.messages" ); } /** *@description: 定義交換器 *@data:16:15 2017/12/22 */ @bean public topicexchange exchange(){ return new topicexchange( "exchange" ); } /** *@description: 交換機與消息隊列進行綁定 隊列messages綁定交換機with topic.messages *@data:16:18 2017/12/22 */ @bean binding bindingexchangemessages( @qualifier ( "messages" ) queue queuemessages,topicexchange exchange){ return bindingbuilder.bind(queuemessages).to(exchange).with( "topic.messages" ); } } |
2>. 第二步(項目a),生產者把消息發送到消息隊列,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * @author:fdh * @description: * @date: create in 14:15 2017/12/22 */ @controller public class rabbitcontroller { @autowired private amqptemplate amqptemplate; @requestmapping ( "/sendss" ) public void send1(){ amqptemplate.convertandsend( "exchange" , "topic.messages" , "hello topic.messages rabbitmq" ); } } |
3>. 接下來,在消費者(項目b)端寫一個監聽器,交換器會根據綁定的routing key(topic.messages)把生產者生產的消息放到匹配的消息隊列中,監聽器會監聽相應的消息隊列來獲取路由到該消息隊列上的消息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; import org.springframework.amqp.rabbit.annotation.rabbitlistener; /** * @ author:fdh * @ description: 消息隊列監聽器 * @ date: create in 14:19 2017/12/22 */ @component public class receiver { @rabbitlistener (queues = "topic.messages" ) public void process2(string str1) throws classnotfoundexception{ system.out.println( "messages :" +str1); system.out.println(thread.currentthread().getname()+ "接收到來自topic.message隊列的消息: " +str1); } |
這樣,一個簡單的字符串的傳輸便寫好了,下面打開剛才定義的mapping: 192.168.1.111:8080/sendss
在消費者端的console窗口便會看到打印的消息
以上就是一個簡單的傳輸字符串的例子了。
2. 下面重點介紹一下消費者和生產者之間對象的傳輸。
對象的傳輸,要現在生產者(a)中進行序列化,即把對象轉化為字節數組進行傳輸,在消費者中,再把轉化的字節數組反序列化為對象。序列化和反序列化的方法很多,這里采用的是java的serializable 接口
1>. 在生產者(項目a)和消費者(項目b)的項目中創建實體類。
!注意!:新建實體類boy.java 該實體類在項目a、b中的位置,必須一致,即包名必須一致,在本項目中,boy.java 在項目a、b中都是: import com.fengdonghao.shiro.bean.boy;
實體類也要一致。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.fengdonghao.shiro.bean; import javax.persistence.*; import java.io.serializable; /** * @author:fdh * @description: * @date:create in11:14 2017/12/16 */ @entity public class boy implements serializable{ private static final long serialversionuid=1l; @id @generatedvalue private int id; private string name; private int age; @override public string tostring() { return "boy{" + "age=" + age + ", id=" + id + ", name='" + name + '\ '' + '}' ; } //此處省略getter 和setter 方法 } |
2>. 在生產者(a)中配置 消息隊列,交換器,并進行綁定binding,和在 例子1中的第一步是一樣的
3>. 在生產者(a)中的rabbitcontroller.java 中另寫一個mapping,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@requestmapping ( "/send" ) public void sendmessage() { boy boy= new boy(); boy.setname( "tim" ); boy.setage( 11 ); system.out.println(boy); //以下是序列化操作 //write obj to file objectoutputstream oos = null ; try { oos = new objectoutputstream( new fileoutputstream( new file( "e:\\webpackage\\a.txt" ))); //把序列化之后的字節數組暫時存放在該目錄下 oos.writeobject(boy); } catch (ioexception e) { e.printstacktrace(); } finally { ioutils.closequietly(oos); } rabbitmqservice.send( "對象已序列化" ); |
4>. 在消費者(b)中對字節數組進行反序列化。
在receiver中,重新編寫例1重點的監聽器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@rabbitlistener (queues = "topic.messages" ) public void process2(string str1) { system.out.println(thread.currentthread().getname()+ "接收到來自topic.message隊列的消息: " +str1+ " 并進行反序列化" ); file file = new file( "e:\\webpackage\\a.txt" ); //消費者和生產者中路徑要保持一致,才能讀取文件,進行解析 objectinputstream ois = null ; try { ois = new objectinputstream( new fileinputstream(file)); boy newuser = (boy) ois.readobject(); system.out.println( "反序列之后:" +newuser); system.out.println( "反序列之后getname:" +newuser.getname()); system.out.println( "反序列之后getage" +newuser.getage()); } catch (ioexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } finally { ioutils.closequietly(ois); try { fileutils.forcedelete(file); } catch (ioexception e) { e.printstacktrace(); } } system.out.println( "messages :" +str1); } |
驗證mapping: ip:8080/send
結果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/east123321/article/details/78900791