一、说明

  Spring中可以配置RedisTemplate来操作Redis,但是本文中并没有使用RedisTemplate,而是单纯的使用Spring的IoC,单独创建一个配置类,用来配置Redis,然后在需要进行Redis操作的地方,注入配置的Jedis即可。

  也就是说,本文中的内容,单纯地使用Jedis,其实和普通java项目配置Redis并没有太多的不同。

二、JedisCluster集群配置

  集群配置,需要先有一个Redis集群,可以参考:Redis集群搭建使用Jedis操作Redis

2.1、导入依赖

  仍旧使用上面的依赖,不需要导入spring-boot-starter-cache、spring-boot-starter-data-redis,只需要导入jedis即可。

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>

  

2.2、配置文件

  在配置文件中加入一下内容:

# redis集群的节点信息
redis.cluster.nodes=192.168.1.3:6379,192.168.1.4:6379,192.168.1.5:6379
# redis连接池的配置
redis.cluster.pool.max-active=8
redis.cluster.pool.max-idle=5
redis.cluster.pool.min-idle=3

  

2.3、创建配置类

  下面是示例代码:

package cn.ganlixin.ssm.config;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig; import java.util.Set;
import java.util.stream.Collectors; @Configuration
public class RedisClusterConfig { private static final Logger log = LoggerFactory.getLogger(RedisClusterConfig.class); @Value("${redis.cluster.nodes}")
private Set<String> redisNodes; @Value("${redis.cluster.pool.max-active}")
private int maxTotal; @Value("${redis.cluster.pool.max-idle}")
private int maxIdle; @Value("${redis.cluster.pool.min-idle}")
private int minIdle; // 初始化redis配置
@Bean
public JedisCluster redisCluster() { if (CollectionUtils.isEmpty(redisNodes)) {
throw new RuntimeException();
} // 设置redis集群的节点信息
Set<HostAndPort> nodes = redisNodes.stream().map(node -> {
String[] nodeInfo = node.split(":");
if (nodeInfo.length == 2) {
return new HostAndPort(nodeInfo[0], Integer.parseInt(nodeInfo[1]));
} else {
return new HostAndPort(nodeInfo[0], 6379);
}
}).collect(Collectors.toSet()); // 配置连接池
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxTotal);
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle); // 创建jediscluster,传入节点列表和连接池配置
JedisCluster cluster = new JedisCluster(nodes, jedisPoolConfig);
log.info("finish jedis cluster initailization"); return cluster;
}
}

  

2.4、测试使用

  使用的时候,只需要注入redisCluster即可。

package cn.ganlixin.ssm.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster; import javax.annotation.Resource; @RestController
@RequestMapping("redis")
public class RedisController { @Resource
private JedisCluster redisCluster; @RequestMapping("test")
public String test() {
redisCluster.set("hello", "world"); String val = redisCluster.get("hello");
return val;
}
}

  

  

Spring整合Redis,并配置Jedis连接池的更多相关文章

  1. Java与redis交互、Jedis连接池JedisPool

    Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...

  2. Spring 整合 Redis (零配置) 的简单使用

    pom.xml <!--jedis--> <dependency> <groupId>redis.clients</groupId> <artif ...

  3. (七)Spring 配置 c3p0 连接池

    目录 在 Spring 核心配置文件中配置 c3p0 连接池 配置 JdbcTemplate 对象 在 service 层注入 userDao 在 UserDao 里面注入 JdbcTemplate ...

  4. spring整合redis客户端及缓存接口设计(转)

    一.写在前面 缓存作为系统性能优化的一大杀手锏,几乎在每个系统或多或少的用到缓存.有的使用本地内存作为缓存,有的使用本地硬盘作为缓存,有的使用缓存服务器.但是无论使用哪种缓存,接口中的方法都是差不多. ...

  5. spring整合redis客户端及缓存接口设计

    一.写在前面 缓存作为系统性能优化的一大杀手锏,几乎在每个系统或多或少的用到缓存.有的使用本地内存作为缓存,有的使用本地硬盘作为缓存,有的使用缓存服务器.但是无论使用哪种缓存,接口中的方法都是差不多. ...

  6. Spring Boot不同版本整合Redis的配置

    1. Spring Boot为1.4及其他低版本 1.1 POM.XML配置 <!--引入 spring-boot-starter-redis(1.4版本前)--> <depende ...

  7. spring boot配置druid连接池连接mysql

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  8. Java Redis系列3(Jedis的使用+jedis连接池技术)

    Jedis的使用 什么是Jedis? 一款Java操作redis数据库的工具 使用步骤 1.下载redis所需的java包 2.使用步骤 import org.junit.Test; public c ...

  9. 三、redis学习(jedis连接池)

    一.jedis连接池 二.jedis连接池+config配置文件 三.jedis连接池+config配置文件+util工具类 util类 public class JedisPoolUtils { / ...

随机推荐

  1. MySQL/MariaDB数据库的并发控制

    MySQL/MariaDB数据库的并发控制 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.并发控制概述 1>.什么是并发控制 MySQL是一个服务器级别的数据库,它通常 ...

  2. thinkphp5.x命令执行漏洞复现及环境搭建

    楼主Linux环境是Centos7,LAMP怎么搭不用我废话吧,别看错了 一.thinkphp5.X系列 1.安装composer yum -y install composer 安装php拓展 yu ...

  3. 解决shiro多次从redis读取session的问题

    /** * 重写sessonManager * 解决shiro多次从redis读取session的问题 */ public class CustomSessionManager extends Def ...

  4. HDU4814——数学,模拟进制转换

    本题围绕:数学公式模拟进制转换 HDU4814 Golden Radio Base 题目描述 将一个十进制的非负整数转换成E(黄金分割数)进制的数 输入 不大于10^9的非负整数,处理到文件尾 输出 ...

  5. git提交项目到已有库

    借鉴地址:https://blog.csdn.net/jerryhanjj/article/details/72777618 Git global setup git config --global ...

  6. 如何有效使用Project(2)——进度计划的执行与监控

    继上次的的<编制进度计划.保存基准>继续讲解如何对计划进行执行和监控. 计划执行即:反馈实际进度.反馈工作消耗(本文只考虑工时,不考虑成本).提出计划变更请求.如果你的企业实施了专门的PM ...

  7. Java 15周作业

    题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id.username.password),验证登录是否成功. 题目2:在上一题基础上,当登录成功后,将t_u ...

  8. 【游记】CSP2019 垫底记

    考试时候的我: Day 1 做完 \(T1\) 和 \(T2\),还有 \(2.5 h\),我想阿克 \(Day1\).(\(T3\):不,你不想) 不过一会就想出来给每个点 dfs 贪心选一个点,然 ...

  9. LeetCode 1101. The Earliest Moment When Everyone Become Friends

    原题链接在这里:https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/ 题目: In a soc ...

  10. LeetCode 971. Flip Binary Tree To Match Preorder Traversal

    原题链接在这里:https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/ 题目: Given a bina ...