1.Redis的安装Ubuntu安装Redis-CSDN博客
2.Redis在Spring Boot 3 下的使用
2.1 pom.xml
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.2 配置端口转发
Redis服务器安装在云服务器上,而我们编写的代码实在本地主机的。要想让本地机器能够访问到Redis,需要把Redis的端口通过云服务器的后台页面的"防护墙"/"安全组"放开,但是这会使得端口暴露在公网中,这可能会使得黑客顺着端口入侵你的服务器,非常危险。因此我们使用转发的方式,直接把服务器的redis端口映射到本地。
在Xshell中,进行如下的配置:
1)右键服务器的会话,选择属性;
2)找到隧道 -> 配置转移规则。

- 使用该会话连接服务器
此时, 访问本地的 8888, 就相当于访问对应服务器的 6379
注意, xshell 和服务器必须处在连接状态, 这样的映射才是有效的.
也就说如果你要在本地使用redis,就要启动云服务器,并且确保redis服务器启动;
2.3 application.properties 配置 Redis
redis
spring.data.redis.host=localhost
spring.data.redis.port=8888
连接空闲超过N(s秒、ms毫秒)后关闭,0为禁用,这里配置值和tcp-keepalive值一致
spring.data.redis.timeout=60s
默认使用 lettuce 连接池
允许最大连接数,默认8(负值表示没有限制)
spring.data.redis.lettuce.pool.max-active=8
最大空闲连接数,默认8
spring.data.redis.lettuce.pool.max-idle=8
最小空闲连接数,默认0
spring.data.redis.lettuce.pool.min-idle=0
连接用完时,新的请求等待时间(s秒、ms毫秒),超过该时间抛出异常
JedisConnectionException,(默认-1,负值表示没有限制)
spring.data.redis.lettuce.pool.max-wait=5s
3. 安装 idea 社区版 redis 可视化插件(免费)
3.1 在File->Settings->Plugins->搜索Redis Helper插件

3.2 右侧会生成一个页签Redis Helper

4. 登录远程Redis

5.测试
java
@SpringBootTest
public class RedisTest {
@Autowired
private StringRedisTemplate redisTemplate;
@Test
public void redisTest() {
redisTemplate.opsForValue().set("key", "value");
String value = redisTemplate.opsForValue().get("key");
System.out.println(value);
//redisTemplate.delete("key");
}
}

欢迎评论区讨论......