记录本地连接虚拟机上的redis

it2023-06-17  71

1.首先把redis下载在虚拟机上,这个可以参考redis官网:

https://redis.io/download

下载编译完成后,然后进入redis中启动redis, 到redis的bin目录下启动. 带配置文件的启动命令,目录为对应conf的目录:

./redis-server /usr/local/redis/etc/redis.conf

然后启动客户端,命令:

./redis-cli 然后就可以简单的使用redis的命令设置值,取值.

2.然后在项目引入redis的依赖

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

配置文件中加入redis的一些配置

spring.redis.host=196.168.221.129 //这里为对应自己虚拟机的ip spring.redis.port=6379 # Redis 数据库索引(默认为 0) spring.redis.database=0 # Redis 服务器连接端口 # Redis 服务器连接密码(默认为空) spring.redis.password= //如redis.conf配置文件中有设置密码,需要填写密码 #连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0 # 连接超时时间(毫秒) spring.redis.timeout=5000

配置完成,写一个简单的单元测试.

@Autowired StringRedisTemplate stringRedisTemplate; @Test public void testSet(){ stringRedisTemplate.opsForValue().set("666", "successful"); System.out.println("successful"); } @Test public void testGet(){ String value = stringRedisTemplate.opsForValue().get("666"); System.out.println(value); }

打印效果代表成功,然后去虚拟机上看看是否有值. 这样一个简单的测试就完成了. 如果在测试的时候出现报错,异常: 需要将redis.conf文件中对应位置修改:

关闭防火墙命令:

systemctl stop firewalld.service

然后再重新启动redis,带上配置文件,不带上redis.conf的启动,配置文件可能 是不会生效的.

最新回复(0)