spring-boot整合redis 总结

4 篇文章 0 订阅

spring-boot整合redis 总结
项目整合redis,如何实现最少写配置代码,从而实现redis客户端实例的注入。Spring提供了一些注解来帮助开发人员减少配置文件的编写。
1,创建RedisConfig类,使用@Configuration 和 @bean来管理 redis的工具类

@Configuration
public class RedisConfig {
@Autowired
private ShardedJedisPool shardedJedisPool;

@Bean(name = "redisCacheManager")
@Scope("singleton")
public RedisCacheManager getRedisCacheManager() {
        return new CacheManagerImpl(shardedJedisPool);
    }
}

使用@Configuration @bean 相当于是在applicationContext.xml文件中配置 一样。当Spring容器扫描到这个类的时候就会将RedisCacheManager的实例放到Ioc容器中。
我们后面就可以通过@Resource注解来调用这个类了。

2,RedisCacheManager只是一个接口,我们需要定义一个实现类,在实现类中来获取ShardedJedis的实例,ShardedJedis的实例是通过ShardedJedisPool对象来获取的。所以关键点
就是就是如何对ShardedJedisPool进行实例化,从而传递给实现类。

public interface RedisCacheManager {
public ShardedJedis getShardJedis();

}

public class CacheManagerImpl implements RedisCacheManager {

private ShardedJedisPool pool;

public CacheManagerImpl() {
}

public CacheManagerImpl(ShardedJedisPool pool) {
    this.pool = pool;
}

@Override
public ShardedJedis getShardJedis() {
    ShardedJedis jedis = pool.getResource();
    return jedis;
}

}

3,最后的问题就是如何给ShardedJedisPool注入实例了,利用spring提供的FactoryBean接口,传入的泛型为ShardedJedisPool,最后在applicationContext.xml文件中配置
。最后实现了ShardedJedisPool实例化。

public class ShardedJedisPoolFactory implements         InitializingBean,DisposableBean,FactoryBean<ShardedJedisPool> {

private JedisPoolConfig poolConfig = null;
private int timeout;
private String password;
private String hostAndPorts;
private int dbIndex;
private ShardedJedisPool pool;
@Override
public void afterPropertiesSet() throws Exception {

}

public ShardedJedisPoolFactory(){
    this.poolConfig = new JedisPoolConfig();
    this.hostAndPorts = "localhost:6379";
    this.dbIndex = 0;
    this.poolConfig.setMaxTotal(200);
    this.poolConfig.setMaxIdle(10);
}

@Override
public ShardedJedisPool getObject() throws Exception {
    if ("".equals(this.hostAndPorts)) return new ShardedJedisPool(new JedisPoolConfig(), new ArrayList<JedisShardInfo>());
    List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
    for (String hp : this.hostAndPorts.split(",")) {
        if ("".equals(hp)) continue;
        String[] host_port_s = hp.split(":");
        JedisShardInfo info = new JedisShardInfo(host_port_s[0], Integer.parseInt(host_port_s[1]));
        jedisShardInfos.add(info);
    }
    pool = new ShardedJedisPool(this.poolConfig, jedisShardInfos);
    return pool;
}

@Override
public Class<?> getObjectType() {
    return ShardedJedisPool.class;
}

@Override
public boolean isSingleton() {
    return true;
}

@Override
public void destroy() throws Exception {
    try{
        pool.destroy();
    }catch (Exception e){

    }
}

public JedisPoolConfig getPoolConfig() {
    return poolConfig;
}

public void setPoolConfig(JedisPoolConfig poolConfig) {
    this.poolConfig = poolConfig;
}

public int getTimeout() {
    return timeout;
}

public void setTimeout(int timeout) {
    this.timeout = timeout;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getHostAndPorts() {
    return hostAndPorts;
}

public void setHostAndPorts(String hostAndPorts) {
    this.hostAndPorts = hostAndPorts;
}

public int getDbIndex() {
    return dbIndex;
}

public void setDbIndex(int dbIndex) {
    this.dbIndex = dbIndex;
}
}


<bean id="jedisPool" class="com.water.core.cache.ShardedJedisPoolFactory">
    <property name="hostAndPorts" value="@redis.address@"/>
    <!--<property name="hostAndPorts" value="127.0.0.1:6379"/>-->
</bean>

ShardedJedisPool实例化的工作也完成了,最后将该实例作为实现类的构造参数传递过去。在实现类中调用该实例获取ShardedJedis的实例。

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值