spring boot 整合redis
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了spring boot 整合redis,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6418字,纯文字阅读大概需要10分钟。
内容图文
1、添加pom依赖:
注意:建议不要使用spring-boot-starter-redis,此包Jun, 2017后已停止维护
示例:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>${spring-boot.version}</version> </dependency>
2、redis配置:
示例:
spring: redis: database: 1 jedis: pool: max-active: 8 max-wait: -1 max-idle: 8 min-idle: 0 timeout: 10000 cluster: nodes: - 127.0.0.1:9001 - 127.0.0.1:9002 - 127.0.0.1:9003
3、redis使用:
(1)spring-boot已经定义了redisTemplate、stringRedisTemplate两个bean,可以直接使用也可自行定义
package org.springframework.boot.autoconfigure.data.redis; import java.net.UnknownHostException; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Redis support. * * @author Dave Syer * @author Andy Wilkinson * @author Christian Dupuis * @author Christoph Strobl * @author Phillip Webb * @author Eddú Meléndez * @author Stephane Nicoll * @author Marco Aust * @author Mark Paluch */ @Configuration @ConditionalOnClass(RedisOperations.class) @EnableConfigurationProperties(RedisProperties.class) @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class }) public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean public StringRedisTemplate stringRedisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }
(2)此处自行定义了一个泛型为<String,Object>的redisTemplate(个人感觉更实用),示例如下:
package com.longc.core.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * redis配置 * Created by log.chang on 2019/6/26. */ @Configuration @ConditionalOnClass(RedisOperations.class) @EnableConfigurationProperties(RedisProperties.class) public class RedisConfig { /** * 定义redisTemplate * 如果在配置文件中定义配置如下: * <bean id="jedisConnectionFactory" * class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> * <property name="hostName" value="${redis.hostName}"/> * <property name="port" value="${redis.port}"/> * <property name="password" value="${redis.password}"/> * <property name="database" value="${redis.database}"/> * <property name="poolConfig"> * <bean class="redis.clients.jedis.JedisPoolConfig"> * <property name="maxTotal" value="${redis.pool.maxTotal}"/> * <property name="maxIdle" value="${redis.pool.maxIdle}"/> * <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/> * <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/> * <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/> * <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> * </bean> * </property> * </bean> * <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> * <property name="connectionFactory" ref="jedisConnectionFactory"/> * <property name="keySerializer"> * <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> * </property> * <property name="valueSerializer"> * <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> * </property> * <property name="hashKeySerializer"> * <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> * </property> * <property name="hashValueSerializer"> * <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> * </property> * </bean> */ @Bean @SuppressWarnings("all") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式采用jackson template.setValueSerializer(jackson2JsonRedisSerializer); // hash的value序列化方式采用jackson template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
(3)封装redis工具类
可以参考 https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html 还是挺全的
内容总结
以上是互联网集市为您收集整理的spring boot 整合redis全部内容,希望文章能够帮你解决spring boot 整合redis所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。