SpringBoot如何整合Druid、Redis
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,这篇文章主要介绍SpringBoot如何整合Druid、Redis,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1.整合Druid1.1Druid简介Java程序很大一部分要操
千家信息网最后更新 2025年11月11日SpringBoot如何整合Druid、Redis
这篇文章主要介绍SpringBoot如何整合Druid、Redis,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1.整合Druid
1.1Druid简介
Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池。
Druid 是阿里巴巴开源平台上一个数据库连接池实现,结合了 C3P0、DBCP 等 DB 池的优点,同时加入了日志监控。
Druid 可以很好的监控 DB 池连接和 SQL 的执行情况,天生就是针对监控而生的 DB 连接池。
1.2添加上 Druid 数据源依赖
com.alibaba druid-spring-boot-starter 1.2.8
1.3使用Druid 数据源
server: port: 8080spring: datasource: druid: url: jdbc:mysql://localhost:3306/eshop?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true username: xxx password: xxx driver-class-name: com.mysql.cj.jdbc.Driver initial-size: 10 max-active: 20 min-idle: 10 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 stat-view-servlet: enabled: true login-username: admin login-password: 1234logging: level: com.wyy.spring.Dao: debug
测试一下看是否成功!
package com.wyy.spring;import com.wyy.spring.Dao.StudentMapper;import com.wyy.spring.service.StudentService;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import javax.sql.DataSource;@SpringBootTestclass SpringBoot04ApplicationTests { @Autowired DataSource dataSource; @Test void contextLoads() { System.out.println(dataSource.getClass()); }}打印结果
2.整合redis
2.1添加上 redis依赖
org.springframework.boot spring-boot-starter-data-redis
2.2yml添加redis配置信息
redis: database: 0 host: 120.0.0.0 port: 6379 password: xxxx jedis: pool: max-active: 8 max-wait: -1 max-idle: 8 min-idle: 0 timeout: 10000
2.3 redis 配置类
package com.wyy.spring.conf; import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.interceptor.KeyGenerator;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.data.redis.cache.RedisCacheConfiguration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import org.springframework.util.ClassUtils;import java.lang.reflect.Array;import java.lang.reflect.Method;import java.time.Duration;@Configuration@EnableCachingpublic class RedisConfiguration extends CachingConfigurerSupport { @Bean @Primary /** * 缓存管理器 */ CacheManager cacheManager(RedisConnectionFactory factory) { RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .computePrefixWith(cacheName -> cacheName + ":-cache-:") /*设置缓存过期时间*/ .entryTtl(Duration.ofHours(1)) /*禁用缓存空值,不缓存null校验*/ .disableCachingNullValues() /*设置CacheManager的值序列化方式为json序列化,可使用加入@Class属性*/ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer( new GenericJackson2JsonRedisSerializer() )); /*使用RedisCacheConfiguration创建RedisCacheManager*/ RedisCacheManager manager = RedisCacheManager.builder(factory) .cacheDefaults(cacheConfiguration) .build(); return manager; } public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(factory); RedisSerializer stringSerializer = new StringRedisSerializer(); /* key序列化 */ redisTemplate.setKeySerializer(stringSerializer); /* value序列化 */ redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); /* Hash key序列化 */ redisTemplate.setHashKeySerializer(stringSerializer); /* Hash value序列化 */ redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; @Override public KeyGenerator keyGenerator() { return (Object target, Method method, Object... params) -> { final int NO_PARAM_KEY = 0; final int NULL_PARAM_KEY = 53; StringBuilder key = new StringBuilder(); /* Class.Method: */ key.append(target.getClass().getSimpleName()) .append(".") .append(method.getName()) .append(":"); if (params.length == 0) { return key.append(NO_PARAM_KEY).toString(); } int count = 0; for (Object param : params) { /* 参数之间用,进行分隔 */ if (0 != count) { key.append(','); } if (param == null) { key.append(NULL_PARAM_KEY); } else if (ClassUtils.isPrimitiveArray(param.getClass())) { int length = Array.getLength(param); for (int i = 0; i < length; i++) { key.append(Array.get(param, i)); key.append(','); } } else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) { key.append(param); } else { /*JavaBean一定要重写hashCode和equals*/ key.append(param.hashCode()); count++; return key.toString(); };} @CacheConfig 一个类级别的注解,允许共享缓存的cacheNames、KeyGenerator、CacheManager 和 CacheResolver
@Cacheable 用来声明方法是可缓存的。将结果存储到缓存中以便后续使用相同参数调用时不需执行实际的方 法。直接从缓存中取值
@CachePut 标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法, 并将执行结果以键值对的形式存入指定的缓存中。
@CacheEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空
以上是"SpringBoot如何整合Druid、Redis"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
缓存
序列
数据
整合
数据库
方法
结果
监控
配置
内容
参数
数据源
篇文章
巴巴
很大
相同
成功
之间
价值
优点
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
生成焓 数据库
北邮有关网络安全的硕导
更改数据库权限
学了数据库和py能做什么
甘肃网易网络技术
软件开发去中兴通讯怎么样
汕头pc软件开发收费
网络安全在我心中画展
教育类软件开发公司排名
论文查重系统数据库
江苏高中信息技术网络技术视频
PHPWIND下载软件开发
如何测试客户机到服务器的速度
浪潮科技招聘软件开发
和平精英充值服务器不显示
数据库返回什么为空
数据库联合主键设置外键
黑马零基础学软件开发难吗
网络技术可以赚多少钱
眼镜行业软件开发的服务怎么样
西班牙网络技术
苏州亚东软件开发
杭州app软件开发平台游戏
网易mc服务器组件管理
b站下载的原神是什么服务器
第八代无线网络技术
软件开发和内存
锋青网络技术有限公司
互联网时代的科技 ppt
apex匹配显示正在启动服务器