首页 > 编程知识 正文

redis和java,redis集群和哨兵的区别

时间:2023-05-06 11:20:18 阅读:133150 作者:1891

redis工具类

打包资源; import com.Alibaba.fast JSON.JSON; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.jedis; import redis.clients.jedis.Jedi spool; import redis.clients.jedis.jedis poolconfig; import java.io.Serializable; import java.util.ArrayList; import java.util.List; publicclassredisutils { privatestaticfinalloggerlog=logger factory.getlogger (redis utils.class ); //Redis服务器地址privatestaticfinalstringredishost=load properties.getproperty (' redis.host ' ); //Redis服务端口privatestaticfinalintegerredisport=integer.parseint (load properties.getproperty (' redis.port ' ) ); //Redis密码privatestaticfinalstringredispassword=load properties.getproperty (' redis.password ' ); //围栏信息数据库indexprivatestaticfinalintfenceindex=integer.parseint (load properties.getproperty (' redis.fence index //设备信息数据库indexprivatestaticfinalintvinmsgindex=integer.parseint (load properties.getproperty (redis.vinmsgindex ' ) Redis可连接实例的最大数量privatestaticfinalintegerredismaxtotal=integer.parseint (load properties.getproperty (redis.max total 默认值为8 privatestaticfinalintegerredismaxidle=integer.parseint ) load properties.get properties//reids等待可用连接的最长时间。 默认值为-1(以毫秒为单位),表示不超时。 私有属性信息管理器redismaxwaitnillis=integer.parseint (load properties.getproperty (redis.maxwaitnillis ' ) ) //Redis超时时间privatestaticfinalintegerredistimeout=integer.parseint (load properties.getproperty (' redis.time out ) 私密性统计信息booleanredistestonborrow=true; //privatestaticjedisfencejedispool=null; //privatestaticjedisvinmsgjedispool=null; 私密性统计信息=空值; 静态{ try } jedispoolconfigconfig=newjedispoolconfig (config.setmaxtotal (redismaxtotal; 配置. set maxidle (redis maxidle; config.setmaxwaitmillis (redismaxwaitnillis; config.settestonborrow (redistestonborrow; EDI spool=newjedispool (配置,redisHost,redisPort,redisTimeOut ); }catch(exceptione ) log.warn )、e.getMessage )、redis初始化失败(); } } /** *

JSON数据,转成Object对象 */ public static <T> T fromJson(String json, Class<T> clazz){ try{ return JSON.parseObject(json, clazz); }catch (Exception e){ log.warn("redis value fromJson error. msg: {}, value: {}, class: {}", e.getMessage(), json, clazz); } return null; } /** * Object转成JSON数据 */ public static String toJson(Object object){ try { if (object instanceof Integer || object instanceof Long || object instanceof Float || object instanceof Double || object instanceof Boolean || object instanceof String) { return String.valueOf(object); } return JSON.toJSONString(object); }catch (Exception e){ log.warn("value toJson error. msg: {}, object: {}", e.getMessage(), object); } return null; } /** * redis get */ public static <T> T get(Integer index, String key, Class<T> clazz){ try{ if(jedisPool!=null) { Jedis jedis = jedisPool.getResource(); jedis.select(index); String value = jedis.get(key); return value == null ? null : fromJson(value, clazz); } }catch (Exception e){ log.warn(" redis get operation error. msg: {}, key: {}, class: {}",e.getMessage(), key, clazz); } return null; } /** * redis hget */ public static <T> T hget(Integer index, String name, String key, Class<T> clazz){ try{ if(jedisPool!=null) { Jedis jedis = jedisPool.getResource(); jedis.select(index); String value = jedis.hget(name, key); return value == null ? null : fromJson(value, clazz); } }catch (Exception e){ log.warn(" redis hget operation error. msg: {}, key: {}, class: {}",e.getMessage(), key, clazz); } return null; } /** * redis get keys */ public static List<String> getKeys(Integer index){ try{ if(jedisPool!=null) { Jedis jedis = jedisPool.getResource(); jedis.select(index); return new ArrayList<String>(jedis.keys("*")); } }catch (Exception e){ log.warn(" redis getKeys operation error. msg: {}",e.getMessage()); } return null; } /** * redis get keys by prefix */ public static List<String> getPrefixKeys(Integer index, String prefix){ try{ if(jedisPool!=null) { Jedis jedis = jedisPool.getResource(); jedis.select(index); return new ArrayList<String>(jedis.keys(prefix + "*")); } }catch (Exception e){ log.warn(" redis getPrefixKeys operation error. msg: {} prefix: {}",e.getMessage(), prefix); } return null; } public static Boolean exists(Integer index, String key){ try{ if(jedisPool!=null){ Jedis jedis = jedisPool.getResource(); jedis.select(index); return jedis.exists(key); } }catch (Exception e){ log.warn(" redis exists operation error. msg: {} index: {} key: {}", e.getMessage(), index, key); } return false; } /** * close redispool */ public static void close(){ if(jedisPool!=null){ jedisPool.close(); } }}

redis 哨兵模式连接

private static JedisSentinelPool jedisPool = null; static { try { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(redisMaxTotal); config.setMaxIdle(redisMaxIdle); config.setMaxWaitMillis(redisMaxWaitNillis); config.setTestOnBorrow(redisTestOnBorrow); //哨兵信息 Set<String> sentinels = new HashSet<String>(Arrays.asList(redisHost.split(","))); //jedisPool = new JedisPool(config, redisHost, redisPort, redisTimeOut); //哨兵模式连接 第一个参数是配置给哨兵的服务名称 第二个是哨兵信息 第三个是连接配置 最后是密码 jedisPool = new JedisSentinelPool("sentine", sentinels, config,redisPassword); } catch (Exception e) { log.warn("Redis初始化失败{}", e.getMessage()); } }

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。