Spring Integration 集成 AMQP
- 1. Spring Integration 集成 AMQP
1. Spring Integration 集成 AMQP
1.1. 版本说明
| 构件 | 版本 |
|---|---|
| spring-boot | 2.7.18 |
| spring-boot-starter-integration | 2.7.18 |
| spring-integration-amqp | 5.5.20 |
1.2. 概览
flowchart TB
subgraph outBound["Out Bound"]
direction TB
AmqpGateway["MessagingGateway\n(amqpGateway)"]
--> outboundMessageChannel["MessageChannel\n(outboundMessageChannel)"]
--> AmqpOutboundChannelAdapter["MessageHandler\n(AmqpOutboundChannelAdapter)"]
end
subgraph rabbitMq["Rabbit MQ"]
direction TB
spring-integratiton-amqp-demo-exchange["Exchange\n(spring-integratiton-amqp-demo-exchange)"]
--"routing-key-1"--> spring-integratiton-amqp-demo-queue-1["Queue\n(spring-integratiton-amqp-demo-queue-1)"]
spring-integratiton-amqp-demo-exchange
--"routing-key-2"--> spring-integratiton-amqp-demo-queue-2["Queue\n(spring-integratiton-amqp-demo-queue-2)"]
end
subgraph inBound["In Bound"]
direction TB
AmqpInboundChannelAdapterSMLCSpec["MessageProducer\n(AmqpInboundChannelAdapterSMLCSpec)"]
--> inboundMessageRouter["MessageRouter\n(inboundMessageRouter)"]
--"routing-key-1"--> inboundMessageChannel1["MessageChannel\n(inboundMessageChannel1)"]
--> inboundMessageHandler1["MessageHandler\n(inboundMessageHandler1)"]
inboundMessageRouter --"routing-key-2"--> inboundMessageChannel2["MessageChannel\n(inboundMessageChannel2)"]
--> inboundMessageHandler2["MessageHandler\n(inboundMessageHandler2)"]
end
AmqpOutboundChannelAdapter
--> spring-integratiton-amqp-demo-exchange
spring-integratiton-amqp-demo-queue-1 --> AmqpInboundChannelAdapterSMLCSpec
spring-integratiton-amqp-demo-queue-2 --> AmqpInboundChannelAdapterSMLCSpec
1.3. Spring 配置
spring:
rabbitmq:
addresses: 127.0.0.1:5672
username: admin
password: admin
virtual-host: /
1.4. 定义常量
public static final String QUEUE_1 = "spring-integratiton-amqp-demo-queue-1";
public static final String QUEUE_2 = "spring-integratiton-amqp-demo-queue-2";
public static final String ROUTING_KEY_1 = "routing-key-1";
public static final String ROUTING_KEY_2 = "routing-key-2";
public static final String EXCHANGE = "spring-integratiton-amqp-demo-exchange";
1.5. 定义交换机和队列
@Bean
public Queue queue1() {
return QueueBuilder.durable(QUEUE_1).build();
}
@Bean
public Queue queue2() {
return QueueBuilder.durable(QUEUE_2).build();
}
@Bean
public Exchange exchange() {
return ExchangeBuilder.directExchange(EXCHANGE).durable(true).build();
}
@Bean
public Binding binding1() {
return BindingBuilder.bind(queue1()).to(exchange()).with(ROUTING_KEY_1).noargs();
}
@Bean
public Binding binding2() {
return BindingBuilder.bind(queue2()).to(exchange()).with(ROUTING_KEY_2).noargs();
}
1.6. 消息入站
1.6.1. 定义 Message Channel
@Bean
public DirectChannelSpec inboundMessageChannel1() {
return MessageChannels.direct();
}
@Bean
public DirectChannelSpec inboundMessageChannel2() {
return MessageChannels.direct();
}
1.6.2. 定义 Service Activator
@Bean
@ServiceActivator(inputChannel = "inboundMessageChannel1")
public MessageHandler inboundMessageHandler1() {
return message -> log.info("queue 1 received a message: {}", message);
}
@Bean
@ServiceActivator(inputChannel = "inboundMessageChannel2")
public MessageHandler inboundMessageHandler2() {
return message -> log.info("queue 2 received a message: {}", message);
}
1.6.3. 定义 Message Router
@Bean
public HeaderValueRouter inboundMessageRouter() {
HeaderValueRouter router = new HeaderValueRouter(CONSUMER_QUEUE);
router.setChannelMapping(QUEUE_1, "inboundMessageChannel1");
router.setChannelMapping(QUEUE_2, "inboundMessageChannel2");
return router;
}
1.6.4. 基于 Java DSL 定义 Inbound Channel Adapter
@Bean
public IntegrationFlow inboundIntegrationFlow(ConnectionFactory connectionFactory) {
return IntegrationFlows.from(Amqp.inboundAdapter(connectionFactory, queue1(), queue2()))
.route(inboundMessageRouter())
.get();
}
1.7. 消息出站
1.7.1. 定义 Message Channel
@Bean
public MessageChannelSpec outboundMessageChannel() {
return MessageChannels.direct();
}
1.7.2. 基于 Java DSL 定义 Outbound Channel Adapter
@Bean
public IntegrationFlow outboundIntegrationFlow(AmqpTemplate amqpTemplate) {
return IntegrationFlows.from(outboundMessageChannel())
.handle(
Amqp.outboundAdapter(amqpTemplate)
.exchangeNameFunction(message -> message.getHeaders().getOrDefault(RECEIVED_EXCHANGE, "").toString())
.routingKeyFunction(message -> message.getHeaders().getOrDefault(RECEIVED_ROUTING_KEY, "").toString()))
.get();
}
1.7.3. 定义 Messaging Gateway
@MessagingGateway(defaultRequestChannel = "outboundMessageChannel")
public interface AmqpGateway {
void send2Amqp(@Header(RECEIVED_EXCHANGE) String exchange, @Header(RECEIVED_ROUTING_KEY) String routingKey, @Payload String payload);
}
1.8. 测试
@Component
@Slf4j
public class SpringIntegrationAmqpDemo implements ApplicationRunner {
@Resource
private AmqpConfiguration.AmqpGateway amqpGateway;
@Override
public void run(ApplicationArguments args) throws Exception {
String payload1 = "hello, queue 1";
amqpGateway.send2Amqp(EXCHANGE, ROUTING_KEY_1, payload1);
log.info("send a message, exchange: {}, routing key: {}, payload: {}", EXCHANGE, ROUTING_KEY_1, payload1);
String payload2 = "hello, queue 2";
amqpGateway.send2Amqp(EXCHANGE, ROUTING_KEY_2, payload2);
log.info("send a message, exchange: {}, routing key: {}, payload: {}", EXCHANGE, ROUTING_KEY_2, payload2);
}
}
启动程序,控制台将输出:
send a message, exchange: spring-integratiton-amqp-demo-exchange, routing key: routing-key-1, payload: hello, queue 1
send a message, exchange: spring-integratiton-amqp-demo-exchange, routing key: routing-key-2, payload: hello, queue 2
queue 2 received a message: GenericMessage [payload=hello, queue 2, headers={amqp_receivedDeliveryMode=PERSISTENT, amqp_receivedExchange=spring-integratiton-amqp-demo-exchange, amqp_deliveryTag=1, amqp_consumerQueue=spring-integratiton-amqp-demo-queue-2, amqp_redelivered=false, amqp_receivedRoutingKey=routing-key-2, replyChannel=nullChannel, amqp_contentEncoding=UTF-8, amqp_timestamp=Mon Sep 23 15:03:21 CST 2024, amqp_messageId=d524592f-a483-943d-d6ed-e1dfb6dc1f07, id=e6d1d2b2-ebb9-bd9c-62eb-65ce13d2e6e9, amqp_consumerTag=amq.ctag-Y157TiDiKLQei_-_mjk9Dw, contentType=text/plain, timestamp=1727075001386}]
queue 1 received a message: GenericMessage [payload=hello, queue 1, headers={amqp_receivedDeliveryMode=PERSISTENT, amqp_receivedExchange=spring-integratiton-amqp-demo-exchange, amqp_deliveryTag=1, amqp_consumerQueue=spring-integratiton-amqp-demo-queue-1, amqp_redelivered=false, amqp_receivedRoutingKey=routing-key-1, replyChannel=nullChannel, amqp_contentEncoding=UTF-8, amqp_timestamp=Mon Sep 23 15:03:21 CST 2024, amqp_messageId=fdff4ffb-8c73-9ac0-7bba-ae4055ace969, id=1ccf9615-4cb6-a9bc-a57c-102a4eed77b1, amqp_consumerTag=amq.ctag-Te7ucInXRmWdeQ4e_aPwLw, contentType=text/plain, timestamp=1727075001386}]
本文来自博客园,作者:Jason,转载请注明原文链接:https://www.cnblogs.com/jason207010/p/18426982
浙公网安备 33010602011771号