微服务-消息中间件

springcloud:https://blog.csdn.net/qq_52681418/article/details/113247805

微服务-消息中间件

消息中间件是基于队列与消息传递技术,在网络环境中为应用系统提供同步或异步、可靠的消息传输的支撑性软件系统。

消息中间件有4个部分:

  • Producer:消息生产者,业务的发起方,负责生产消息传输给broker。
  • Consumer:消息消费者,业务的处理方,负责从broker获取消息并进行业务逻辑处理。
  • Broker:消息服务器,作为server提供消息核心服务。
  • Queue:队列,PTP模式下,特定生产者向特定queue发送消息,消费者订阅特定的queue完成指定消息的接收。
  • Topic:主题,发布订阅模式下的消息统一汇集地,不同生产者向topic发送消息,由MQ服务器分发到不同的订阅者,实现消息的广播。
  • Message:消息体,根据不同通信协议定义的固定格式进行编码的数据包,来封装业务数据,实现消息的传输。

ActiveMQ

RabbitMQ

https://blog.csdn.net/qq_52681418/article/details/115078628

RocketMQ

Kafka

Stream整合中间件

全名为spring cloud stream,用于整合各种中间件。

消息生产者将消息通过通道发送到绑定层,绑定层将消息通过通道发送给消息消费者,想切换中间件时,只需修改绑定层中绑定器即可。bingding指定目的地。

发布-订阅者模式: 消息发布后,所有订阅者都能看到。

1.消息发送

为发送消息的服务(生产者)进行配置。

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-stream</artifactId>
</dependency>
<!--生产者-->
<dependency>
   <groupId>org.sshengcpringframework.cloud</groupId>
   <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

生产者配置:

server:
 port: 7001 #服务端口
spring:
 application:
   name: rabbitmq-service #指定服务名
 rabbitmq:
   addresses: 127.0.0.1
   username: itcast
   password: itcast
   virtual-host: myhost
 cloud:
   stream:
     bindings:
       output: #发送通道
         destination: itcast-default #指定消息发送目的地
         #contentType: text/plain #消息类型
     binders:	#配置绑定器
       defaultRabbit:
         type: rabbit
         

编写生产者接口:

//自定义的消息通道
public interface Source {

	//消息生产者、消费者的配置
	String OUTPUT = "output";
	
   //通道
	@Output("output")
	MessageChannel myoutput();

}

消息发送类:

//负责向中间件发送数据
@Component
@EnableBinding(Source.class)
public class MessageSender {

	@Autowired
	@Qualifier(value="output")
	private MessageChannel myoutput;

	//发送消息
	public void send(Object obj) {
		myoutput.send(MessageBuilder.withPayload(obj).build());
	}

}

2.消息接收

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-stream</artifactId>
</dependency>
<!--消费者-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>

配置:

spring:
 cloud:
   stream:
     bindings:
       input: #消费通道
         destination: itcast-default #指定消息发送目的地

编写接口:

public interface Sink {
 String INPUT = "input";
 @Input("input")
 SubscribableChannel input();
}

编写接收类:

@Component
@EnableBinding(Sink.class)
public class MessageAccept{
	// 监听 binding 为 Sink.INPUT 的消息
	@StreamListener(Sink.INPUT)
	public void input(Message<String> message) {
		System.out.println("监听收到:" + message.getPayload());
	}
}


3.自定义通道

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-stream</artifactId>
</dependency>
<!--生产者-->
<dependency>
   <groupId>org.sshengcpringframework.cloud</groupId>
   <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<!--消费者-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>

编写配置:

spring:
	cloud:
		stream:
			defaultBinder: defaultRabbit
		bindings:
			inputOrder:				# 消费者
				destination: mqTestOrder
			outputOrder:			# 生产者
				destination: mqTestOrder

通道接口:

/*自定义的消息通道*/
public interface MyProcessor {

	String MYOUTPUT = "myoutput";
	String MYINPUT = "myinput";

	@Output("myoutput")	//发送通道
	MessageChannel myoutput();
	
	@Input("myinput")	//接收通道
	SubscribableChannel myinput();

}

发送、接收类:

@Component
@EnableBinding(MyProcessor.class)
public class Message{
	//消息发送-----------------------------------------
	@Autowired
	private MessageChannel myoutput;
	public void send(Object msg){
		myoutput.send((MessageBuilder.withPayload(msg).build());
	}
	//消息接收-----------------------------------------
	@StreamListener(MyProcessor.MYINPUT)
	public void input(String msg) {
		System.out.println("获取到消息: "+msg);
	}
}

4.消息分组

默认情况下,在服务启动多个实例时,实例都会绑定到同一个消息通道的目标主题上,每个实例都可以接收,如果不想这样,就需要消息分组。

只需要添加配置:

spring:
 cloud:
  stream:
   bindings:
    input:
      destination: itcast-default
    myinput:
      destination: testChannel
      group: group-2	#分组

5.消息分区

多个相同特征的消息被一个实例接收。

生产者配置:

spring:
 cloud:
   stream:
     bindings:
       output:
         destination: itcast-default
       outputOrder:
         destination: testChannel
         producer:
           partition-key-expression: payload #分区关键字
           partition-count: 2	#分区大小
     binders:
       defaultRabbit:
         type: rabbit

消费者配置:

spring:
 cloud:
   stream:
     instanceCount: 2 #消费者总数
     instanceIndex: 1 #当前索引,0开始
     bindings:
       input:
         destination: itcast-default
       inputOrder:
         destination: testChannel
         group: group-2
         consumer:
           partitioned: true #开启分区支持
     binders:
       defaultRabbit:
         type: rabbit 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值