• spring cloud 快速上手系列 -> 03-消息队列 Stream -> 035-发送消息


    spring cloud 快速上手系列

    系列说明:快速上手,一切从简,搭建一个简单的微服务框架,让新手可以在这个基础框架上做各种学习、研究。

    03-消息队列 Stream

    035-发送消息

    1,说明

    现在,我们关闭配置中心,自行发送消息。利用前一章的微服务收获我们发送的消息。

    2,StreamProvider
    1) 代码目录

    在这里插入图片描述

    2) 代码内容
    • pom.xml
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.3version>
            <relativePath/>
        parent>
        <groupId>com.hui.study.cloudgroupId>
        <artifactId>StudyStreamProviderartifactId>
        <version>1.0.0-SNAPSHOTversion>
        <properties>
            <java.version>1.8java.version>
            <spring-cloud.version>2021.0.4spring-cloud.version>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>${spring-cloud.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-stream-rabbitartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    没有新加依赖,就以前的依赖包。

    • application.yml
    server:
      port: 5002  #Stream-Provider的端口号
    spring:
      application:
        name: stream-provider  # 应用名称
      cloud:
        stream:
          binders: # 在此处配置要绑定的rabbitmq的服务信息;
            defaultRabbit: # 表示定义的名称,用于于binding整合
              type: rabbit # 消息组件类型
              environment: # 设置rabbitmq的相关的环境配置
                spring:
                  rabbitmq:
                    host: localhost
                    port: 5672
                    username: guest
                    password: guest
          bindings: # 服务的整合处理
            cloudBus-out-0: # 一个通道的名称
              destination: springCloudBus     # 表示要使用的exchange名称定义
              content-type: application/json  # 设置消息类型,本次为json,文本则设为text/plain
    eureka:
      client:
        #表示是否将自己注册进EurekaServer
        register-with-eureka: true
        #是否从EurekaServer抓取已有的注册信息,默认为true。
        fetchRegistry: true
        service-url:
          #服务中心地址
          defaultZone: http://localhost:7001/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • SendMessageController.java
    package com.hui.study.cloud.stream.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.stream.function.StreamBridge;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @RestController
    public class SendMessageController {
        /**
         * 消息发送管道
         */
        @Autowired
        private StreamBridge streamBridge;
    
        @PostMapping("/send")
        public void send(String msg) {
            Map<String, Object> msgContent = new HashMap<>();
            msgContent.put("msg", msg);
            msgContent.put("timeStamp", System.currentTimeMillis());
            streamBridge.send("cloudBus-out-0", msgContent);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    从简原则,就不单独写业务Service了,直接在Controller里面写逻辑了。正常的工程里面,发送消息的逻辑还是要写在Service里的。
    没有用@EnableBinding注解,最新版本的spring cloud,这个注解已经是标记了Deprecated
    在这里插入图片描述
    我们用新推荐的StreamBridge。

    • CloudStreamProviderApplication.java
    package com.hui.study.cloud.stream;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    /**
     * 开启微服务
     */
    public class CloudStreamProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(CloudStreamProviderApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    3) 启动

    注册中心正常启动,配置中心不用了。上一章的消费微服务启动。

    执行 CloudStreamProviderApplication.java
    启动成功后,访问 http://localhost:7001
    在这里插入图片描述

    4)调用

    用apifox或postman访问:http://localhost:5002/send
    在这里插入图片描述

    可以看到,我们已经消费到了消息:
    在这里插入图片描述

    3,代码及作业

    链接:https://pan.baidu.com/s/1Pia3pkKzyy0-Bvf4uhgIQQ?pwd=pydh
    提取码:pydh

    链接:https://pan.baidu.com/s/1KwjfIKc3usZ5RSAKmGDwyA?pwd=v1vp
    提取码:v1vp

    作业:
    1、接收消息微服务,启动多个节点来接收消息。

  • 相关阅读:
    淘女郎买家秀API接口
    业内专业人士揭秘:双11即将来临,挑选SSD硬盘避坑指南
    C++精通之路:设计模式(特殊类设计)
    时间复杂度和空间复杂度
    营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
    [21天学习挑战赛——内核笔记](三)——Pinctrl介绍
    【STM32】FSMC接口的复用和非复用
    vue--push,pop,unshift,shift,reverse,splice,sort数组修改产生视图更新
    智能遥测终端机RTU的好处介绍
    Redis(哈希Hash和发布订阅模式)
  • 原文地址:https://blog.csdn.net/yihui823/article/details/126846903