zoukankan      html  css  js  c++  java
  • spring boot 整合redis

    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: #如果是redis集群使用此配置
            nodes:
              - 127.0.0.1:9001
              - 127.0.0.1:9002
              - 127.0.0.1:9003
          host: 172.16.0.7 #如果是单机redis使用一下配置
          port: 6379
          password:  123
    

      

    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 还是挺全的 

  • 相关阅读:
    KnowYoueSelf
    计算机组成原理--海明码的编码和校验方法(易懂)
    html5新特性
    web前端性能优化
    web标准
    《王者归来》笔记-安全&性能01
    vue核心最基本功能
    BOM&DOM
    JavaScript-语法
    前端04
  • 原文地址:https://www.cnblogs.com/longc-pub/p/11093787.html
Copyright © 2011-2022 走看看