相关文章推荐
睿智的松鼠  ·  HTTP客户端之Spring ...·  1 月前    · 
飞奔的丝瓜  ·  关于解决 错误: ...·  7 月前    · 
无聊的柚子  ·  svg ...·  2 年前    · 
温柔的墨镜  ·  .NET Core 3.0 ...·  2 年前    · 
bindings: #服务的整合处理 myChannel-out-0: #这个名字是一个通道的名字 destination: studyExchange #表示要使用Exchange名称定义 contentType: application/json #设置消息类型,本次为json,文本则设置为“text/plain”

注意bingdings 集合中的key由 通道名-out/in-数字组成

service层代码:

package com.jx.springCloud.service.impl;
import com.jx.springCloud.service.MessageProvider;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.stereotype.Service;
import java.util.UUID;
 * @author LDW
 * @date 2022/4/10 20:35
@Service
public class MessageProviderImpl implements MessageProvider {
    private final StreamBridge streamBridge;
    public MessageProviderImpl(StreamBridge streamBridge) {
        this.streamBridge = streamBridge;
    @Override
    public String send() {
        String serial = UUID.randomUUID().toString();
        streamBridge.send("myChannel-out-0", MessageBuilder.withPayload(serial).build());
        System.out.println("发送消息: " + serial);
        return null;

@Autowire注解自动注入StreamBridge的实例,上述代码的写法省去了@Autowire注解

StreamBridge的send方法第一个参数是binding的名字,第二个参数是想要发送的消息

controller层代码:

@RestController
public class SendMessageController {
    private final MessageProvider messageProvider;
    public SendMessageController(MessageProvider messageProvider) {
        this.messageProvider = messageProvider;
    @GetMapping("/sendMessage")
    public String sendMessage() {
        return messageProvider.send();

消费者案例:

yml配置:

server:
  port: 8802
spring:
  application:
    name: cloud-stream-consumer
  rabbitmq:
    host: 192.168.220.101
    port: 5672
    username: guest
    password: guest
  cloud:
    stream:
      bindings: #服务的整合处理
        myChannel-in-0: #这个名字是一个通道的名字
          destination: studyExchange #表示要使用Exchange名称定义
          contentType: application/json #设置消息类型,本次为json,文本则设置为“text/plain”

service层代码:

@Service
@Slf4j
public class StreamConsumerService {
    @Bean
    public Consumer<String> myChannel() {
        return message -> log.info("消息:"+message);

上述代码的方法名(即Consumer的bean实例名)需要是yml配置中的通道名,应用程序启动后会自动接收生产者发送的消息

发送请求:localhost:8801/sendMessage

@EnableBinding源码中明确声明该注解在从3.1版本开始被弃用,推荐我们使用函数编程的方式我将给出一个生产者和消费者的使用案例:生产者案例:yml配置:server: port: 8801spring: application: name: cloud-stream-provider rabbitmq: host: 192.168.220.101 port: 5672 username: guest password:
Spring Cloud Stream 3.1中@EnableBinding已经被做了@deprecated的标注。那我们原有的程序如何适应新版本呢,确实Spring Cloud Stream在这方面做了很大的改进。 Spring现在不再使用基于注释的配置,而是使用检测到的Consumer / Function / Supplier的Bean来定义流。 较旧的版本带有注释的代码如下所示: interface InputChannels { @Input("input") Subscribabl
这又是学习尚硅谷 spring cloud中遇到的一个坑 因为之前有一个 所以这里索引从2开始 2、spring cloud stream 3.1 @StreamListener 官方不建议使用了 在使用stream 整合rabbitMQ的时候 突然发现新版本不建议使用@Binding(Source.class) 、@StreamListener(Sink.class) 查看了 [官方文档][https://docs.spring.io/spring-cloud-stream/docs/3.1.0 本节介绍Spring Cloud Stream的编程模型。Spring Cloud Stream提供了许多预定义的注释,用于声明绑定的输入和输出通道,以及如何收听频道。 声明和绑定频道 触发绑定@EnableBinding 您可以将Spring应用程序转换为Spring Cloud Stream应用程序,将@EnableBinding注释应用于应用程序的配置类之一。@EnableBind...
本文内容翻译自官方文档,spring-cloud-stream docs,对 Spring Cloud Stream的应用入门介绍。 一、Spring Cloud Stream 简介 官方定义 Spring Cloud Stream 是一个构建消息驱动微服务的框架。 Spring Cloud Stream构建在SpringBoot之上,提供了Kafka,RabbitMQ等消息中间件的个性化...
1、引入 springcloud Stream springcloud Stream 是一个可以用于构建消息驱动服务的框架 为了能够使你的应用连到一个message broker,你可以添加一个 @EnableBinding 注解到你的应用中,然后,你可以添加 @StreamListener 来监听接收事件在你的Stream进程里面。看看下面这个例子 @SpringBootApplication
Redis是一种内存数据存储系统,而Spring Cloud Stream是一个构建消息驱动微服务应用的框架。将Redis和Spring Cloud Stream结合起来,可以实现快速高效的消息传递和数据存储。 具体来说,可以使用Redis作为Spring Cloud Stream中消息中间件的存储介质,从而提高系统的性能和可伸缩性。此外,还可以使用Redis的数据结构来存储应用程序的状态和缓存数据,这有助于提高应用程序的响应速度和可用性。 Spring Cloud Stream提供了对Redis的集成支持,可以通过配置连接工厂等参数来连接Redis实例。使用Spring Cloud Stream提供的注解和接口可以轻松实现消息的生产和消费,同时利用Redis提供的数据结构,还可以实现更复杂的数据操作和查询。 总之,Redis与Spring Cloud Stream的整合可以让微服务应用变得更加高效和可靠,提高应用程序的性能和可伸缩性。
Shiro整合Redis出现异常 org.apache.shiro.authc.SimpleAuthenticationInfo cannot be cast to org.apache.shiro