1 前提准备

  1.1 创建一个springboot项目

    技巧01:本博文基于springboot2.0创建

  1.2 安装redis

    1.2.1 linux版本

      参考博文

    1.2.2 windows版本

      到redis官网下载windows版本的压缩包后,解压即可

  1.3 redis使用

    本博文以window版本为例子,linux版本请参见

    1.3.1 开启服务端

      》进入到解压后的redis根目录

        》执行 redis-server.exe

    1.3.2 开启客户端

      进入到redis解压目录 -> 执行 redis-cli.exe

    1.3.3 测试redis服务端和客户端的通信

      在redis客户端执行 ping,如果返回了 PONG 就表明redis前后端通信正常

    1.3.4 关闭

      客户端和服务端都用 Ctrl + C 就可以关闭了

2 SpringBoot 集成 Redis

  2.1 创建一个SpringBoot项目

    技巧01:创建时引入 spring-boot-starter-web 和 spring-boot-starter-data-redis

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion> <groupId>cn.xiangxu</groupId>
<artifactId>redis_pub_sub</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>redis_pub_sub</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-redis-reactive</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build> </project>

pom.xml

  2.2 配置redis服务器

    技巧01:springboot的启动包已经给我们配置好了redis相关的配置类,所以我们只需要在配置文件中对redis服务器进行相关的配置即可

  2.3 使用redis服务器

    坑01:外部的redis客户端在连接redis服务器时需要关闭redis服务器的守护进程,否则会出现连接失败;修改redis.conf配置文件即可,windows版本的redis配置文件在根目录下的 redis.windows.conf 中;将配置文件中protected-mode 配置值从 yes 改为 no 即可。

    技巧01:因为springboot已经为我们配置好了一切,所以我们直接调用  RedisTemplate 或者 StringRedisTemplate 的相关API就可以对redis服务器进行相关的操作了

    》依赖注入 RedisTemplate 或者 StringRedisTemplate

    》利用依赖注入的  RedisTemplate 或者 StringRedisTemplate  对象进行操作即可

package cn.xiangxu.redis_pub_sub.web;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.*; @RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class TestControllerTest { /**
* 依赖注入RedisTemplate,直接利用RedisTemplate操作redis即可
*/
@Autowired
private RedisTemplate<String, String> redisTemplate; @Test
public void test01(){
log.info("Hello Boy"); // 设置数据
redisTemplate.opsForValue().set("age", "33"); // 获取数据
String result = redisTemplate.opsForValue().get("name");
System.out.println(result.toString());
// System.out.println(redisTemplate.getClientList());;
} }

3 SpringBoot 利用 Redis 实现队列的效果

  3.1 流程介绍

    参考博文

  3.2 源代码

package cn.xiangxu.redis_pub_sub.domain;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import java.util.concurrent.CountDownLatch; /**
* @author 王杨帅
* @create 2018-07-09 16:13
* @desc
**/
@Slf4j
public class Receiver {
private CountDownLatch latch;
@Autowired
public Receiver(CountDownLatch latch) {
this.latch = latch;
} public void receiveMessage(String message) {
log.info("Received <" + message + ">");
latch.countDown();
} }
package cn.xiangxu.redis_pub_sub;

import cn.xiangxu.redis_pub_sub.domain.Receiver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; import java.util.concurrent.CountDownLatch; @SpringBootApplication
@Slf4j
public class RedisPubSubApplication { /*
* Redis消息监听器容器
* 这个容器加载了RedisConnectionFactory和消息监听器
*/
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter){
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("sprinboot-redis-messaage"));
return container;
} /*
* 将Receiver注册为一个消息监听器,并指定消息接收的方法(receiveMessage)
* 如果不指定消息接收的方法,消息监听器会默认的寻找Receiver中的handleMessage这个方法作为消息接收的方法
*/
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver){
return new MessageListenerAdapter(receiver, "receiveMessage");
} /*
* Receiver实例
*/
@Bean
Receiver receiver(CountDownLatch latch){
return new Receiver(latch);
} @Bean
CountDownLatch latch(){
return new CountDownLatch(1);
} /*
* Redis Template 用来发送消息
*/
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory){
return new StringRedisTemplate(connectionFactory);
} public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(RedisPubSubApplication.class, args); StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
// CountDownLatch latch = ctx.getBean(CountDownLatch.class); log.info("Sending message......");
template.convertAndSend("sprinboot-redis-messaage", "Hello, SpringBoot redis message!!!!");
// latch.wait(); // System.exit(0);
}
}

  3.3 效果测试

    3.3.1 利用redis服务器中的客户端测试发布订阅效果

    3.3.2 启动springBoot项目

      在redis服务器中发布的消息会自动打印到控制台上

      

      

      

