一、说明

  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. java 获取最近7天 最近今天的日期

    private static Date getDateAdd(int days){ SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-d ...

  2. 实验八 《Coderxiaoban团队》团队作业4:基于原型的团队项目需求调研与分析

    实验八 <Coderxiaoban团队>团队作业4:基于原型的团队项目需求调研与分析 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验八 团队作业4:基于 ...

  3. robot framework 笔记(四),使用时遇到的问题

    背景: 使用rf遇到的一些问题汇总 一:跑WEBUI的时候报错: [ WARN ] Keyword 'Capture Page Screenshot' could not be run on fail ...

  4. 通过vue-cli搭建vue项目

    一.搭建环境 安装node.js 从node.js官网下载并安装node,安装过程很简单,一路“下一步”就可以了.安装完成之后,打开命令行工具(win+r,然后输入cmd),输入 node -v,如下 ...

  5. 10 分钟了解 Actor 模型

    http://www.moye.me/2016/08/14/akka-in-action_actor-model/ 过去十几年CPU一直遵循着摩尔定律发展,单核频率越来越快,但是最近这几年,摩尔定律已 ...

  6. JavaScript Array 對象

    JavaScript array 對象 array對象,是用於在單個變量中存儲多個值的一種變量類型. 創建array對象的語法: new array(); new array(size); new a ...

  7. LeetCode 1102. Path With Maximum Minimum Value

    原题链接在这里:https://leetcode.com/problems/path-with-maximum-minimum-value/ 题目: Given a matrix of integer ...

  8. 在WinDbg里使用MEX调试扩展

    简介 针对WinDbg的MEX调试扩展可以帮助您简化常见的调试器任务,并为调试器提供强大的文本筛选功能.此扩展被Microsoft支持工程师广泛用于解决流程应用程序的故障. 下载&安装 下载m ...

  9. apisix 基于openresty 的api 网关

    apisix 是由openresty 团队开发并开源的微服务api gateway,还不错,官方文档也比较全,同时这个也是一个不错的学习openresty 的项目 以下为来自官方的架构图 插件加载 插 ...

  10. C# 读取Excel 单元格是日期格式

    原文地址:https://www.cnblogs.com/liu-xia/p/5230768.html DateTime.FromOADate(double.Parse(range.Value2.To ...