• java spring boot消息队列 RabbitMQ


    java spring boot消息队列 RabbitMQ

    由于RabbitMQ是基于erlang的,所以,在正式安装RabbitMQ之前,需要先安装一下erlang

    先看看对应关系

    https://www.rabbitmq.com/which-erlang.html

    然后 安装 erlang和RabbitMQ

    完成后看有没有

    没有就启动下

    然后配置下 进入如下

    I:Program FilesRabbitMQ Server
    abbitmq_server-3.8.3sbin
    

     运行命令

    rabbitmq-plugins enable rabbitmq_management

    然后localhost:15672

    可以看到后台了
    默认账户:guest
    默认密码:guest

    ps:修改密码  rabbitmqctl  change_password  guest  1234


    下面说下spring boot 用RabbitMQ


    首先要在后台创建1个队列 不然会报错

    ps:ready 9是生产了9个 还没有消费 当然默认是0

    1先下载依赖

            <!--rabbitmq-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
            </dependency>

    2 配置

    #对于rabbitMQ的支持
    spring.rabbitmq.host=127.0.0.1
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=guest
    spring.rabbitmq.password=guest

    3 队列模式

    package com.example.demo2122;
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import javax.annotation.Resource;
    
    import java.util.*;
    import java.util.stream.IntStream;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    
    @RestController
    @Component
    public class HelloControl {
        @Autowired
        private AmqpTemplate rabbitTemplate;
    
    
        @GetMapping("/hello")
        public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
            String context="生产了1个苹果";
            System.out.println("Sender : " + context);
            this.rabbitTemplate.convertAndSend("hello11", context);
            return "success";
        }
    
        @RabbitListener(queues = "hello11")
        public void process(String hello) {
            System.out.println("Receiver : " + hello);
        }
    
    
    }

    好了 正常不会错了





  • 相关阅读:
    3.4.4 反射和泛型
    4.4.2 空合并操作符让比较不再痛苦
    NPOI导出EXCEL 打印设置分页及打印标题
    20、异常和状态管理
    14 字符字符串和文本处理
    15、枚举类型和标志位
    Oracle 数据库连接的一些坑
    17、委托
    《山鬼·九歌》——屈原
    每周一卦测感情,还是这么凶
  • 原文地址:https://www.cnblogs.com/newmiracle/p/12751597.html
Copyright © 2020-2023  润新知