SpringBoot连接redis

springboot使用redis 默认有三种方式连接redis.第一种:jedis—传统的项目–ssm第二种:lettuce:---->刚出现没有多久就被springboot整合进来。第三种:springboot连接redis。 阅读详情
AI摘要

本文介绍如何使用Docker安装配置Redis,并通过Spring Boot实现应用与Redis数据库的连接和交互,包括基本操作和数据类型的应用。

1 docker安装redis(6.2.2)

1.1 下载redis镜像

sudo docker pull redis:6.2.2

1.2 设置配置文件

进入宿主机redis的挂载目录”/home/redis/“,手动创建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

# (4)查看所有的key
KEYS *

# 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.100
    port: 6379
    password: 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 {
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate(){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        // 序列化key
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        // 序列化hash
        redisTemplate.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 {

    @Autowired
    private 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);
	}

}
spring boot中redis的使用 通过这个示例,我们可以看到,在SpringBoot项目中使用Redis作为缓存的流程。我们首先需要添加Redis依赖,然后在配置文件中配置Redis连接信息。对象,并提供了一些方法来对Redis进行操作。现在,我们可以在SpringBoot项目中的任何地方使用这个工具类来进行缓存操作。这个依赖包含了Spring Data Redis,以及Jedis和Lettuce这两种Redis客户端的实现。方法中,我们首先构造了一个缓存的key,然后使用。是Redis的密码(如果没有密码,可以不填), 阅读详情

相关推荐

SpringBoot中集成Redis

有了,并于Spring Boot配置文件中定义Redis相关属性后,就会在项目启动时会注入到Redis中去。前面说到spring中为redis定义了专门的自动配置类,其中定义创建泛型为的RedisTemplate对象,为了允许自定义配置Bean,自动配置类使用@ConditionalOnMissingBean注解,表示当其他地方定义一个RedisTemplate的Bean时,会替代自动配置中的结果。​@Bean​@Bean复制代码为了使用操作更方便,我们定义一个。

2401_83703893的博客 624

springboot连接redis

默认有三种方式连接redis.第一种:jedis---传统的项目--ssm第二种:lettuce:---->刚出现没有多久就被springboot整合进来。第三种:springboot连接redis。

m0_65294164的博客 3291

AI落地困局与破局者FDE:从技术神话到价值实干的战略转型

AI落地困局与破局者FDE:从技术神话到价值实干的战略转型 当前AI企业级应用面临三大核心挑战:业务场景深度非标化、技术落地"最后一公里"困局、以及市场竞争焦点转向实用价值。前线部署工程师(FDE)作为破局关键,通过技术-业务-产品三维交汇的复合能力,将AI从实验室推向生产环境。FDE不仅是技术架构师,更是业务价值挖掘机和产品进化传感器,通过"碎石路-高速公路"双循环模式,将定制化需求沉淀为可复用的平台能力。这种模式颠覆了传统SaaS的销售逻辑,形成基于价值的J型增长

随笔记录-分享&记忆 4002

SpringBoot简单连接redis数据库, 返回string、json等类型数据

springboot连接redis

xiaomingming99的博客 3743

ADS版图优化中的参数化设计技巧

本文详细介绍了在ADS中进行射频电路版图参数化设计的核心技巧。通过将版图关键尺寸定义为变量,并与电磁仿真及优化器联动,实现从手动修改到自动迭代的转变,极大提升了PA、LNA等电路的设计与优化效率。文章以阶梯阻抗变换器为例,阐述了变量创建、绑定及协同仿真的完整流程,并提供了避免网格冲突、变量混乱等常见问题的实用指南。

weixin_29296241的博客 405

win 11bitlocker恢复密匙一般情况的解决方式(这里指的是你现在使用的微软账户一直没有变更过)

