必要性応答メッセージのためにプログラマリファレンスDDギャングの記事、彼らの新しいデモを学び、@SendToこの注釈は、レッツ・スタート学習デモを達成することができます参照して、二つの新しいプロジェクトcloud-stream-consumer
消費者側とcloud-stream-consumer
生産側
public interface StreamReceive {
@Input("MQRece")
SubscribableChannel mqReceive();
}
追加StreamReceive
定義するインターフェース@input
チャネルを
@Component
@Slf4j
public class ReceiveListener {
@StreamListener("MQRece")
public byte[] receive(byte[] bytes){
log.info("接受消息:"+new String(bytes));
return "ok".getBytes();
}
}
定義されたメッセージを受信するメッセージリスナーを追加します。byte[]
追加application.properties
のプロファイル情報を
spring.cloud.stream.rocketmq.binder.namesrv-addr= 192.168.211.11:9876
spring.cloud.stream.bindings.MQRece.destination=message-topic
spring.cloud.stream.bindings.MQRece.group=rece-group
server.port=19999
テーマMQReceチャンネルを追加しmessage-topic
、グループ名rece-group
このクライアントの消費量は、彼らと@SendToノートに、このセクションの必要性完了しているストリームMessageChannel生成するニュースを作成する必要
public interface MsgBackPush {
@Output("back-push")
MessageChannel backPush();
}
その後、ReceiveListener
追加@SendTo
@Component
@Slf4j
public class ReceiveListener {
@StreamListener("MQRece")
@SendTo("back-push")
public byte[] receive(byte[] bytes){
log.info("接受消息:"+new String(bytes));
return "ok".getBytes();
}
}
新しいチャンネルの設定application.properties
spring.cloud.stream.bindings.back-push.destination=back-topic
spring.cloud.stream.bindings.back-push.group=back-group
SpringBootを開始するためにクラスを追加することを忘れないでください @EnableBinding(value = {StreamReceive.class,MsgBackPush.class})
@SpringBootApplication
@EnableBinding(value = {StreamReceive.class,MsgBackPush.class})
public class CloudStreamConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudStreamConsumerApplication.class, args);
}
}
このデモは完了です。この、クラウドストリームの消費者
そして、見てみましょう cloud-stream-producer
public interface StreamPush {
@Output("MQPush")
MessageChannel mqPush();
}
チャンネル名を定義しMQPush
、メッセージが生成しました
public interface ProducerReceive {
@Input("producer-receive")
SubscribableChannel producerReceive();
}
呼ばれるチャネルを定義しproducer-receive
、受信メッセージの消費を
@Component
@Slf4j
public class ProducerListener {
@StreamListener("producer-receive")
public void producerReceive(byte[] bytes){
log.info("come back message:"+new String(bytes));
}
}
ロジックを扱う詳細な領収書メッセージ、見application.properties
spring.cloud.stream.rocketmq.binder.namesrv-addr= 192.168.214.191:9876
spring.cloud.stream.bindings.MQPush.destination=message-topic
spring.cloud.stream.bindings.MQPush.group=push-group
spring.cloud.stream.bindings.producer-receive.destination=back-topic
spring.cloud.stream.bindings.producer-receive.group=back-group
server.port=20000
チャンネルやグループに設定トピックはHTTPの結果をテストするための新しいインターフェイスを作成します
@SpringBootApplication
@EnableBinding(value = {StreamPush.class,ProducerReceive.class})
@RestController
public class CloudStreamProducerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudStreamProducerApplication.class, args);
}
@Autowired
private StreamPush streamPush;
@GetMapping("/sendMessage")
public String sendMessage(){
streamPush.mqPush().send(MessageBuilder.withPayload("message body".getBytes()).build());
return "ok";
}
}
アクセスhttp://localhost:20000/sendMessage
、図の次の結果。
クラウド・ストリーム・消費者のログ出力
クラウド・ストリーム・プロデューサーの出力ログ
@ServiceActivatorこの注釈を学び、上記プロジェクトcloud-stream-consumer
ReceiveListenerクラスが追加します
@Component
@Slf4j
public class ReceiveListener {
@StreamListener("MQRece")
@SendTo("back-push")
public byte[] receive(byte[] bytes){
log.info("接受消息:"+new String(bytes));
// 抛出异常
if(1==1){
throw new RuntimeException("Message consumer failed!");
}
return "ok".getBytes();
}
@Autowired
private MsgBackPush msgBackPush;
@ServiceActivator(inputChannel = "message-topic.rece-group.errors")
public void error(Message<?> message){
log.info("消费者消费消息失败:"+message);
msgBackPush.backPush().send(MessageBuilder.withPayload("消息消费失败".getBytes()).build());
}
}
@ServiceActivator(inputChannel =「テストtopic.stream-例外handler.errors」)ハンドルマッピングエラーを使用して指定チャンネル。以下の通りであり、inputChannel設定の対応は次のとおりです。
- メッセージトピック:ターゲットに対応するメッセージ・チャネル(宛先、すなわち:spring.cloud.stream.bindings.MQRece.destination構成)
- RECEグループ:消費者グループに対応するメッセージ・チャネル(群、すなわち:spring.cloud.stream.bindings.MQRece.group構成)
アクセスhttp://localhost:20000/sendMessage
、図の次の結果。
クラウド・ストリーム・消費者のログ出力
クラウド・ストリーム・プロデューサーの出力ログ