SpringBoot连接redis
1 docker安装redis(6.2.2)
1.1 下载redis镜像
sudo docker pull redis:6.2.2
1.2 设置配置文件
手动创建redis.conf,并添加如下配置参数。
# 可远程连接
# bind 127.0.0.1
# 解除保护模式
protected-mode no
# 数据持久化
appendonly yes
# 设置密码
requirepass 123456
也可以在网上下载redis配置文件,注意需要修改里面的配置。
wget http://download.redis.io/redis-stable/redis.conf
授权配置文件
chmod 777 redis.conf
1.3 生成容器
sudo docker run -itd \
--name myredis \
-p 6379:6379 \
-v /home/redis/redis.conf:/etc/redis/redis.conf \
-v /home/redis/data:/data \
redis:6.2.2 redis-server /etc/redis/redis.conf
2 简单使用redis
# 进入redis
sudo docker exec -it myredis /bin/bash# 使用redis
# 参数--raw,表示按原生数据显示,可以解决中文显示乱码,按原始格式打印
redis-cli --raw# 输入密码
auth 123456# (1) 键值对
# 创建键值对
set name mason
# 查看键值对
get name# (2) 列表
# 插入列表
RPUSH user "河南大学"
# 查看列表中所有数据
LRANGE users 0 -1# (3) hash
# 插入键值对,key:book, field: title, value: "河南大学发展"
HSET book title "河南大学发展"
# 查看键值对
HGET book title# redis清空所有数据
flushall
3 SpringBoot连接redis
3.1 工程结构
3.2 pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>myredis</artifactId><version>1.0.0</version><name>myredis</name><description>Myredis project for Spring Boot</description><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- Init the entity --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version><scope>provided</scope></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><version>2.3.1.RELEASE</version></plugin></plugins></build></project>
3.3 application.yml
spring:redis:host: 192.168.108.100port: 6379password: 123456
3.4 conf
ConfigRedis.java
package com.example.myredis.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class ConfigRedis {@Autowiredprivate RedisConnectionFactory redisConnectionFactory;@Beanpublic RedisTemplate<String, Object> redisTemplate(){RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();// 序列化keyredisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new StringRedisSerializer());// 序列化hashredisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new StringRedisSerializer());// 连接redis数据库redisTemplate.setConnectionFactory(redisConnectionFactory);return redisTemplate;}
}
3.5 controller
RequestController.java
package com.example.myredis.controller;import com.example.myredis.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;@Controller
@RequestMapping("/redis")
@ResponseBody
public class RequestController {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@GetMapping(value = "/key")public String putKey(String key, String value){this.redisTemplate.opsForValue().set(key,value,30, TimeUnit.SECONDS);System.out.println(this.redisTemplate.opsForValue().get(key));return "1";}@GetMapping(value = "/object")public String putObject(){User user = new User("河南大学", 10);// 序列化对象this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));this.redisTemplate.opsForValue().set("user", user,30, TimeUnit.SECONDS);System.out.println(this.redisTemplate.opsForValue().get("user"));return "1";}@GetMapping(value = "/list")public String putList(){this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));this.redisTemplate.opsForList().leftPush("users", new User("河南大学", 10));System.out.println(this.redisTemplate.opsForList().range("users", 0, -1));return "1";}@GetMapping(value = "/map")public String putMap(){Map<String, String> myMap = new HashMap<>();myMap.put("name", "河南大学");myMap.put("age", "20");this.redisTemplate.opsForHash().putAll("map", myMap);System.out.println(this.redisTemplate.opsForHash().get("map", "name"));return "1";}}
3.6 entity
User.java
package com.example.myredis.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class User{private String name;private int age;
}
3.7 Application
package com.example.myredis;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class MyredisApplication {public static void main(String[] args) {SpringApplication.run(MyredisApplication.class, args);}}
SpringBoot连接redis相关推荐
- SpringBoot连接Redis服务出现DENIED Redis is running in protected mode because protected mode is enabled
问题描述:SpringBoot连接Redis服务出现DENIED Redis is running in protected mode because protected mode is enable ...
- springboot连接redis错误 io.lettuce.core.RedisCommandTimeoutException:
springboot连接redis报错 超时连接不上 可以从以下方面排查 1查看自己的配置文件信息,把超时时间不要设置0毫秒 设置5000毫秒 2redis服务长时间不连接就会休眠,也会连接不上 重 ...
- springboot连接redis并动态切换database(db0到db15)
redis redis db0到db15 springboot连接redis 添加配置文件application.properties 测试是否连接成功 redis动态切换database redis ...
- Springboot连接redis配置
Springboot连接redis配置 application.properties #Redis服务器地址 spring.redis.host=192.168.233.128 #Redis服务器连接 ...
- springboot连接redis集群
开启redis服务和客户端 查看下当前redis的进程 [root@localhost ~]# ps -ef | grep redis 启动redis服务 [root@localhost ~]# cd ...
- SpringBoot连接Redis服务出现Command timed out
问题描述:SpringBoot整合Redis,连接Redis服务时出现Command timed out 解决方法: 查看配置文件是否是设置的连接超时时间过小,一般将其设置为5000毫秒
- org.springframework.data.redis.RedisSystemException: Error in execution; nes遇到springboot连接Redis报错
org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is io.lett ...
- SpringBoot连接Redis集群时发生错误Cannot determine a partition to read for slot
今天做SpringBoot和Redis整合时发生错误 io.lettuce.core.cluster.PartitionSelectorException: Cannot determine a pa ...
- SpringBoot连接Redis超简单
建立一个springboot项目,什么也不用设置,建立一个最简单的就可以的,首先还是加入我们的Maven驱动包 <!-- 加入redis连接池--> <dependency>& ...
最新文章
- RuntimeError: each element in list of batch should be of equal size
- 【教程】2、读取新闻条目
- MyBatis的DAO接口中参数传递建议使用map类型的原因
- linux教程:通过编译安装ansible解决apt install ansible后无法安装AWX的莫名问题
- weekendplan
- 开源java性能分析工具_Java性能监控:您应该知道的5个开源工具
- 【百度地图API】如何给自定义覆盖物添加事件
- RHEL6入门系列之十七,打包与压缩
- 深度学习基础 | 从Language Model到RNN
- 字符编码 ASCII,Unicode和UTF-8的关系
- WPF中同一窗口下的界面切换
- 传智播客 C/C++学习笔记 字符串替换
- Tomcat中的四大servlet容器及管道机制
- linux系统中连接两个网桥,Linux 网桥代码分析 (二)
- 常用的即时通讯软件排行榜TOP10介绍
- 公有云时代企业需要什么样的云平台
- Android锁屏下弹窗的尝试,android开发实战我的云音乐
- 猿如意中的【取色器】效率工具详情介绍
- 新南威尔士大学纯硅量子计算机,新南威尔士大学工程科学硕士-电气工程小方向课程解析...
- 《魔兽世界插件》教程—21点扑克游戏 Blackjack