Spring boot 手动配置redis

本文介绍了如何在Spring Boot应用中手动配置Redis连接池。通过MyRedisProperties.java类读取Redis配置,然后在RedisConfig.java中进行详细设置,最后通过AppTest.java测试用例验证配置的正确性。


MyRedisProperties.java,读取redis配置

package com.redis.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(
 prefix = "my.redis"
)
public class MyRedisProperties {
	 private int database = 0;
	 private String host = "localhost";
	 private String password;
	 private int port = 6379;
	 private MyRedisProperties.Pool pool;
	 private MyRedisProperties.Sentinel sentinel;
	
	 public MyRedisProperties() {
	 }
	
	 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 MyRedisProperties.Pool getPool() {
	     return this.pool;
	 }
	
	 public void setPool(MyRedisProperties.Pool pool) {
	     this.pool = pool;
	 }
	
	 public int getDatabase() {
	     return this.database;
	 }
	
	 public void setDatabase(int database) {
	     this.database = database;
	 }
	
	 public MyRedisProperties.Sentinel getSentinel() {
	     return this.sentinel;
	 }
	
	 public void setSentinel(MyRedisProperties.Sentinel sentinel) {
	     this.sentinel = sentinel;
	 }
	
	 public static class Sentinel {
	     private String master;
	     private String nodes;
	
	     public Sentinel() {
	     }
	
	     public String getMaster() {
	         return this.master;
	     }
	
	     public void setMaster(String master) {
	         this.master = master;
	     }
	
	     public String getNodes() {
	         return this.nodes;
	     }
	
	     public void setNodes(String nodes) {
	         this.nodes = nodes;
	     }
	 }
	
	 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;
	     }
	 }
}



RedisConfig.java,  手动配置redis连接池

package com.redis.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import redis.clients.jedis.JedisPoolConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * Created by zhangzw on 2016/1/8.
 */
@Configuration
public class RedisConfig {
	
	@Bean(name = {"MyRedisProperties"})
    @ConditionalOnMissingBean
    public MyRedisProperties redisProperties() {
        return new MyRedisProperties();
    }

	@Autowired
	@Qualifier("MyRedisProperties")
    private MyRedisProperties 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="myRedisTemplate")
    @ConditionalOnMissingBean(
            name = {"myRedisTemplate"}
    )
    public RedisTemplate redisTemplate(){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        return redisTemplate;
    }



}


AppTest.java 测试用例

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.dubbo.test.api.Application;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class AppTest {
	@Autowired
	@Qualifier("myRedisTemplate")
	private RedisTemplate myRedisTemplate;
	
	@Test
    public void test() throws Exception {
		String key = "key1";
		String value = "value1";
		set(key, value);
		System.out.println(get(key));
    }
	
	public  String get(String key){
        return myRedisTemplate.opsForValue().get(key).toString();
    }

	public  void set(String key, String value){
		myRedisTemplate.opsForValue().set(key, value);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值