1. 程式人生 > >springboot之redis的配置

springboot之redis的配置

當看這邊文章之前,相信大家對redis已經能夠熟悉的運用,對springmvc也有一定的瞭解了。redis,在個人的觀點中是一個很優秀的快取組建,無論是單用,還是做叢集用,輕便,簡單,持久,都很不錯。言歸正傳,看下錶題,這裡講一下springboot配置redis是如何實現的。

首先看一下,spring mvc 中對應的redis的配置檔案,檔案如下

 <context:property-placeholder location="/WEB-INF/property/redis.properties" />
   
  <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
    <property name="maxIdle" value="${redis.maxIdle}" /> 
    <property name="maxActive" value="${redis.maxActive}" /> 
    <property name="maxWait" value="${redis.maxWait}" /> 
    <property name="minIdle" value="${redis.minIdle}" /> 
  </bean> 
     
  <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
    p:host-name="${redis.host}"
    p:port="${redis.port}"
    p:password="${redis.pass}" 
    p:pool-config-ref="poolConfig"/> 
     
  <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 
    <property name="connectionFactory"   ref="connectionFactory" /> 
  </bean>  

如上面的程式碼所示:redis.properties存放redis引數,id為poolConfig的bean和id為connectionfactory的bean分別讀取配置檔案。因為springboot是為了簡化操作,零配置的輕型框架,那麼這裡對應的配置檔案如何在spingboot中實現呢?  在我們學習語言的初始,提到萬物皆物件,這裡也不例外,對應的spingboot的配置是javaconfig合成的。這裡對應的bean,我個人為了簡化記憶,理解成一個物件,這個物件中會有物件的變數,讓這些變數去載入你所需要傳的引數。物件對當前物件中的引數持有引用,此時拿到這些引數,就可以來組裝我們的配置了。

下面我用一個例子來說明,根據上面的程式碼,我們分析下對應的springboot中該如何完成

首先看引數如何組裝被載入。第一個bean,poolconfig,下面的四個屬性,載入到jedispoolconfig中的幾個變數。這裡我命名一個類,比如pool,裡面有物件的變數,如maxidel,maxactive等,下面的connectionfactory用到的引數對應的有第一個bean的,這裡載入了所有屬性,這裡需要修改下了,如下:

@Component
@ConfigurationProperties(prefix = "admin.redis")
public class RedisProperties {
	
	 private int database;  
     private String host;  
     private String password;  
     private int port;  
     private RedisProperties.Pool pool = new Pool();
      
     public RedisProperties() {  
     }  
      
     public String getHost() {  
         return this.host;  
     }  
      
     public void setHost(String host) {  
         this.host = host;  
     }  
      
     public int getPort() {  
         return this.port;  
     }  
      
     public void setPort(int port) {  
         this.port = port;  
     }  
      
     public String getPassword() {  
         return this.password;  
     }  
      
     public void setPassword(String password) {  
         this.password = password;  
     }  
      
     public RedisProperties.Pool getPool() {  
         return this.pool;  
     }  
      
     public void setPool(RedisProperties.Pool pool) {  
         this.pool = pool;  
     }  
      
     public int getDatabase() {  
         return this.database;  
     }  
      
     public void setDatabase(int database) {  
         this.database = database;  
     }
      
     public static class Pool {  
         private int maxIdle = 8;  
         private int minIdle = 0;  
         private int maxActive = 8;  
         private int maxWait = -1;  
      
         public Pool() {  
         }  
      
         public int getMaxIdle() {  
             return this.maxIdle;  
         }  
      
         public void setMaxIdle(int maxIdle) {  
             this.maxIdle = maxIdle;  
         }  
      
         public int getMinIdle() {  
             return this.minIdle;  
         }  
      
         public void setMinIdle(int minIdle) {  
             this.minIdle = minIdle;  
         }  
      
         public int getMaxActive() {  
             return this.maxActive;  
         }  
      
         public void setMaxActive(int maxActive) {  
             this.maxActive = maxActive;  
         }  
      
         public int getMaxWait() {  
             return this.maxWait;  
         }  
      
         public void setMaxWait(int maxWait) {  
             this.maxWait = maxWait;  
         }  
     }  
	
}

把類pool定義為redisproperties的一個變數。

上面的引數已經定義好,並且能夠確認被載入進來,那麼如何拿這些引數去配置呢?程式碼如下

@Configuration
public class RedisConfig {
	@Autowired
	private RedisProperties properties;
	
	@Bean  
    public RedisConnectionFactory jedisConnectionFactory(){  
        JedisPoolConfig poolConfig = new JedisPoolConfig();  
        poolConfig.setMaxTotal(properties.getPool().getMaxActive());  
        poolConfig.setMaxIdle(properties.getPool().getMaxIdle());  
        poolConfig.setMaxWaitMillis(properties.getPool().getMaxWait());  
        poolConfig.setTestOnBorrow(true);  
        poolConfig.setTestOnCreate(true);  
        poolConfig.setTestWhileIdle(true);  
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);  
        jedisConnectionFactory.setHostName(properties.getHost());  
        if(null != properties.getPassword()){  
            jedisConnectionFactory.setPassword(properties.getPassword());  
        }  
        jedisConnectionFactory.setPort(properties.getPort());  
          
        //其他配置,可再次擴充套件  
          
        return jedisConnectionFactory;  
    }  
  
    @Bean(name="redisTemplate")  
    public RedisTemplate redisTemplate(){  
        RedisTemplate redisTemplate = new RedisTemplate();  
        
        redisTemplate.setConnectionFactory(jedisConnectionFactory());  
        
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        
        redisTemplate.afterPropertiesSet();
        redisTemplate.setEnableTransactionSupport(true);
        
        return redisTemplate;  
    }  

RedisConnectionFactory 是對應springmvc中配置中定義的第二個bean,
 當所有的初始化結束後,那麼我們的工作就完成了。

不正之處,敬請指正。