redis的failover ,redmon安装
Redis  /  管理员 发布于 4年前   739
public class RedisFailToleranceUtil {private static JRedisService jredisService = null;private static ConnectionSpec defaultConnectionSpec = null;private static int current = 1;private static HashMap<String,ConnectionSpec> serverPools = new HashMap<String,ConnectionSpec>();static{ConnectionSpec connectionSpec1 = DefaultConnectionSpec.newSpec("192.168.1.238", 6380, 0,null);ConnectionSpec connectionSpec2 = DefaultConnectionSpec.newSpec("192.168.1.238", 6381, 0,null);serverPools.put("1", connectionSpec1);serverPools.put("2", connectionSpec2);}private String next(){if(current>serverPools.size()){current=1;}int nextIndex = current;current++;return nextIndex+"";}private ConnectionSpec getConnectionSpec(){if(defaultConnectionSpec != null){return defaultConnectionSpec;}jredisService = null;/** * we are working multiple servers * try different servers,util we fetch the first available server pool */HashMap<String,ConnectionSpec> tryServers = new HashMap<String,ConnectionSpec>(serverPools);if(serverPools.size() == 1){return (ConnectionSpec)serverPools.get("1");}while(!tryServers.isEmpty()){ConnectionSpec connectionSpec = tryServers.get(this.next());if(isConnect(connectionSpec)){return connectionSpec;}tryServers.remove(connectionSpec);if(tryServers.isEmpty()){break;}}return null;}/** * try whether the server is available * @param connectionSpec * @return true or false */private boolean isConnect(ConnectionSpec connectionSpec){if(connectionSpec == null){return false;}JRedis jredis = new JRedisClient(connectionSpec.getAddress().getHostAddress(), connectionSpec.getPort());try{jredis.ping();jredis.quit();}catch(Exception e){return false;}return true;}public void initialize(){defaultConnectionSpec = this.getConnectionSpec();if(jredisService == null){synchronized (RedisFailToleranceUtil.class) {jredisService = new JRedisService(defaultConnectionSpec,100);}}}public String getS(String key){this.initialize();String value = null;try {value = DefaultCodec.toStr(jredisService.get(key));} catch (Exception e) {defaultConnectionSpec = null;this.initialize();}return value;}public static void main(String args[]){RedisFailToleranceUtil redis = new RedisFailToleranceUtil();redis.getS("name");try {Thread.sleep(10000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}redis.getS("name");try {Thread.sleep(30000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}redis.getS("name");}}
public class JedisPoolTest extends Assert { private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); private static HostAndPort hnp2 = HostAndPortUtil.getRedisServers().get(1); private static HashMap<String,JedisPool> serverPools = new HashMap<String,JedisPool>(); static{ JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.host, hnp.port, 2000); JedisPool pool2 = new JedisPool(new JedisPoolConfig(), hnp2.host, hnp2.port, 2000); serverPools.put("1", pool1); serverPools.put("2", pool2);} private static int current = 1; static Jedis jredisService=null; private static JedisPool defaultConnectionSpec = null; public void initialize(){defaultConnectionSpec = this.getConnectionSpec();if(jredisService == null){synchronized (RedisFailToleranceUtil.class) {jredisService = defaultConnectionSpec.getResource();}}} public String getS(String key){this.initialize();String value = null;try {value = jredisService.get(key);} catch (Exception e) {defaultConnectionSpec = null;this.initialize();}return value;} public static void main(String[] args) { JedisPoolTest redis = new JedisPoolTest();System.out.println("1 "+redis.getS("foo"));try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("2 "+redis.getS("foo"));try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("3 "+redis.getS("foo"));try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("4 "+redis.getS("foo"));try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("5 "+redis.getS("foo"));} private JedisPool getConnectionSpec(){if(defaultConnectionSpec != null){return defaultConnectionSpec;}HashMap<String,JedisPool> tryServers = new HashMap<String,JedisPool>(serverPools);System.out.println("tryServers:"+tryServers.size());if(serverPools.size() == 1){System.out.println("22222");//jredisService=connectionSpec.getResource();return (JedisPool)serverPools.get("2");}while(!tryServers.isEmpty()){JedisPool connectionSpec = tryServers.get("1");if(isConnect(connectionSpec)){System.out.println("1111");jredisService=connectionSpec.getResource();return connectionSpec;}else{connectionSpec = tryServers.get("2");jredisService=connectionSpec.getResource();System.out.println("1111---2222");serverPools.remove("1");return connectionSpec;}}return null;} private boolean isConnect(JedisPool pool){if(pool == null){return false;}try{Jedis jedis = pool.getResource();jedis.ping();pool.returnResource(jedis);}catch(Exception e){return false;}return true;}}
appendonly 默认情况下,redis 会在后台异步的把数据库镜像备份到磁盘,但是该备份是非常耗时 的,而且备份也不能很频繁,如果发生诸如拉闸限电、拔插头等状况,那么将造成比较 大范围的数据丢失。所以 redis提供了另外一种更加高效的数据库备份及灾难恢复方式。 开启 append only 模式之后,redis 会把所接收到的每一次写操作请求都追加到 appendonly.aof 文件中,当redis重新启动时,会从该文件恢复出之前的状态。但是这样 会造成 appendonly.aof 文件过大,所以 redis 还支持了 BGREWRITEAOF 指令,对 appendonly.aof 进行重新整理。所以我认为推荐生产环境下的做法为关闭镜像,开启 appendonly.aof,同时可以选择在访问较少的时间每天对 appendonly.aof 进行重写一次
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号