springboot 整合redis

本文介绍了如何在SpringBoot项目中整合Redis,包括搭建SpringBoot工程、添加redis起步依赖、配置redis属性、使用RedisTemplate以及进行测试验证。在配置过程中,需要注意Redis的protected-mode设置,以确保能正常连接。
摘要由CSDN通过智能技术生成

实现步骤

  1. 搭建SpringBoot工程。
  2. 引入redis起步依赖。
  3. 配置redis相关属性。
  4. 注入RedisTemplate模板。
  5. 编写测试方法。

搭建SpringBoot工程
在这里插入图片描述
起步依赖
pom文件中默认就添加了

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

配置文件
application.yml文件(前提是对应IP已经启用了redis)

spring:
  redis:
    host: 192.168.0.103
    port: 6379

redis配置

systemctl stop firewall
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
tar -xvf redis-6.2.6.tar.gz
cd redis-6.2.6 && make
cd src/
./redis-server

telnet 验证6379端口报错,需要修改redis.conf配置文件,protected-mode为no,且注释127.0.0.1的IP
-DENIED Redis is running in protected mode because protected mode is enabled

vi redis.conf
protected-mode no
#bind 127.0.0.1 -::1

启动

./redis-server ../redis.conf

测试

package com.yy.springboot_redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class SpringbootRedisApplicationTests {

    //    注入模板
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSet() {
        //存入数据
        redisTemplate.boundValueOps("username").set("zhangyy");
    }

    @Test
    public void testGet() {
        //获取数据
        Object username = redisTemplate.boundValueOps("username").get();
        System.out.println(username);
    }


}

好的,下面是SpringBoot整合Redis的步骤: 1. 在pom.xml中引入Redis的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在application.properties或application.yml中配置Redis连接信息,例如: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.database=0 ``` 3. 创建RedisConfig类,配置RedisTemplate和StringRedisTemplate: ``` @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(redisConnectionFactory); return stringRedisTemplate; } } ``` 4. 在需要使用Redis的地方注入RedisTemplate或StringRedisTemplate,并使用相应方法操作Redis,例如: ``` @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } ``` 以上就是SpringBoot整合Redis的基本步骤,希望能够帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值