栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring cloud day(8) stream

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring cloud day(8) stream

Spring cloudday(8) stream
  • 一、消息驱动
    • 1.1 当前问题
    • 1.2 Spring cloud Stream
    • 1.3 input和output
    • 1.4 binder
    • 1.5 stream流程
    • 1.6 常用注解
    • 1.7 项目模块
  • 二、实操
    • 2.1 provider消息驱动生产者
      • 2.1.1 作用
      • 2.1.2 测试消息是否到消息中间件
    • 2.2 消息驱动消费者
    • 2.3 消息重复消费问题
      • 2.3.1 多个消费者
      • 2.3.2 加入分组配置
    • 2.4 消息持久化

一、消息驱动 1.1 当前问题

消息中间件太多了,学习不同消息中间件浪费大量时间,需要一个东西可以再不同场景中切换消息中间件。

1.2 Spring cloud Stream

-只支持rabbitmp和kfaka

1.3 input和output

1.4 binder

1.5 stream流程

1.6 常用注解

1.7 项目模块

二、实操 2.1 provider消息驱动生产者 2.1.1 作用

生产消息发送到消息中间件

  • pom
        
            org.springframework.cloud
            spring-cloud-starter-stream-rabbit
        

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        


        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        


        
        
        
        
        
        
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        


  • yml
server:
  port: 8801

spring:
  application:
    name: cloud-stream-provider
  cloud:
    stream:
      binders: # 在此处配置要绑定的rabbitmq的服务信息;
        defaultRabbit: # 表示定义的名称,用于于binding整合
          type: rabbit # 消息组件类型
          environment: # 设置rabbitmq的相关的环境配置
            spring:
              rabbitmq:
                host: localhost
                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:7001/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地址


注意这里:binders: # 在此处配置要绑定的rabbitmq的服务信息;
defaultRabbit: # 表示定义的名称,用于于binding整合
type: rabbit # 消息组件类型

  • controller
package com.pack.mymodel.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class sendMeesgaController {
    @Resource
    private IMessageProviderService messageProviderService;

    @GetMapping(value = "/sendMessage")
    public String sendMessage() {
        return messageProviderService.send();
    }

}


  • services

接口

public interface IMessageProviderService {
    
    String send();
}

实现类

package com.pack.mymodel.services.Imp;

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;

import javax.annotation.Resource;
import java.util.UUID;




@EnableBinding(Source.class)
public class MessageProviderImp implements IMessageProviderService {
    
    @Resource
    private MessageChannel output;

    @Override
    public String send() {
        String serial = UUID.randomUUID().toString();
        //MessageBuilder.withPayload(serial).build();
        Message stringMessage = MessageBuilder.withPayload(serial).build();
        output.send(stringMessage);

        System.out.println("*****serial: " + serial);
        return serial;
    }
}

  • controller类
package com.pack.mymodel.controller;

import com.pack.mymodel.services.Imp.IMessageProviderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class sendMeesgaController {
    @Resource
    private IMessageProviderService messageProviderService;

    @GetMapping(value = "/sendMessage")
    public String sendMessage() {
        return messageProviderService.send();
    }
}
2.1.2 测试消息是否到消息中间件
  • 启动eureka
  • 启动rabbitmq
  • 启动8001
  • 访问http://localhost:8801/sendMessage
2.2 消息驱动消费者
  • yml
server:
  port: 8802

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:7001/eureka
#  instance:
#    lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
#    lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
#    instance-id: receive-8802.com  # 在信息列表时显示主机名称
#    prefer-ip-address: true     # 访问的路径变为IP地址


  • 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.stereotype.Component;


@Component
@EnableBinding(Sink.class)
public class mycontroller {
    @Value("${server.port}")
    private String serverPort;

    @StreamListener(Sink.INPUT)
    //从消息队列中取出消息
    public void input(Message message) {
        System.out.println("port:" + serverPort + "t接受:" + message.getPayload());
    }

}


  • 主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class bootMAin {
    public static void main(String[] args) {
        SpringApplication.run(bootMAin.class, args);
        System.out.println("启动成功");
    }

}

  • pom



        
            org.springframework.cloud
            spring-cloud-starter-stream-rabbit
        

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        


        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        


        
        
        
        
        
        
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    
2.3 消息重复消费问题 2.3.1 多个消费者

当多个消费者存在的时候,每个消费者都会消费消息,造成消息的重复消费。

  • 原因
    不同的微服务默认在不同的组,组流水号不一样,所以只需要分到同一个组即可

2.3.2 加入分组配置

  • 8802,8803设置成同一个组
2.4 消息持久化
  • 消费者不配置group,就不会对消息队列的消息进行消费,后台就不会输出消费记录。
  • 配置了group的消费者,重启会消费未被消费的消息。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995737.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号