java中channelmessage,spring-integration – 如何使用Spring Cloud Stream MessageChannels配置@MessagingGateway?...

这是个好问题,也是个好主意.但它不会那么容易.

首先,我们必须自己确定网关意味着请求/回复,因此相关性.这可以在@MessagingGateway中通过面向TemporaryReplyChannel实例的replyChannel头文件获得.即使您有明确的replyChannel = AccountChannels.ACCOUNT_CREATED,也只能通过提到的标头及其值进行关联.事实上,此TemporaryReplyChannel不可序列化,无法通过网络传输到另一方的消费者.

幸运的是,Spring Integration为我们提供了一些解决方案.它是HeaderEnricher及其HeaderChannelRegistry后面的headerChannelsToString选项的一部分:

Starting with Spring Integration 3.0, a new sub-element is available; it has no attributes. This converts existing replyChannel and errorChannel headers (when they are a MessageChannel) to a String and stores the channel(s) in a registry for later resolution when it is time to send a reply, or handle an error. This is useful for cases where the headers might be lost; for example when serializing a message into a message store or when transporting the message over JMS. If the header does not already exist, or it is not a MessageChannel, no changes are made.

但在这种情况下,您必须引入从网关到HeaderEnricher的内部通道,只有最后一个通道会将消息发送到AccountChannels.CREATE_ACCOUNT_REQUEST.因此,replyChannel标头将转换为字符串表示,并能够通过网络传输.在消费者方面,当您发送回复时,您应该确保转移该replyChannel标头,就像它一样.因此,当消息将到达生产者端的AccountChannels.ACCOUNT_CREATED时,我们有@MessagingGateway,相关机制能够将信道标识符转换为适当的TemporaryReplyChannel,并将回复与等待网关调用相关联.

这里只有您的生产者应用程序必须作为AccountChannels.ACCOUNT_CREATED组中的单个使用者的问题 – 我们必须确保云中只有一个实例一次运行.仅仅因为只有一个实例在其内存中具有TemporaryReplyChannel.

UPDATE

一些帮助代码:

@EnableBinding(AccountChannels.class)

@MessagingGateway

public interface AccountService {

@Gateway(requestChannel = AccountChannels.INTERNAL_CREATE_ACCOUNT_REQUEST, replyChannel = AccountChannels.ACCOUNT_CREATED, replyTimeout = 60000, requestTimeout = 60000)

Account createAccount(@Payload Account account, @Header("Authorization") String authorization);

}

@Bean

public IntegrationFlow headerEnricherFlow() {

return IntegrationFlows.from(AccountChannels.INTERNAL_CREATE_ACCOUNT_REQUEST)

.enrichHeaders(headerEnricher -> headerEnricher.headerChannelsToString())

.channel(AccountChannels.CREATE_ACCOUNT_REQUEST)

.get();

}

UPDATE

一些简单的应用程序来演示PoC:

@EnableBinding({ Processor.class, CloudStreamGatewayApplication.GatewayChannels.class })

@SpringBootApplication

public class CloudStreamGatewayApplication {

interface GatewayChannels {

String REQUEST = "request";

@Output(REQUEST)

MessageChannel request();

String REPLY = "reply";

@Input(REPLY)

SubscribableChannel reply();

}

private static final String ENRICH = "enrich";

@MessagingGateway

public interface StreamGateway {

@Gateway(requestChannel = ENRICH, replyChannel = GatewayChannels.REPLY)

String process(String payload);

}

@Bean

public IntegrationFlow headerEnricherFlow() {

return IntegrationFlows.from(ENRICH)

.enrichHeaders(HeaderEnricherSpec::headerChannelsToString)

.channel(GatewayChannels.REQUEST)

.get();

}

@StreamListener(Processor.INPUT)

@SendTo(Processor.OUTPUT)

public Message> process(Message request) {

return MessageBuilder.withPayload(request.getPayload().toUpperCase())

.copyHeaders(request.getHeaders())

.build();

}

public static void main(String[] args) {

ConfigurableApplicationContext applicationContext =

SpringApplication.run(CloudStreamGatewayApplication.class, args);

StreamGateway gateway = applicationContext.getBean(StreamGateway.class);

String result = gateway.process("foo");

System.out.println(result);

}

}

application.yml:

spring:

cloud:

stream:

bindings:

input:

destination: requests

output:

destination: replies

request:

destination: requests

reply:

destination: replies

我使用spring-cloud-starter-stream-rabbit.

MessageBuilder.withPayload(request.getPayload().toUpperCase())

.copyHeaders(request.getHeaders())

.build()

技巧是否将请求标头复制到回复消息.因此,网关能够在回复侧将报头中的信道标识符转换为适当的TemporaryReplyChannel,以将回复正确地传送给网关的呼叫者.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Stream 是一个构建消息驱动微服务的框架,它基于 Spring Boot 和 Spring Integration,通过简单声明式模型来实现消息的发送和接收。Spring Cloud Stream 可以与多种消息间件集成,如 RabbitMQ、Kafka 等。 使用 Spring Cloud Stream 的步骤如下: 1. 添加依赖 在 pom.xml 添加 Spring Cloud Stream 相关的依赖,如下: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream</artifactId> </dependency> ``` 2. 创建消息处理器 创建一个消息处理器,用于接收和处理消息,例如: ``` @Component public class MessageHandler { @StreamListener(Sink.INPUT) public void handleMessage(String message) { System.out.println("Received message: " + message); } } ``` 这个消息处理器使用 `@StreamListener` 注解来监听 `Sink.INPUT`,表示接收来自输入通道的消息,并打印出来。 3. 配置绑定器 在配置文件配置绑定器,用于将应用程序与消息间件连接起来。例如,如果要使用 RabbitMQ,可以这样配置: ``` spring.cloud.stream.bindings.input.destination=myQueue spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 这里配置了输入通道的目的地为 `myQueue`,以及 RabbitMQ 的连接信息。 4. 发送消息 使用 `MessageChannel` 来发送消息,例如: ``` @Autowired private MessageChannel output; public void sendMessage(String message) { output.send(MessageBuilder.withPayload(message).build()); } ``` 这里注入了一个 `output` 的 `MessageChannel`,通过它发送消息,例如: ``` sendMessage("Hello, world!"); ``` 这样就可以将一条消息发送到 `myQueue` 队列。 以上就是使用 Spring Cloud Stream 的基本步骤。需要注意的是,消息的发送和接收是基于通道(Channel)的,而通道是由绑定器(Binder)来创建和管理的。因此需要在配置文件配置绑定器相关的信息,才能正常发送和接收消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值