本来没有打算写这一篇解决帖子的,但是最近好多微博的友友都在私我询问解决方法,孩子虽然很热心,但是真的回复不过来了,打字太累了,这里给大家简单指个路,希望能够对大家有所帮助。 当时出现这个问题的时候,一搜网上的解决方法都是重装系统什么的,真是吓死孩子啦。后来去微软的官网倒腾了半天解决了问题。其实只要你没有改过微软账户,你可以直接试试我这个方法,大概三到五分钟解决问题吧。 和我情况不一样的也可以参考一下贴吧里一些大佬的分享,或者实在着急我感觉去淘宝上找个解决也不是未尝不可。 出现问题: 解决方案: 直接去微软

zhiqingxue的博客 2万+

SpringBoot连接Redis

本文介绍了SpringBoot连接Redis的配置与使用方法。以及介绍了使用RedisTemplate和StringRedisTemplate进行数据操作,分析了两者的序列化规则和适用场景。

2401_84284464的博客 1600

PCB布线避坑指南:晶振布局的5个致命错误(附正确示例图)

本文深入解析PCB布线中晶振布局的5个致命错误,包括底层布线、电容布局、地平面处理等关键问题,并提供实战解决方案。通过真实案例和量化数据,帮助硬件开发者避免常见陷阱,提升电路稳定性和性能。特别针对晶振这一高敏感元件,给出布线参数黄金法则和三维避让原则。

weixin_29215391的博客 428

java连接Redis和springboot连接Redis

连接Redis的两种方式一、使用Java连接Redis二、使用springboot连接Redis 一、使用Java连接Redis 创建一个Maven项目 导入连接redis的相关依赖 <dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId>

shi_AXing_a的博客 375

从零开始学习PX4源码0(固件下载及编译)

本节主要记录从零开始学习PX4源码1(固件下载)的过程,欢迎批评指正!!!下载固件主要分为两个版本,之前稳定版本和最新官网发布版本,为什么要下载两个版本,主要是说明两个版本有一定的差异(开发环境及源码文件格式等),我们是从之前版本开始学习,为什么不从最新的?因为之前版本代码文件格式还是固定的,最新的变化很大,不利于初学者学习,apm和px4不好的地方就是之前版本v1.7.2和最新版本v1.14.0有些文件总是在变化,这样很不好。

陌城烟雨 4616

由JavaScript版迷宫游戏引出Java版迷宫地图生成器

Java案例:迷宫地图生成器 一、引入问题 本期18软件1班网页脚本实训项目是完成一个JavaScript版迷宫游戏。 迷宫地图是如何生成的呢?很简单,将一个正方形区域等分为若干行与若干列,比如25行25列,这样就有625个单元格,用一个二维数组map[25][25]就可以保存625个单元格信息,map[i][j] = 1表示该单元格要绘制砖,而map[i][j] = 0表示该单元格不绘制砖。 ...

howard2005的专栏 1473

springboot redis连接不上_springboot项目连接Redis操作

springboot项目连接Redis操作基于缓存的数据库存储,C语言开发,支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API,操作很是舒服,我们先来跑通Redis服务,并连上springboot项目首先需要下载Redis的服务器包GitHub下载地址https://github.com/tporadowski/redis/releases我们选择...

weixin_39878247的博客 2122

ximalaya 新xm-sign逆向思路

本文主要介绍目标网站新xm-sign值的逆向思路。仅代表个人理解,如有其他建议可在评论区沟通。声明仅仅记录一下自己的学习方法,不作为其他参考、更不作为商业用途。如有侵犯请联系本人删除。

weixin_66580433的博客 2017

redis安装、连接及springboot配置

Redis(Remote Dictionary Server)是一个开源的内存数据库,遵守 BSD 协议,它提供了一个高性能的键值(key-value)存储系统,常用于缓存、消息队列、会话存储等应用场景。高性能:redis的高性能(能够支持每秒数十万次的读写操作),使得Redis成为处理高并发请求的理想选择。数据类型丰富:提供字符串、列表、集合、哈希表、有序集合等数据类型。原子性操作:Redis 的所有操作都是原子性的,这意味着操作要么完全执行,要么完全不执行。

一只又菜又爱玩的小卡拉米 1320

