博客
关于我
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/

你可能感兴趣的文章
Objective-C实现ABC人工蜂群算法(附完整源码)
查看>>
Objective-C实现activity selection活动选择问题算法(附完整源码)
查看>>
Objective-C实现AC算法(Aho-Corasick) 算法(附完整源码)
查看>>
Objective-C实现adaboost算法(附完整源码)
查看>>
Objective-C实现Adler32算法(附完整源码)
查看>>
Objective-C实现AES算法(附完整源码)
查看>>
Objective-C实现AffineCipher仿射密码算法(附完整源码)
查看>>
Objective-C实现aliquot sum等分求和算法(附完整源码)
查看>>
Objective-C实现all combinations所有组合算法(附完整源码)
查看>>
Objective-C实现all permutations所有排列算法(附完整源码)
查看>>
Objective-C实现all subsequences所有子序列算法(附完整源码)
查看>>
Objective-C实现AlphaNumericalSort字母数字排序算法(附完整源码)
查看>>
Objective-C实现alternate disjoint set不相交集算法(附完整源码)
查看>>
Objective-C实现alternative list arrange备选列表排列算法(附完整源码)
查看>>
Objective-C实现An Armstrong number阿姆斯特朗数算法(附完整源码)
查看>>
Objective-C实现anagrams字谜算法(附完整源码)
查看>>
Objective-C实现ApproximationMonteCarlo蒙特卡洛方法计算pi值算法 (附完整源码)
查看>>