Article Directory
- 1 environment
- 2 Introduction
- 3 shown signs
- 1 Create a project to add web rabbitmq stream dependent
- 2 rabbitmq Configuration
- Message receiver 3
- 4 sends a message in the rabbitmq
- 5 View Results
- Custom Message Channel 4
- 1 Custom Interface
- 2 Custom Recipient
- 3 controller testing
- 4 the input and output message (channel docked)
- 5 start, visit
- 5 message packet
- 6 news partition
- 7 Timer
- 8 Summary
1 environment
- System environment: win10
- Editor: IDEA
- springcloud:H版
- Installation rabbitmq article reference
2 Introduction
Spring Cloud Stream is a framework for building micro-service message-driven application, which provides a series of abstract shield differences use different types of messaging middleware, but also greatly simplifies the integration Spring use in complex messaging middleware degree.
Spring Cloud Stream provides Binder (responsible for interacting with the messaging middleware)
3 shown signs
1 Create a project to add web rabbitmq stream dependent
2 rabbitmq Configuration
# 其他参数默认配置
spring.rabbitmq.host=你的host
Message receiver 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 sends a message in the rabbitmq
5 View Results
Custom Message Channel 4
1 Custom Interface
public interface MyChannel {
String INPUT = "test-input";
String OUTPUT = "test-output";
// 收
@Input(INPUT)
SubscribableChannel input();
// 发
@Output(OUTPUT)
MessageChannel output();
}
2 Custom Recipient
// 绑定自定义消息通道
@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 controller testing
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 the input and output message (channel docked)
spring.cloud.stream.bindings.test-input.destination=test-topic
spring.cloud.stream.bindings.test-output.destination=test-topic
5 start, visit
5 message packet
- Message packet (fertilizer does not stay outsiders fields you might not know Which flow field but is really one of us)
1 package access (message packet is not used)
- Start
java -jar stream-0.0.1-SNAPSHOT.jar
and
java -jar stream-0.0.1-SNAPSHOT.jar --server.port=8081
run accesshttp://localhost:8080/hello
- The results (repeated consumption)
- Now I do not want a message is repeated consumption (assuming that the consumer is a cluster -> multiple people doing the same thing digression: Distributed -> One thing given to more than one person to do) Is there any way to do
a message packet to help we solve (specify the input and output have it taste load balancing)
Configuration message packet 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
- In order to verify whether it can successfully run and repackaged as above Access Interface
- Information emptied two console again access interface
6 news partition
- It is to consume the same number of message consumer example is provided with the same characteristics of each.
1 news partition configuration
- Configuration properties
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 controller configuration
@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 Access
- Packaging run
java -jar stream-0.0.1-SNAPSHOT.jar --spring.cloud.stream.instance-index=0
andjava -jar stream-0.0.1-SNAPSHOT.jar --server.port=8081 --spring.cloud.stream.instance-index=0
(do not forget to turn off the startup class will complain otherwise packaged)
- visit http: // localhost: 8080 / hello
4 If the random access it
@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());
}
- Same as above package visit
7 Timer
Although the scheduled tasks can but for some special timing tasks can use stream + rabbitmq more appropriate use cron expression such as the implementation of a few minutes later
rabbitmq plug-in installed
1 configuration
- properties
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
- Custom channel
// 绑定自定义消息通道
@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());
}
}
- controller
@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 Start Access
- Open rabbitmq View
- View idea Console
8 Summary
comes with a custom stream (added destination = xxx) and a similar distinction between the
settlement (group) repeated consumption packet
message packet access to a single instance (Examples consumption open partition producer index number of instances of nodes arranged consumption)
Timer rabbitmq relevant plug mounted rear running code implements (delayed-exchange configuration and destination, and add setHeader ( "x-delay" when transmission controller, 3000) 3s delay)