您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

SpringBoot如何整合Druid、Redis

2024/3/25 6:25:46发布21次查看
1.整合druid1.1druid简介java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池。
druid 是阿里巴巴开源平台上一个数据库连接池实现,结合了 c3p0、dbcp 等 db 池的优点,同时加入了日志监控。
druid是专为监控而生的数据库连接池,能够有效监控数据库连接池的连接情况和sql执行情况。
1.2添加上 druid 数据源依赖<dependency> <groupid>com.alibaba</groupid> <artifactid>druid-spring-boot-starter</artifactid> <version>1.2.8</version> </dependency>
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.整合redis2.1添加上 redis依赖<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>
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<string, object> redistemplate = new redistemplate<string, object>(); 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的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product