博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring 使用redis集群配置
阅读量:5824 次
发布时间:2019-06-18

本文共 4270 字,大约阅读时间需要 14 分钟。

hot3.png

 

redis的java支持已经做的非常好了,所以我们来试着使用这些api来进行redis的操作,首先我们需要操作redis的架包:

redis.clients
jedis
2.7.3

我们需要做的仅仅是在spring配置文件中注入这些基本类,然后自己实现dao,下面是配置文件:

复制代码

      
127.0.0.1:26379

复制代码

上面配置好了以后,我们就相当于将redis集群给映射过来了,下来我们只需要做一些操作,涉及到数据的CRUD.

复制代码

package com.yonyou.hotusm.module.nosql.redis;import redis.clients.jedis.ShardedJedis;//这个接口是操作sharedJedispublic interface RedisDataSource {        public abstract ShardedJedis getRedisClient();    public void returnResource(ShardedJedis shardedJedis);    public void returnResource(ShardedJedis shardedJedis,boolean broken);}

复制代码

实现:

复制代码

package com.yonyou.hotusm.module.nosql.redis;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;@Repository("redisDataSource")public class RedisDataSourceImpl implements RedisDataSource {    private static final Logger log = LoggerFactory.getLogger(RedisDataSourceImpl.class);    @Autowired    private ShardedJedisPool    shardedJedisPool;    public ShardedJedis getRedisClient() {        try {            ShardedJedis shardJedis = shardedJedisPool.getResource();            return shardJedis;        } catch (Exception e) {            log.error("getRedisClent error", e);        }        return null;    }    public void returnResource(ShardedJedis shardedJedis) {        shardedJedisPool.close();        //shardedJedisPool.returnResource(shardedJedis);    }    public void returnResource(ShardedJedis shardedJedis, boolean broken) {        if (broken) {            shardedJedisPool.close();            //shardedJedisPool.returnBrokenResource(shardedJedis);        } else {            shardedJedisPool.close();            //shardedJedisPool.returnResource(shardedJedis);        }    }}

复制代码

 下面就是操作jedis的具体类了:

复制代码

@Repository("jedisTemplate")public class JedisTemplate {    private static final Logger log = LoggerFactory.getLogger(JedisTemplate.class);    @Autowired    private RedisDataSource     redisDataSource;    public void disconnect() {        ShardedJedis shardedJedis = redisDataSource.getRedisClient();        shardedJedis.disconnect();    }    /**     * 设置单个值     *      * @param key     * @param value     * @return     */    public String set(String key, String value) {        String result = null;        ShardedJedis shardedJedis = redisDataSource.getRedisClient();        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.set(key, value);        } catch (Exception e) {            e.printStackTrace();            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    /**     * 获取单个值     *      * @param key     * @return     */    public String get(String key) {        String result = null;        ShardedJedis shardedJedis = redisDataSource.getRedisClient();        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.get(key);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }

复制代码

转载于:https://my.oschina.net/u/2380961/blog/758832

你可能感兴趣的文章
C++ 0X 新特性实例(比较常用的) (转)
查看>>
node生成自定义命令(yargs/commander)
查看>>
各种非算法模板
查看>>
node-express项目的搭建并通过mongoose操作MongoDB实现增删改查分页排序(四)
查看>>
如何创建Servlet
查看>>
.NET 设计规范--.NET约定、惯用法与模式-2.框架设计基础
查看>>
win7 64位+Oracle 11g 64位下使用 PL/SQL Developer 的解决办法
查看>>
BZOJ1997:[HNOI2010]PLANAR——题解
查看>>
BZOJ1014:[JSOI2008]火星人prefix——题解
查看>>
使用Unity3D引擎开发赛车游戏
查看>>
HTML5新手入门指南
查看>>
opennebula 开发记录
查看>>
ubuntu 修改hostname
查看>>
sql 内联,左联,右联,全联
查看>>
C++关于字符串的处理
查看>>
6、Web Service-拦截器
查看>>
Flask 源码流程,上下文管理
查看>>
stream classdesc serialVersionUID = -7218828885279815404, local class serialVersionUID = 1.
查看>>
ZAB与Paxos算法的联系与区别
查看>>
java 读取本地的json文件
查看>>