spring 整合 rabbitmq 的项目为 spring-amqp.
spring rabbitmq 支持RPC。需要使用spring-rabbit-support.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
spring 通过 rabbitmq 支持RPC,采用Java序列化和AMQP协议。
接口源代码:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
实现源代码:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
创建客户端配置文件
- 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
- 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
创建服务器端配置文件
- 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
- 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
服务器端代码:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
客户端调用代码:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
服务端日志
EBUG] [2017-06-09 15:38:45] org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$InternalConsumer.handleDelivery(829) | Storing delivery for Consumer@6b419da: tags=[{amq.ctag-Pz6I0WhtxbKgR5li4-HoMw=remoting.queue}], channel=Cached Rabbit Channel: AMQChannel(amqp://root@120.92.73.218:5672/,1), conn: Proxy@b24502d Shared Rabbit Connection: SimpleConnection@666138da [delegate=amqp://root@120.92.73.218:5672/, localPort= 62043], acknowledgeMode=AUTO local queue size=0
[DEBUG] [2017-06-09 15:38:46] org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.handle(418) | Received message: (Body:’RemoteInvocation: method name ‘add’; parameter types [int, int]’ MessageProperties [headers={}, timestamp=null, messageId=null, userId=null, receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null, correlationIdString=null, replyTo=amq.rabbitmq.reply-to.g2dkABVyYWJiaXRAdm0xNzItMzEtMTYtMTIAAAS4AAAAAAI=.+iFiCAvwbRdzkBP01tN2Tw==, contentType=application/x-java-serialized-object, contentEncoding=null, contentLength=0, deliveryMode=null, receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=remoting.exchange, receivedRoutingKey=remoting.binding, receivedDelay=null, deliveryTag=1, messageCount=0, consumerTag=amq.ctag-Pz6I0WhtxbKgR5li4-HoMw, consumerQueue=remoting.queue])
[DEBUG] [2017-06-09 15:38:46] org.springframework.amqp.rabbit.connection.CachingConnectionFactory.getCachedChannelProxy(494) | Creating cached Rabbit Channel from AMQChannel(amqp://root@120.92.73.218:5672/,2)
[DEBUG] [2017-06-09 15:38:46] org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(1453) | Executing callback on RabbitMQ Channel: Cached Rabbit Channel: AMQChannel(amqp://root@120.92.73.218:5672/,2), conn: Proxy@b24502d Shared Rabbit Connection: SimpleConnection@666138da [delegate=amqp://root@120.92.73.218:5672/, localPort= 62043]
[DEBUG] [2017-06-09 15:38:46] org.springframework.amqp.rabbit.core.RabbitTemplate.doSend(1507) | Publishing message on exchange [], routingKey = [amq.rabbitmq.reply-to.g2dkABVyYWJiaXRAdm0xNzItMzEtMTYtMTIAAAS4AAAAAAI=.+iFiCAvwbRdzkBP01tN2Tw==]
从日志可以看到,服务端从remoting.binding这个队列获取RPC请求信息。完成处理后反馈到一个特定路由键,客户端通过这个路由键获取处理后的结果。
客户端日志:
[DEBUG] [2017-06-09 15:38:45] org.springframework.amqp.rabbit.core.RabbitTemplate.doSend(1507) | Publishing message on exchange [remoting.exchange], routingKey = [remoting.binding]
[DEBUG] [2017-06-09 15:38:46] org.springframework.amqp.rabbit.core.RabbitTemplate.exchangeMessages(1380) | Reply: (Body:’org.springframework.remoting.support.RemoteInvocationResult@6d763516’ MessageProperties [headers={}, timestamp=null, messageId=null, userId=null, receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null, correlationIdString=null, replyTo=null, contentType=application/x-java-serialized-object, contentEncoding=null, contentLength=0, deliveryMode=null, receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=, receivedRoutingKey=amq.rabbitmq.reply-to.g2dkABVyYWJiaXRAdm0xNzItMzEtMTYtMTIAAAS4AAAAAAI=.+iFiCAvwbRdzkBP01tN2Tw==, receivedDelay=null, deliveryTag=1, messageCount=null, consumerTag=null, consumerQueue=null])