一、说明

  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. 使用Gerrit发送测试邮件

    使用Gerrit发送测试邮件 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装HTTP服务 1>.安装HTTP服务 [root@gerrit.yinzhengjie.o ...

  2. ThreadLocal源码原理以及防止内存泄露

    ThreadLocal的原理图: 在线程任务Runnable中,使用一个及其以上ThreadLocal对象保存多个线程的一个及其以上私有值,即一个ThreadLocal对象可以保存多个线程一个私有值. ...

  3. java怎么比较两个实体类的属性值

    分享一下比较两个实体类的工具包 package cn.mollie.utils; import java.beans.Introspector; import java.beans.PropertyD ...

  4. 绑定事件 .on("click",function(){})和.click(function(){})

    1.$(element).click(function(){ }) 2.$(element).on("click",function(){ }) 在一般的情况之下第1种和第2种没什 ...

  5. [Ignatius and the Princess III] 整数的无序拆分(DP + 生成函数)

    整数的有序拆分就是隔板法,无序拆分则有两种处理方法 DP递推 我们假设P(n,m)P(n,m)P(n,m)是正整数nnn无序拆分为mmm个正整数的方案数 对于某一种拆分,不妨将拆分出来的mmm个数从小 ...

  6. JavaScript基础06——Math对象和日期对象

    内置对象-Math:  Math对象用于执行 数学任务,Math 不像 Date 和 String 那样是对象的类,因此没有构造函数Math().无需创建,直接把Math当成对象使用,就可以调用其所有 ...

  7. codeforces B. Make Them Odd -C++stl之set的使用

    B. Make Them Odd There are nn positive integers a1,a2,…,ana1,a2,…,an. For the one move you can choos ...

  8. POJ P2251 Dungeon Master 题解

    深搜,只不过是三维的. #include<iostream> #include<cstring> #include<cstdio> #include<algo ...

  9. linux学习8 运维基本功-Linux获取命令使用帮助详解

    一.Linux基础知识 1.人机交互界面: a.GUI b.CLI:[login@hostname workdir]# COMMAND 2.命令知识 通用格式:# COMMAND  OPTIONS A ...

  10. 【洛谷P4093】 [HEOI2016/TJOI2016]序列 CDQ分治+动态规划

    你发现只会改变一个位置,所以可以直接进行dp 具体转移的话用 CDQ 分治转移就好了~ #include <bits/stdc++.h> #define N 100006 #define ...