YOLOv10改进策略【模型轻量化】| MoblieNetV3:基于搜索技术和新颖架构设计的轻量型网络模型

本文记录的是基于MobileNet V3的YOLOv10目标检测轻量化改进方法研究。的模型结构是通过得来的,其中的基础模块结合了的深度可分离卷积、的线性瓶颈和倒置残差结构以及MnasNet中基于挤压和激励的轻量级注意力模块,使模型在性能、效率和灵活性方面都具有显著的优势。本文在替换骨干网络中配置了原论文中的small和large2种模型,以满足不同的需求。

Limiiiing的博客 514

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

如何在SpringBoot项目中优雅的连接多台Redis

本文介绍了在Spring Boot项目中优雅连接多个Redis实例的解决方案。通过配置两个独立的RedisProperties和RedisConnectionFactory(主Redis标记为@Primary),解决了多实例注入歧义问题。同时提供了完整的RedisConfig配置类,创建了两个RedisTemplate实例(主Redis和爬虫Redis),并封装了通用的RedisCache工具类,支持动态选择Redis实例操作。这种方法适用于需要连接不同Redis实例的业务场景,如主业务数据存储和爬虫数据缓

Cavewang的博客 1761

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

java springboot连接redis

java springboot连接redis pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> application.yml #单机配置 sprin

qq_33619502的博客 356

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

SpringBoot 配置 Redis 连接池

SpringBoot2.0默认采用 Lettuce 客户端来连接 Redis 服务 默认是不使用连接池的,只有配置 redis.lettuce.pool下的属性的时候才可以使用到redis连接池

御剑长歌的博客 1万+

SpringBoot连接多个redis

SpringBoot连接多个redis

katz123451的博客 573

java连接redis及springboot连接redis

springboot在整合reids时提供两个模板类,StringRedisTemplate和RedisTemplate,以后对redis的操作都在该模板类中。是StringRedisTemplate的父类,类可以存储任意数据类型,但是任意类型必须序列化,默认采用的是jdk的序列化方式。有请求先查询缓存是否缓存命中 如果命中直接返回响应 如果未命中查询数据库 拿到结果后把查到数据存入缓存中并把数据返回响应。第一种:jedis --- 传统的项目 --- ssm。--- 提高系统的性能。

m0_73809543的博客 967

SpringBoot连接多个Redis或者Redis里的多个不同的库_springboot 连接多个redis

要想实现连接不同的Redis,只需要将对应的Redis地址写进配置里即可。// ================================ Map类型 =================================// =============================== List类型 =================================// ============================= 公共方法 ============================

2401_84264107的博客 2124

springBoot连接远程Redis连接失败(已解决)

我本地没有安装redis,当然连接不上了,但是为什么我配置的远程ip,会连接到本地!由于在springBoot中每个配置项都会有一个默认的自动配置类与之对应,我这里配置了但是没效果,显然配置失败,项目使用默认的localhost进行连接的,那为啥会配置失败呢。首先,我是先用jedis进行的redis连接,成功连接,没有任何问题,说明redis配置,以及访问地址、端口、密码都是正确的。而我使用的是spirngBoot3,上面的yml配置方法是springBoot2的配置方法,所以产生了配置失效!

m0_74824170的博客 631

不会用SpringBoot连接Redis,那就赶紧看这篇

摘要:如何通过springboot来集成操作Redis。

华为云官方博客 3410

springboot连接不上redis,但是redis客户端是能连接上的

在 Spring Boot 2.x 及以上的版本中,推荐使用 spring.data.redis 进行 Redis 相关的配置。但对于 Spring Boot 1.x 版本仍然可以使用 spring.redis 进行配置。除了常规排查,还有一个就是检查配置文件格式。这个旧版本格式会导致读取不到配置,改成下面这个就没问题了。

qq_39153893的博客 394
上一篇: fastText文本分类
下一篇: Spring Boot使用log4j2管理日志
MasonYyp
博客等级 码龄9年 301粉丝 297原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值