SpringCloud-Stream-消息驱动

简介:

SpringCloud-Stream 官方定义SpringCloud-Stream是一个构建消息驱动的微服务框架,应用程序通过,inputs 或者ouputs来与SpringCloud-Stream的binder对象负责与消息中间件交互,所以只需知道怎么和SpringCloud-Stream交互就可以方便的使用消息驱动的方式、通过使用 SpringCloud-Stream来连接信息代理中间件以实现消息驱动,SpringCloud-Stream为一些应用商的消息中间件,提供了个性化的自动化配置实现 ,引用可发发布,订阅,消费组,分区的三个核心概念,现在只支持 RabbitMQ Kafka

SpringCloud-Stream中的消息通信方式遵循了发布 订阅 模式

binder:很方便的中间件,屏蔽差异
Channel: 通道是 队列Queue的抽象,在消息通讯系统中就是实现存储和转发的媒介,通过对Channel队列进行匹配
Source和Sink:简单可以理解是SpringCloud-Stream自身,从Stream发布消息就是输出,接受消息就是输入

在这里插入图片描述
在这里插入图片描述
常用的注解
在这里插入图片描述
示例:
创建生产着:(使用RabbitMQ )
StreamMQMain8086

pom依赖

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>

yml

server:
  port: 8086


spring:
  application:
    name: cloud-stream-provider
  cloud:
    stream:
      binders: # 在此处配置要绑定的rabbitmq的服务信息;
        defaultRabbit: # 表示定义的名称,用于于binding整合
          type: rabbit # 消息组件类型
          environment: # 设置rabbitmq的相关的环境配置
            spring:
              rabbitmq:
                host: 123.57.90.11
                port: 5672
                username: guest
                password: guest
      bindings: # 服务的整合处理
        output: # 这个名字是一个通道的名称
          destination: studyExchange # 表示要使用的Exchange名称定义
          content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
          binder: defaultRabbit  # 设置要绑定的消息服务的具体设置

eureka:
  client: # 客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:9001/eureka,http://127.0.0.1:9002/eureka
  instance:
    lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
    lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
    instance-id: send-8801.com  # 在信息列表时显示主机名称
    prefer-ip-address: true     # 访问的路径变为IP地址

service

public interface ImesageProvider {


    public  String test();
}

impl注解 @EnableBinding(Source.class) /定义消息的推送管道

package com.tang.cloud.sevice.impl;

import com.tang.cloud.sevice.ImesageProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@EnableBinding(Source.class)
public class ImesageProviderImpl implements ImesageProvider {

    @Autowired
   private MessageChannel output;
    @Override
    public String test() {
         String t="测试";
        output.send(MessageBuilder.withPayload(t).build());
        return "测试成功";
    }
}

controller 以前现在怎么写
在这里插入图片描述

启动测试
在这里插入图片描述
在这里插入图片描述
消费者:
pom

      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>

yml
配置文件盒服务者不同的是 input

server:
  port: 8087


spring:
  application:
    name: cloud-stream-consumer
  cloud:
    stream:
      binders: # 在此处配置要绑定的rabbitmq的服务信息;
        defaultRabbit: # 表示定义的名称,用于于binding整合
          type: rabbit # 消息组件类型
          environment: # 设置rabbitmq的相关的环境配置
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
      bindings: # 服务的整合处理
        input : # 这个名字是一个通道的名称
          destination: studyExchange # 表示要使用的Exchange名称定义
          content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
          binder: defaultRabbit  # 设置要绑定的消息服务的具体设置
eureka:
  client: # 客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:9001/eureka,http://127.0.0.1:9002/eureka
  instance:
    lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
    lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
    instance-id: send-8801.com  # 在信息列表时显示主机名称
    prefer-ip-address: true     # 访问的路径变为IP地址

controller

package com.tang.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableBinding(Sink.class)
public class ReceiveMessageListenerController {

    @StreamListener(Sink.INPUT)
    public void input(Message<String> message) {
        System.out.println(message.getPayload());
    }


}

启动项目访问
服务端端发请求
在这里插入图片描述
客户端鞥接受的到

在这里插入图片描述
mq
在这里插入图片描述重复消费 可以用Stream中的消息分组解决,不同组是全面消费 重复消费
同一个组内会发生竞争关系,只有其中一个会消费
解决在 yml 配置分组group: 也可解决持久化的问题,避免消息丢失。

    group: a 把 消费者 都配置一个组就可以不被重复消费
server:
  port: 8088


spring:
  application:
    name: cloud-stream-consumer
  cloud:
    stream:
      binders: # 在此处配置要绑定的rabbitmq的服务信息;
        defaultRabbit: # 表示定义的名称,用于于binding整合
          type: rabbit # 消息组件类型
          environment: # 设置rabbitmq的相关的环境配置
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
      bindings: # 服务的整合处理
        input : # 这个名字是一个通道的名称
          destination: studyExchange # 表示要使用的Exchange名称定义
          content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
          binder: defaultRabbit  # 设置要绑定的消息服务的具体设置
          group: a
eureka:
  client: # 客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:9001/eureka,http://127.0.0.1:9002/eureka
  instance:
    lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
    lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
    instance-id: send-8088.com  # 在信息列表时显示主机名称
    prefer-ip-address: true     # 访问的路径变为IP地址

评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

  T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值