SpringBoot+rabbitMq的配置和使用Demo

SpringBoot+rabbitMq的配置和使用Demo

首先如果编辑器使用的IDEA,用SpringBoot创建项目,则无需考虑插件问题。
如果使用eclipse进行编程,要在eclipse中安装STS插件。

SpringBoot是什么

定义不多说,SpringBoot可以理解成是一个简化配置的Spring,只需要引入jar包,无需写Spring中Spring.xml等配置,便可实现与客户端的交互。
还有一个优点就是,启动项目时,省去了在Tomcat等服务器上部署的麻烦,浏览器访问后台数据时路径的优化也提高了安全性。

安装STS及创建第一个SpringBoot项目

可以根据链接步骤添加STS插件,安装过程中可能因为网速较慢等原因,需要耐心等待。
http://jingyan.baidu.com/article/1612d5005fd087e20f1eee10.html

点击eclipse中的window–>preferences 查看Spring,出现如下内容即安装成功

创建第一个SpringBoot项目

1、File–>New–>Project
选择Spring Starter Project,然后点击next
这里写图片描述
2、填写好项目名等信息,点击next
这里写图片描述
3、勾选需要的Dependency。
这里写图片描述
4、点击finish,等待下载Dependency,就创建成功了。

运行SpringBoot项目

找到主函数

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.HelloProducer;
@EnableAutoConfiguration 
@RestController 
@RequestMapping("/user")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Run As -> Spring Boot App,查看控制台打印出如下图,就是运行成功。
这里写图片描述

在SpringBoot项目中整合RabbitMq

RabbitMq是流行的消息中间件

生产者将消息给交换机(Exchange),交换机(Exchange)根据相应的调度放入相应的队列(Queue)中。消费者监听指定的消息队列,获取数据。

1、在pom.xml中,添加 Maven 依赖

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

2、配置application.properties
这里写图片描述

spring.application.name=springboot-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.virtual-host=/

3、注册 bean,新建类RabbitConfiguration

package com.example.demo;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class RabbitConfiguration {
     final static String queueName = "helloQueue";//队列名

        @Bean
        public Queue helloQueue1() {
            return new Queue("hello");
        }

        @Bean
        public Queue userQueue() {
            return new Queue("user");
        }


    public static void main(String[] args) throws Exception {
        SpringApplication.run(RabbitConfiguration.class, args);
    }
}

4、生产者

package com.example.demo;

import java.util.Date;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HelloProducer {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public String send() {
        String sendMsg = "hello1 " + new Date();
        System.out.println("Sender1 : " + sendMsg);
        this.rabbitTemplate.convertAndSend("helloQueue", sendMsg);
        return sendMsg;
    }
}

5消费者

package com.example.demo;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "helloQueue")
public class HelloConsumer {
    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver1  : " + hello);
    }
}

6、Controller

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.HelloProducer;
@EnableAutoConfiguration 
@RestController 
@RequestMapping("/user")
public class DemoApplication {
//  public static void main(String[] args) {
//      SpringApplication.run(DemoApplication.class, args);
//      
//  }
        @RequestMapping("")
          public String hello1(){
          return"Hello world!";
          }


        @Autowired
        private HelloProducer helloSender1;
        @Autowired
        private HelloProducer helloSender2;

        @RequestMapping("/hello")
        public String hello() {
            return helloSender1.send();
        }
}

7、在RabbitConfiguration中run as–>Spring Boot App运行项目
8、打开浏览器,输入url
http://localhost:8080/user/hello/
这里写图片描述
9、控制台看到,消费者接受到生产者发送的消息
这里写图片描述

成功!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值