Springboot21 整合redis、利用redis实现消息队列的更多相关文章

  1. (七)整合 Redis集群 ,实现消息队列场景

    整合 Redis集群 ,实现消息队列场景 1.Redis集群简介 1.1 RedisCluster概念 2.SpringBoot整合Redis集群 2.1 核心依赖 2.2 核心配置 2.3 参数渲染 ...

  2. 利用System V消息队列实现回射客户/服务器

    一.介绍 在学习UNIX网络编程 卷1时,我们当时可以利用Socket套接字来实现回射客户/服务器程序,但是Socket编程是存在一些不足的,例如: 1. 服务器必须启动之时,客户端才能连上服务端,并 ...

  3. SpringBoot2.0 整合 Redis集群 ,实现消息队列场景

    本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...

  4. redis分布式锁和消息队列

    最近博主在看redis的时候发现了两种redis使用方式,与之前redis作为缓存不同,利用的是redis可设置key的有效时间和redis的BRPOP命令. 分布式锁 由于目前一些编程语言,如PHP ...

  5. RabbitMQ,Apache的ActiveMQ,阿里RocketMQ,Kafka,ZeroMQ,MetaMQ,Redis也可实现消息队列,RabbitMQ的应用场景以及基本原理介绍,RabbitMQ基础知识详解,RabbitMQ布曙

    消息队列及常见消息队列介绍 2017-10-10 09:35操作系统/客户端/人脸识别 一.消息队列(MQ)概述 消息队列(Message Queue),是分布式系统中重要的组件,其通用的使用场景可以 ...

  6. Redis学习笔记~实现消息队列比MSMQ更方便

    什么是队列:简单的说就是数据存储到一个空间里(可以是内存,也可以是物理文件),先存储的数据对象,先被取出来,这与堆栈正好相反,消息队列也是这样,将可能出现高并发的数据进行队列存储,并按着入队的顺序依次 ...

  7. Redis实现简单的消息队列

    1.问:什么是消息队列?  答:是一个消息的链表,是一个异步处理的数据处理引擎. 2.问:有什么好处? 答:不仅能够提高系统的负荷,还能够改善因网络阻塞导致的数据缺失. 3.问:用途有哪些? 答:邮件 ...

  8. redis实现有序的消息队列

    redis是什么东西就不多说了,网上文章一搜一大堆. 首先来说一下我要实现的功能: 类似一个消息中转站吧,如果有人要发送消息,先将消息发到我这里来,然后我这边进行转发,为的就是有一个统一的管理和修改时 ...

  9. Redis与RabbitMQ作为消息队列的比较

    简要介绍 RabbitMQ RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.消息中间 ...

  10. 使用Redis Stream来做消息队列和在Asp.Net Core中的实现

    写在前面 我一直以来使用redis的时候,很多低烈度需求(并发要求不是很高)需要用到消息队列的时候,在项目本身已经使用了Redis的情况下都想直接用Redis来做消息队列,而不想引入新的服务,kafk ...

随机推荐

  1. 【Java】Java学习笔记

    教程 计算机所有的数据信息都是由二进制的0,1组成的,B(Byte)就是字节,1B=8bit(位),2的10次幂是1024,我们所说的硬盘容量是40GB.80GB.160GB,这里的B指是的Byte也 ...

  2. Oracle 索引的失效和重建

    查询指定表的索引 SELECT T1.TABLE_NAME, T1.INDEX_NAME, T1.INDEX_TYPE, T1.UNIQUENESS, T1.TABLE_OWNER, T1.STATU ...

  3. HihoCoder1620: 股票价格3 (单调队列 or DP)

    股票价格3 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在关注股票,为了计算股票可能的盈利,他获取了一只股票最近N天的价格A1~AN. 小Hi想知道,对于第 ...

  4. PS基础教程[3]如何去除照片上的水印

    网络上的照片大部分都有很多的水印,要嘛就是网站的地址,要嘛就是一些煽情的文字,我们看图片想要的可不是这些东西,那么我们怎样去掉图片上的水印呢?本次我们就来分享一下仿制图章工具的使用. 方法 1.打开P ...

  5. 数据分析笔试-sql

    题目说明及要求: 以下是模似数据库里的表单信息,请根据要求写出SQL语句 表1:职工信息 表结构如下: 表名:Employee 职工ID   职工姓名       入职年份       部门ID A1 ...

  6. Spring、Spring MVC、Struts2优缺点整理

    Spring 及其优点 大部分项目都少不了Spring的身影,为什么大家对他如此青睐,而且对他的追捧丝毫没有减退之势呢 Spring是什么: Spring是一个轻量级的DI和AOP容器框架. 说它轻量 ...

  7. vuecli3修改项目启动端口

    工作中可能存在启动多个项目的时候,默认端口号会被占,导致启动错误,这种情况下只要把要启动的项目的端口号换掉启动未用的端口就可以了,具体实现如下: vuecli3中的端口文件存放目录为:node_mod ...

  8. Android 中jar包封装及调用-转

    在android开发过程中,我们经常会有这种需求,自己开发一个类库jar包,提供给别人调用. 即把项目A封装成jar包,供项目B调用,而在项目B中调用项目A的activity的时候问题就出现了:找不到 ...

  9. LinkedList插入排序实现

    昨天遇到一个集合排序的问题,要求在list中插入后数据有序,首先考虑使用集合自带的排序方法,但需要把list转成数组,排序后再转回list.后来发现使用插入算法是最省事的,因为既然是在插入里排序,那么 ...

  10. 【转】windows下mysql5.1忘记root密码解决方法

    步骤如下:1.停止mysql服务(以管理员身份,在cmd命令行下运行) net stop mysql D:\>net stop mysql MySQL 服务正在停止. MySQL 服务已成功停止 ...