博客
关于我
10-Redis6-连接整合
阅读量:512 次
发布时间:2019-03-07

本文共 2740 字,大约阅读时间需要 9 分钟。

Spring Boot与Redis集成完全指南

当需要在Spring Boot应用中集成Redis时,下面将从基础操作到高级应用全面的介绍如何实现 Redis 与 Spring Boot 的无缝整合。

1. 引入依赖

在项目中首先需要引入相关的 Redis 描述文件。推荐使用Redis客户端 Jedis,具体依赖信息如下:

redis.clients
jedis
3.2.0

同时为了充分发挥Spring Boot与Redis的优势,还需引入Spring Boot Redis集成starter包:

org.springframework.boot
spring-boot-starter-data-redis

另外,为了支持连接池功能,需引入Apache Commons Pool2:

org.apache.commons
commons-pool2

2. 建立Redis连接

在应用程序启动之前,需要确保Redis服务器认可应用的连接请求。以下是基本的连接代码示例:

Jedis jedis = new Jedis("192.168.181.138", 6379);

那种直接创建 Jedis 实例的方式可以满足基本需求,但建议在商业环境中使用 LettuceConnectionFactory 提供的连接池机制,以享受高效的线程管理和负载均衡能力。

3. Redis 常用操作

使用 JedisRedisTemplate 进行 Redis 操作时,常用的方式如下:

  • 操作字符串:

    redisTemplate.opsForValue().set("123", "1111111");
  • 获取字符串值:

    String value = redisTemplate.opsForValue().get("123");
  • 操作Hash:

    redisTemplate.opsForHash().hSet("user", "id", "123");
  • 操作列表:

    redisTemplate.opsForList().leftPush("my-list", "new-element");
  • 操作集合:

    redisTemplate.opsForSet().add("my-set", "new-element");

4. Spring Boot 整合 Redis

在Spring Boot项目中整合 Redis,主要通过 RedisTemplate 实现。

4.1 RedisBean 配置

@Configurationpublic class RedisConfig {    @Bean    public RedisTemplate
redisTemplate( LettuceConnectionFactory connectionFactory) { RedisTemplate
redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; }}

这里,RedisTemplate 已经配置好了:

  • 键序列化:默认使用 StringRedisSerializer
  • 值序列化:这里使用 Json 序列化功能,通过 GenericJackson2JsonRedisSerializer 将 Java 对象序列化为 JSON 格式存储。

4.2 配置文件说明

application.properties 文件中,设置 Redis 连接信息:

# Redis服务器地址spring.redis.host=192.168.181.138# Redis服务器连接端口spring.redis.port=6379# 连接池最大连接数,默认为8spring.redis.lettuce.pool.max-active=20# 最大阻塞等待时间,默认为-1(无限制)spring.redis.lettuce.pool.max-wait=-1# 最大空闲连接数,默认为8spring.redis.lettuce.pool.max-idle=8# 最小空闲连接数,默认为0spring.redis.lettuce.pool.min-idle=0

这些配置只需在项目启动之前准备好,Spring Boot会自动初始化 Redis 连接池并管理连接。

5. 测试控制器

@Controllerpublic class DemoController {    @Autowired    private RedisTemplate
redisTemplate; @RequestMapping("/home") @ResponseBody public String setRedis() { redisTemplate.opsForValue().set("123", "1111111"); String value = redisTemplate.opsForValue().get("123"); System.out.println("从Redis获取到的值为:" + value); return "从Redis获取到的值为:" + value; }}

这个测试控制器主要展示了如何在Spring Boot应用中进行 Redis 操作,包括:

  • 字符串操作:通过 opsForValue 操作字符串值。
  • 哈希操作:使用 opsForHash 操作哈希数据结构。
  • 列表和集合操作:展示如何对 Redis 列表和集合进行操作。

转载地址:http://vdgjz.baihongyu.com/

你可能感兴趣的文章
Nginx配置Https证书
查看>>
Nginx配置ssl实现https
查看>>
Nginx配置TCP代理指南
查看>>
Nginx配置——不记录指定文件类型日志
查看>>
nginx配置一、二级域名、多域名对应(api接口、前端网站、后台管理网站)
查看>>
Nginx配置代理解决本地html进行ajax请求接口跨域问题
查看>>
nginx配置全解
查看>>
Nginx配置参数中文说明
查看>>
nginx配置域名和ip同时访问、开放多端口
查看>>
Nginx配置好ssl,但$_SERVER[‘HTTPS‘]取不到值
查看>>
Nginx配置如何一键生成
查看>>
Nginx配置实例-负载均衡实例:平均访问多台服务器
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Nginx配置负载均衡到后台网关集群
查看>>
ngrok | 内网穿透,支持 HTTPS、国内访问、静态域名
查看>>
NHibernate学习[1]
查看>>
NHibernate异常:No persister for的解决办法
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_日期类型_以及null数据同步处理补充---大数据之Nifi工作笔记0057
查看>>
NIFI1.21.0_NIFI和hadoop蹦了_200G集群磁盘又满了_Jps看不到进程了_Unable to write in /tmp. Aborting----大数据之Nifi工作笔记0052
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增删改数据分发及删除数据实时同步_通过分页解决变更记录过大问题_02----大数据之Nifi工作笔记0054
查看>>