springboot——集成redis

Ubuntu下Redis安装与SpringBoot集成
本文详细介绍了在Ubuntu系统中安装配置Redis的过程,并演示了如何在SpringBoot项目中集成Redis,包括依赖配置、属性设置及基本的读写操作测试。

一、安装Redis

在redis官网上找到最新稳定版本redis,当前最新版本是5.0.5,复制链接然后在Ubuntu上使用curl命令直接下载

ouyp@ouyp-VirtualBox:~/redis$ curl -O http://download.redis.io/releases/redis-5.0.5.tar.gz
ouyp@ouyp-VirtualBox:~/redis$ tar -zxvf redis-5.0.5.tar.gz 
ouyp@ouyp-VirtualBox:~/redis/redis-5.0.5$ make

二、配置Redis

编辑~/redis-5.0.5/redis.conf文件,做以下修改

  1. 注释bind 127.0.0.1这样其他机器才能访问
#bind 127.0.0.1
  1. 将protected-mode 改为no
protected-mode no
  1. 以配置文件方式启动redis
root@ouyp-VirtualBox:/home/ouyp/redis-5.0.5# ./src/redis-server redis.conf

三、springboot+redis配置

  1. pom.xml配置
<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>
  1. application.properties配置
spring.redis.host=10.6.3.148
spring.redis.port=6379
spring.redis.password=

四、springboot+redis测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class AApplicationTests {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void contextLoads() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "kk-cock");
        map.put("age", 10);
        ObjectMapper objectMapper = new ObjectMapper();
        String value = objectMapper.writeValueAsString(map);
        stringRedisTemplate.opsForValue().set("obj", value);
    }
}

五、参考链接

  1. Ubuntu安装Redis-Redis入门
  2. Redis 远程链接报错(error) DENIED Redis is running 需配置如下
  3. SpringBoot连接Redis服务出现DENIED Redis is running in protected mode because protected mode is enabled
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值