Spring Boot統合Spring Data Redis-統合手順

9124 ワード

    SpringBoot      Redis,      SpringBoot Redis        ,  SpringBoot Redis,

       Spring JPA  Spring Data      ,         ,Spring Data JPA,        

     ,  SpringBoot Data Redis,          ,    Redis  ,  Redis       ,

  SpringBoot Data             ,  JDBC,  MongoDB,     SpringData      ,

         ,                    ,                  ,    

       ,SpringData Redis     ,Spring Data Redis   Spring Data      ,       

   ,      Redis     ,        ,          ,          ,    

SpringBoot   Redis,       eclipse,          ,           ,        

       ,   pom       ,       ,      ,       Spring Data Redis,   

            ,           



	org.springframework.boot
	spring-boot-starter-data-redis


        ,              ,spring-boot-starter-data-redis,           ,

   Maven Dependecies,    ,     Spring-data-redis   jar ,  Spring data  Redis 

    jar ,         jedis jar ,  jedis jar       ,  jedis         

JAVA  redis   API,     spring  redis        jar ,     jar     ,   

  spring data redis       ,              ,          ,     Spring

  Redis   ,     ,        ,     Spring  ,      Redis       ,

          ,            ,        Redis   ,                

Jedis Pool Config   ,             ,       Jedis Pool Config,         

      ,       ,           ,         Jedis    ,         ,

Jedis Client,        ,Jedis Cluster,       SpringBoot      Redis,    ,    

         ,   SpringBoot  ,            ,              ,    

        ,       ,         ,               ,        ,   

          ,              ,    config  ,        ,          ,

    RedisConfig,            ,    Redis       ,             ,Redis 

   ,RedisConfig  ,              ,      xml     ,        ,     

      ,       ,      SpringBoot      ,     ,           @Annotation,

@Configuration,    Annotation     ,    ,   SpringBoot     ,         ,    

   ,          ,   Redis     ,            ,        ,       

   ,            ,      RedisTemplate         ,                

  ,               ,          ,          ,        ,JedisPoolConfig

     ,           ,           ,  JedisPoolConfig

/**
 * 1.  JedisPoolConfig  。              
 *
 */
@Bean
public JedisPoolConfig jedisPoolConfig(){
	JedisPoolConfig config = new JedisPoolConfig();
	//     
	config.setMaxIdle(10);
	//     
	config.setMinIdle(5);
	//     
	config.setMaxTotal(20);
	
	return config;
}

          ,             ,       JedisPoolConfig      ,      ,

   jedisPoolConfig,          SpringBoot        ,          ,       

@Bean   ,           ,           ,SpringBoot    ,  @Configuration    ,

          @Bean     ,     ,@Bean           Bean        ,     

        ,  SpringIOC      ,      ,              pool  ,            ,

         JedisPoolConfig     ,              ,              ,     

setMaxIdle,            ,          ,minIdle,       ,             ,

                ,         ,               ,SpringBoot       ,

     JedisPoolConfig  IOC   ,        ,        JedisConnectionFactory       

/**
 * 2.  JedisConnectionFactory:  redis    
 */
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
	JedisConnectionFactory factory = new JedisConnectionFactory();
	//          
	factory.setPoolConfig(config);
	//    Redis   
	//    
	factory.setHostName("10.40.8.152");
	//  
	factory.setPort(6379);
	
	return factory;
}

         ,          JedisPoolConfig,  redis       ,           ,   

    ,     ,     JedisConnectionFactory,             ,     Spring      ,

       @Bean,    JedisConnectionFactory,            ,                ,

                   ,     Spring IOC       ,   SpringBoot         ,

        ,    ,          ,    JedisPoolConfig    ,               ,

         Redis     ,             ,       Redis   ,       Redis   

  ,      Redis       ,   Redis   Linux  ,       Redis    ,         

Redis   ,Redis        ,    Redis   ,            ,    6379,       ,

          database,  Redis    ,  16  ,        0 15,        ,         

0  ,     0 ,        ,  return factory,       JedisConnectionFactory,        

   ,            ,       SpringBoot Redis,  Redis         ,  RedisTemplate,

       ,         API,     Redis  ,   Spring  Redis   ,             ,

       Redis     ,          ,    RedisTemplate     ,         ,

      Redis   ,     @Bean,     RedisTemplate,               ,  Key

 String   ,value     Object,      redisTemplate

/**
 * 3.  RedisTemplate:    Redis     
 */
@Bean
public RedisTemplate redisTemplate(JedisConnectionFactory factory){
	RedisTemplate template = new RedisTemplate<>();
	//  
	template.setConnectionFactory(factory);
	
	// key      
	template.setKeySerializer(new StringRedisSerializer());
	// value      
	template.setValueSerializer(new StringRedisSerializer());
	
	return template;
}

           ,         JedisConnectionFactory,          ,    Redis

    ,          ,              ,      JedisConnectionFactory  ,

    ,               ,RedisTemplate    ,     API, Redis     ,

          key value          ,        ,                ,

org\springframework\data\spring-data-redis\1.8.11.RELEASE\spring-data-redis-1.8.11.RELEASE.jar

org.springframework.data.redis.serializer

       ,       ,            Redis         , key value        

  ,         ,    ,      Spring  Redis   ,         ,  Redis ,   ,

               Redis ,               ,        JSON   ,   

JSON    ,        JSON,          ,  Redis    ,Redis    ,   key value 

  ,         ,         ,       String    List  ,  String   List  ,

                   ,           ,              ,      

    ,                  ,     Spring Data Redis   ,          

    ,          ,                ,            ,         

  ,          ,       String    , StringRedisSerializer,            ,

                     ,  ,     key,     value,            

    ,        

template.setKeySerializer(new StringRedisSerializer());

    key      ,   key            ,                 ,    

        ,          value        

template.setValueSerializer(new StringRedisSerializer());

  new          ,    SpringBoot   Redis,           ,           ,

       RedisTemplate,              ,  RedisTemplate,      ,         

      ,  Spring Data Redis    

  4.0.0
  com.learn
  spring-boot-redis
  0.0.1-SNAPSHOT
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.12.RELEASE
  
  
	
		UTF-8
		UTF-8
		1.8
	
  
  
  
    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        org.springframework.boot
        spring-boot-starter-data-redis
    

package com.learn.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import redis.clients.jedis.JedisPoolConfig;

/**
 *    Redis        
 */
@Configuration
public class RedisConfig {

	/**
	 * 1.  JedisPoolConfig  。              
	 *
	 */
	@Bean
	public JedisPoolConfig jedisPoolConfig(){
		JedisPoolConfig config = new JedisPoolConfig();
		//     
		config.setMaxIdle(10);
		//     
		config.setMinIdle(5);
		//     
		config.setMaxTotal(20);
		
		return config;
	}
	
	/**
	 * 2.  JedisConnectionFactory:  redis    
	 */
	@Bean
	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
		JedisConnectionFactory factory = new JedisConnectionFactory();
		//          
		factory.setPoolConfig(config);
		//    Redis   
		//    
		factory.setHostName("10.40.8.152");
		//  
		factory.setPort(6379);
		factory.setPassword("1234");
		
		return factory;
	}
	
	/**
	 * 3.  RedisTemplate:    Redis     
	 */
	@Bean
	public RedisTemplate redisTemplate(JedisConnectionFactory factory){
		RedisTemplate template = new RedisTemplate<>();
		//  
		template.setConnectionFactory(factory);
		
		// key      
		template.setKeySerializer(new StringRedisSerializer());
		// value      
		template.setValueSerializer(new StringRedisSerializer());
		
		return template;
	}
}