記事のディレクトリ
1つの環境
- システム環境:win10
- 編集者:IDEA
- springcloud:H版
- インストールRabbitMQの記事参照
2はじめに
スプリングクラウドストリーム抽象シールド違い使用ミドルウェアメッセージングの異なるタイプのシリーズを提供するマイクロサービスのメッセージ駆動型アプリケーションを構築するためのフレームワークであるが、非常に複雑なメッセージング・ミドルウェアの積分スプリングの使用を簡素化程度。
春クラウドストリーム(メッセージングミドルウェアとの相互作用の責任)バインダーを提供します
3つのに示す兆候
1依存ウェブRabbitMQのストリームを追加するプロジェクトを作成します。
2 RabbitMQの設定
# 其他参数默认配置
spring.rabbitmq.host=你的host
メッセージ受信機3
// 该注解表示绑定Sink消息通道
@EnableBinding(Sink.class)
public class MsgReceiver {
private static final Logger logger = LoggerFactory.getLogger(MsgReceiver.class);
// 自带 消费者
@StreamListener(Sink.INPUT)
public void receive(Object payload){
logger.info("received: " + payload);
}
}
図4は、RabbitMQの中でメッセージを送信します
5つの結果の表示
カスタムメッセージチャンネル4
1カスタムインターフェース
public interface MyChannel {
String INPUT = "test-input";
String OUTPUT = "test-output";
// 收
@Input(INPUT)
SubscribableChannel input();
// 发
@Output(OUTPUT)
MessageChannel output();
}
2カスタム受信者
// 绑定自定义消息通道
@EnableBinding(MyChannel.class)
public class MsgReceiver1 {
private static final Logger logger = LoggerFactory.getLogger(MsgReceiver1.class);
// 收
@StreamListener(MyChannel.INPUT)
public void receive(Object payload){
logger.info("received1: " + payload + ":" + new Date());
}
}
3コントローラのテスト
package com.sundown.stream.controller;
import com.sundown.stream.bean.ChatMessage;
import com.sundown.stream.msg.MyChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
@RestController
public class HelloController {
@Autowired
MyChannel myChannel;
@GetMapping("/hello")
public void hello(){
String message = "welcome spring cloud stream";
myChannel.output().send(MessageBuilder.withPayload(message).build());
}
}
4入力と出力メッセージ(チャネルドッキング)
spring.cloud.stream.bindings.test-input.destination=test-topic
spring.cloud.stream.bindings.test-output.destination=test-topic
5スタート、訪問
5メッセージパケット
- メッセージパケット(部外者にフィールドを流していますが知らないかもしれないフィールドが滞在していません肥料は本当に私たちの一つです)
1パッケージアクセス(メッセージ・パケットが使用されていません)
- スタート
java -jar stream-0.0.1-SNAPSHOT.jar
と
java -jar stream-0.0.1-SNAPSHOT.jar --server.port=8081
実行アクセスhttp://localhost:8080/hello
- 結果(繰り返し消費)
- 今、私はメッセージが繰り返され、消費したくない(消費者がクラスタであると仮定すると- >複数の人が同じことを余談やって:分散-行うには、複数の人に与えられた>ことの一つ)を行うための任意の方法があり
、ヘルプにメッセージパケットは、我々は解決(それが負荷分散を味わっていた入力と出力を指定します)
コンフィギュレーション・メッセージパケット2
spring.cloud.stream.bindings.test-input.destination=test-topic
spring.cloud.stream.bindings.test-output.destination=test-topic
spring.cloud.stream.bindings.test-input.group=gg
spring.cloud.stream.bindings.test-output.group=gg
- それが正常に実行し、アクセスインターフェイス上記のように再パッケージ化できるかどうかを検証するために
- 情報は、アクセスインタフェース2つのコンソール再び空
6ニュースのパーティション
- これは、消費者の例は、それぞれの同じ特性を備えているメッセージの同じ数を消費することです。
1ニュースのパーティション構成
- 構成プロパティ
spring.cloud.stream.bindings.test-input.destination=test-topic
spring.cloud.stream.bindings.test-output.destination=test-topic
spring.cloud.stream.bindings.test-input.group=gg
spring.cloud.stream.bindings.test-output.group=gg
# 开启消费分区(消费者上配置)
spring.cloud.stream.bindings.test-input.consumer.partitioned=true
# 消费者实例个数(消费者上配置)
spring.cloud.stream.instance-count=2
# 当前实例下标(消费者上配置)
spring.cloud.stream.instance-index=0
2コントローラ構成
@RestController
public class HelloController {
@Autowired
MyChannel myChannel;
@GetMapping("/hello")
public void hello(){
String message = "welcome spring cloud stream";
// 先写死
int whichPart = 1;
System.out.println("发送消息:" + message + ",发往分区:" + whichPart);
myChannel.output().send(MessageBuilder.withPayload(message).setHeader("whichPart", whichPart).build());
}
}
3アクセス
- 包装実行
java -jar stream-0.0.1-SNAPSHOT.jar --spring.cloud.stream.instance-index=0
してjava -jar stream-0.0.1-SNAPSHOT.jar --server.port=8081 --spring.cloud.stream.instance-index=0
(起動クラスがパッケージ化さそう文句を言うでしょうオフにすることを忘れないでください)
-訪問にhttp:// localhost:8080 /ハロー
4もしランダムアクセスも
@GetMapping("/hello")
public void hello(){
String message = "welcome spring cloud stream";
int whichPart = new Random().nextInt(2);
System.out.println("发送消息:" + message + ",发往分区:" + whichPart);
myChannel.output().send(MessageBuilder.withPayload(message).setHeader("whichPart", whichPart).build());
}
- パッケージ訪問上記と同じ
7タイマー
いくつかの特別なタイミングタスクは、数分後の実装としてストリーム+のRabbitMQのより適切な使用のcron式を使用することができますのためにスケジュールされたタスクはなく、することができますが
RabbitMQのプラグインをインストール
1つの構成
- プロパティ
spring.rabbitmq.host=xxx
spring.cloud.stream.bindings.test-input.destination=topic
spring.cloud.stream.bindings.test-output.destination=topic
spring.cloud.stream.rabbit.bindings.test-input.consumer.delayed-exchange=true
spring.cloud.stream.rabbit.bindings.test-output.producer.delayed-exchange=true
#spring.cloud.stream.bindings.test-input.destination=test-topic
#spring.cloud.stream.bindings.test-output.destination=test-topic
#
#spring.cloud.stream.bindings.test-input.group=gg
#spring.cloud.stream.bindings.test-output.group=gg
#
## 开启消费分区(消费者上配置)
#spring.cloud.stream.bindings.test-input.consumer.partitioned=true
## 消费者实例个数(消费者上配置)
#spring.cloud.stream.instance-count=2
## 当前实例下标(消费者上配置)
#spring.cloud.stream.instance-index=0
#
## 生产者配置
#spring.cloud.stream.bindings.test-output.producer.partition-key-expression=headers['whichPart']
## 消费节点数量
#spring.cloud.stream.bindings.test-output.producer.partition-count=2
- カスタムチャネル
// 绑定自定义消息通道
@EnableBinding(MyChannel.class)
public class MsgReceiver1 {
private static final Logger logger = LoggerFactory.getLogger(MsgReceiver1.class);
// 收
@StreamListener(MyChannel.INPUT)
public void receive(Object payload){
// 添加日期 一会好对比
logger.info("received1: " + payload + ":" + new Date());
}
}
- コントローラ
@RestController
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Autowired
MyChannel myChannel;
@GetMapping("/delay")
public void delay(){
String message = "welcome spring cloud stream";
logger.info("send msg:" + new Date());
// x-delay --> 延迟3s
myChannel.output().send(MessageBuilder.withPayload(message).setHeader("x-delay", 3000).build());
}
}
2 Accessを起動
- オープンのRabbitMQ見ます
- ビューアイデアコンソール
8まとめ
カスタム・ストリームが付属して(追加先= XXX)との間で同様の区別
決済(群)を繰り返す消費パケットの
単一のインスタンスにメッセージ・パケット・アクセス(ノードのインスタンスの例消費オープンパーティションプロデューサインデックス番号は、消費を配置)
タイマRabbitMQの関連するプラグは、後方走行コードが実装搭載(遅延交換構成と目的地を、いつ送信制御部、3000(「X-遅延」のsetHeaderを追加)3S遅延)