Spring整合Redis,并配置Jedis连接池
一、说明
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连接池的更多相关文章
- Java与redis交互、Jedis连接池JedisPool
Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...
- Spring 整合 Redis (零配置) 的简单使用
pom.xml <!--jedis--> <dependency> <groupId>redis.clients</groupId> <artif ...
- (七)Spring 配置 c3p0 连接池
目录 在 Spring 核心配置文件中配置 c3p0 连接池 配置 JdbcTemplate 对象 在 service 层注入 userDao 在 UserDao 里面注入 JdbcTemplate ...
- spring整合redis客户端及缓存接口设计(转)
一.写在前面 缓存作为系统性能优化的一大杀手锏,几乎在每个系统或多或少的用到缓存.有的使用本地内存作为缓存,有的使用本地硬盘作为缓存,有的使用缓存服务器.但是无论使用哪种缓存,接口中的方法都是差不多. ...
- spring整合redis客户端及缓存接口设计
一.写在前面 缓存作为系统性能优化的一大杀手锏,几乎在每个系统或多或少的用到缓存.有的使用本地内存作为缓存,有的使用本地硬盘作为缓存,有的使用缓存服务器.但是无论使用哪种缓存,接口中的方法都是差不多. ...
- Spring Boot不同版本整合Redis的配置
1. Spring Boot为1.4及其他低版本 1.1 POM.XML配置 <!--引入 spring-boot-starter-redis(1.4版本前)--> <depende ...
- spring boot配置druid连接池连接mysql
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Java Redis系列3(Jedis的使用+jedis连接池技术)
Jedis的使用 什么是Jedis? 一款Java操作redis数据库的工具 使用步骤 1.下载redis所需的java包 2.使用步骤 import org.junit.Test; public c ...
- 三、redis学习(jedis连接池)
一.jedis连接池 二.jedis连接池+config配置文件 三.jedis连接池+config配置文件+util工具类 util类 public class JedisPoolUtils { / ...
随机推荐
- 多线程之美6一CAS与自旋锁
1.什么是CAS CAS 即 compare and swap 比较并交换, 涉及到三个参数,内存值V, 预期值A, 要更新为的值B, 拿着预期值A与内存值V比较,相等则符合预期,将内存值V更新为B, ...
- Gerrit 服务器入门使用-项目的创建与克隆
Gerrit 服务器入门使用-项目的创建与克隆 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.创建克隆项目 1>.点击"BROWSE" 2>.点 ...
- tensorflow tfrecoder read write
# write in tfrecord import tensorflow as tf import os os.environ[' FLAGS = tf.app.flags.FLAGS tf.app ...
- postgres —— 有序集与假象聚集
有序集 -- 有序集.分组后,按给定顺序排序,再进行计算 SELECT region, percentile_disc(0.5) WITHIN GROUP (order by production) ...
- springboot 打成的jar包在ClassLoader().getResource方法读取文件为null
1.属性文件如下: 10001=错误 2.文件读取主要代码 // getResource方式 URL resourceURI = getClass().getClassLoader().getReso ...
- windows查看端口被占用情况
首先,使用cmd命令打开CMD命令窗口 使用下面的命令来查看某端口被占用的情况,以8035为例: netstat -ano|findstr " 结果如下图: 最后一列的6532为PID号,根 ...
- 20199302《Linux内核原理与分析》第十二周作业
ShellShock攻击实验 什么是ShellShock? Shellshock,又称Bashdoor,是在Unix中广泛使用的Bash shell中的一个安全漏洞,首次于2014年9月24日公开.许 ...
- 微信小程序中登录操作-----与-----引用
login.wxml <view> <!-- <image src="./88.png"></image> --> # 在当前目录下 ...
- 2019.12.10 switch(){ case: }
if 适合判断范围 switch 适合判断某个值 两种方法: import java.util.Scanner; class Demo02 { public static void main(Stri ...
- 正确创建本地C++发布构建PDBS
在调试版本中遇到的一个问题是编译本地的C++应用程序.例如,许多局部变量消失了,因为代码生成器没有将它们放在堆栈上,而是将它们放在寄存器中,就像在调试生成中发生的那样.此外,release积极地构建对 ...