springboot系列十、springboot整合redis、多redis数据源配置
一、简介
Redis 的数据库的整合在 java 里面提供的官方工具包:jedis,所以即便你现在使用的是 SpringBoot,那么也继续使用此开发包。
二、redidTemplate操作
在 Spring 支持的 Redis 操作之中提供有一个 RedisTemplate 处理程序类,利用这个类可以非常方便的实现 Redis 的各种基本数 据操作。
1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置yml
spring:
redis:
host: 47.52.199.52
port:
password:
timeout:
database:
jedis:
pool:
max-active:
max-idle:
min-idle:
max-wait:
3、使用示例
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class DemoApplicationTests { @Resource
private RedisTemplate<String, String> redisTemplate; @Test
public void testRedis(){
redisTemplate.opsForValue().set("xing","12345678");
System.out.println(redisTemplate.opsForValue().get("xing"));
} }
三、配置多个Redis
由于在项目的实际开发过程之中 Redis 的使用会非常的频繁, 那么就有可能出现这样一种问题:现在的项目里面要求连接两 个 Redis 数据库。SpringBoot 里面针对于 Redis 的连接配置本质上只提供有一个连接配置项,那么如果你真的需要进行更多的 Redis 的连接配置,那么就需要自己来进行 Redis 的创建管理了。相当于直接使用spring-data-redis。
1、引入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、配置yml
spring:
redis-two:
host: 47.52.199.52
port: 6379
password: 123456
timeout: 1000
database: 0
pool:
max-active: 10
max-idle: 8
min-idle: 2
max-wait: 100
3、初始化连接,创建实例
RedisTwoConfig.java
package com.example.demo.config.redisConfig; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig; @Configuration
public class RedisTwoConfig { @Bean("redisTwo")
public RedisTemplate<String, Object> getRedisTemplate(
@Value("${spring.redis-two.host}") String hostName,
@Value("${spring.redis-two.password}") String password,
@Value("${spring.redis-two.port}") int port,
@Value("${spring.redis-two.database}") int database,
@Value("${spring.redis-two.pool.max-active}") int maxActive,
@Value("${spring.redis-two.pool.max-idle}") int maxIdle,
@Value("${spring.redis-two.pool.min-idle}") int minIdle,
@Value("${spring.redis-two.pool.max-wait}") long maxWait) { System.out.println(hostName+port+password);
RedisConnectionFactory factory = this.getRedisConnectionFactory(
hostName, password, port, maxActive, maxIdle, minIdle, maxWait,
database); // 建立Redis的连接
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer()); // key的序列化类型
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // value的序列化类型
return redisTemplate;
} public RedisConnectionFactory getRedisConnectionFactory(String hostName,
String password, int port, int maxActive, int maxIdle, int minIdle,
long maxWait, int database) { // 是负责建立Factory的连接工厂类
JedisConnectionFactory jedisFactory = new JedisConnectionFactory();
jedisFactory.setHostName(hostName);
jedisFactory.setPort(port);
jedisFactory.setPassword(password);
jedisFactory.setDatabase(database);
JedisPoolConfig poolConfig = new JedisPoolConfig(); // 进行连接池配置
poolConfig.setMaxTotal(maxActive);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxWaitMillis(maxWait);
jedisFactory.setPoolConfig(poolConfig);
jedisFactory.afterPropertiesSet(); // 初始化连接池配置
return jedisFactory;
}
}
4、使用示例
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class RedisTest {
@Resource
private RedisTemplate<String, Object> redisTemplate; @Resource
private StringRedisTemplate stringRedisTemplate; @Resource(name = "redisTwo")
private RedisTemplate<String, Object> redisTemplate2; @Test
public void testRedis2(){
UserPO userPO = new UserPO();
userPO.setAge(25);
userPO.setName("小明");
userPO.setUid(111L);
redisTemplate2.opsForValue().set("user25",userPO);
UserPO result = (UserPO) redisTemplate2.opsForValue().get("user25");
System.out.println(result);
}
}
springboot系列十、springboot整合redis、多redis数据源配置的更多相关文章
- SpringBoot系列十:SpringBoot整合Redis
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Redis 2.背景 Redis 的数据库的整合在 java 里面提供的官方工具包:jed ...
- SpringBoot系列(十二)过滤器配置详解
SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...
- springBoot系列教程04:mybatis及druid数据源的集成及查询缓存的使用
首先说下查询缓存:查询缓存就是相同的数据库查询请求在设定的时间间隔内仅查询一次数据库并保存到redis中,后续的请求只要在时间间隔内都直接从redis中获取,不再查询数据库,提高查询效率,降低服务器负 ...
- SpringBoot系列十二:SpringBoot整合 Shiro
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Shiro 2.具体内容 Shiro 是现在最为流行的权限认证开发框架,与它起名的只有最初 ...
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...
- springboot系列十四、自定义实现starter
一.starter的作用 当我们实现了一个组建,希望尽可能降低它的介入成本,一般的组建写好了,只要添加spring扫描路径加载spring就能发挥作用.有个更简单的方式扫描路径都不用加,直接引入jar ...
- springboot系列十五、springboot集成PageHelper
一.介绍 项目中经常会遇到分页,PageHelper为我们解决了这个问题.本质上实现了Mybatis的拦截器,作了分页处理. 二.配置PageHelper 1.引入依赖 pagehelper-spri ...
- springboot系列十二、springboot集成RestTemplate及常见用法
一.背景介绍 在微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.N ...
- SpringBoot(十)-- 整合MyBatis
1.pom.xml 配置maven依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <ar ...
随机推荐
- JS循环中使用bind函数的参数传递问题
JS循环中使用bind函数的参数传递问题,问题代码如下: for (var sc in result) { var tempp = '<div class="sidebar_todo_ ...
- 利用spring boot+vue做的一个博客项目
技术栈: 后端 Springboot druid Spring security 数据库 MySQL 前端 vue elementUI 项目演示: GitHub地址: 后端:https://githu ...
- I/O多路复用详解
要想完全理解I/O多路复用,需先要了解I/O模型: 一.五种I/O模型 1.阻塞I/O模型 最流行的I/O模型是阻塞I/O模型,缺省情形下,所有套接口都是阻塞的.我们以数据报套接口为例来讲解此模型(我 ...
- 【CH5302】金字塔 区间DP
题目大意:给定一棵树,树上点有标记,给定一棵树的\(dfs\)序标记序列,求有多少种可能的子树形态.(子树之间有序) 这是一道区间计数类DP,涉及到树的\(dfs\)序. 这道题区间的划分点 \(k\ ...
- 有趣的canvas
最近看了一本canvas的书,里面对canvas的一些基本知识讲的很详细.相比于一个div加点颜色,我臭屁的觉得使用canvas画长方形正方形圆形之类的是大才小用. 下面我放几个canvas还不错的功 ...
- Machine Learning Netsite
Google: Machine Learning
- (next_permutation) 排列2 hdu 1716
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 用webstorm来开发微信小程序之less的配置
1.安装less. 安装好node之后,打开运行-->cmd-->进入安装node的文件夹目录-->输入 npm install -g less. 然后自动就会在C:\Users\A ...
- nginx下后端节点realserverweb健康检测模块ngx_http_upstream_check_module
本文章收录做资料使用,非本人原创,特此说明. 公司前一段对业务线上的nginx做了整理,重点就是对nginx上负载均衡器的后端节点做健康检查.目前,nginx对后端节点健康检查的方式主要有3种,这里列 ...
- django基础之数据库操作
Django 自称是“最适合开发有限期的完美WEB框架”.本文参考<Django web开发指南>,快速搭建一个blog 出来,在中间涉及诸多知识点,这里不会详细说明,如果你是第一次接触D ...