├── redis-conf ├── redis-cluster │ ├── master6666 │ │ ├── startup.sh │ │ ├── client.sh │ │ ├── shutdown.sh │ │ ├── genaof.sh │ │ ├── import-aof.sh │ │ ├── redis-dump.rdb │ │ └── redis-aof.aof │ ├── slave7771 │ │ ├── startup.sh │ │ ├── client.sh │ │ ├── shutdown.sh │ │ ├── genaof.sh │ │ ├── import-aof.sh │ │ ├── slaveof-6666.sh │ │ ├── redis-dump.rdb │ │ └── redis-aof.aof │ └── slave7772 │ │ ├── startup.sh │ │ ├── client.sh │ │ ├── shutdown.sh │ │ ├── genaof.sh │ │ ├── import-aof.sh │ │ ├── slaveof-6666.sh │ │ ├── redis-dump.rdb │ │ ├── redis-aof.aof │ │ └── redislog.log └── redis-sentinel │ ├── redis-node-8000 │ ├── master8000 │ │ ├── startup.sh │ │ ├── client.sh │ │ ├── shutdown.sh │ │ ├── genaof.sh │ │ ├── import-aof.sh │ │ ├── redis-dump.rdb │ │ └── redis-aof.aof │ ├── slave8101 │ │ ├── startup.sh │ │ ├── client.sh │ │ ├── shutdown.sh │ │ ├── genaof.sh │ │ ├── import-aof.sh │ │ ├── slaveof-8000.sh │ │ ├── redis-dump.rdb │ │ └── redis-aof.aof │ └── slave8102 │ │ ├── startup.sh │ │ ├── client.sh │ │ ├── shutdown.sh │ │ ├── genaof.sh │ │ ├── import-aof.sh │ │ ├── slaveof-8000.sh │ │ ├── redis-dump.rdb │ │ └── redis-aof.aof │ └── sentinel │ ├── sentinel-9000 │ ├── redis-sentinel-start.sh │ └── sentinel.conf │ ├── sentinel-9001 │ ├── redis-sentinel-start.sh │ └── sentinel.conf │ ├── sentinel-9002 │ ├── redis-sentinel-start.sh │ └── sentinel.conf │ └── sentinel.conf ├── redis-cluster-monitor ├── src │ ├── sql │ │ ├── Product.sql │ │ ├── Cluster.sql │ │ ├── RedisNode.sql │ │ ├── MonitorKeyspace.sql │ │ ├── MonitorCpu.sql │ │ ├── MonitorClients.sql │ │ ├── MonitorMemory.sql │ │ ├── MonitorReplication.sql │ │ ├── MonitorServer.sql │ │ ├── MonitorStat.sql │ │ └── MonitorPersistence.sql │ ├── test │ │ ├── resources │ │ │ ├── mysql.properties │ │ │ └── log4j.properties │ │ └── java │ │ │ └── com │ │ │ └── linda │ │ │ └── cluster │ │ │ └── redis │ │ │ └── monitor │ │ │ ├── AppTest.java │ │ │ ├── RedisNodeMonitorTest.java │ │ │ └── RedisAdminServiceTest.java │ └── main │ │ ├── resources │ │ ├── dev │ │ │ ├── mysql.properties │ │ │ └── log4j.properties │ │ ├── test │ │ │ ├── mysql.properties │ │ │ └── log4j.properties │ │ ├── spring-context-config.xml │ │ ├── monitor-start.bat │ │ ├── sqlmap-config.xml │ │ └── monitor-start.sh │ │ └── java │ │ └── com │ │ └── linda │ │ └── cluster │ │ └── redis │ │ └── monitor │ │ ├── pojo │ │ ├── Product.java │ │ ├── Cluster.java │ │ ├── RedisNode.java │ │ ├── MonitorSlaveBean.java │ │ ├── MonitorClients.java │ │ ├── MonitorPartBase.java │ │ ├── MonitorCpu.java │ │ ├── MonitorReplication.java │ │ ├── MonitorMemory.java │ │ ├── MonitorStat.java │ │ ├── MonitorPersistence.java │ │ ├── MonitorServer.java │ │ └── MonitorKeyspace.java │ │ ├── dao │ │ ├── ProductDao.java │ │ ├── ClusterDao.java │ │ ├── MonitorServerDao.java │ │ ├── RedisNodeDao.java │ │ ├── sqlmap │ │ │ ├── ProductDaoSqlmap.xml │ │ │ ├── ClusterDaoSqlmap.xml │ │ │ ├── RedisNodeDaoSqlmap.xml │ │ │ ├── MonitorServerDaoSqlmap.xml │ │ │ ├── MonitorKeyspaceDaoSqlmap.xml │ │ │ ├── MonitorCpuDaoSqlmap.xml │ │ │ ├── MonitorClientsDaoSqlmap.xml │ │ │ ├── MonitorMemoryDaoSqlmap.xml │ │ │ ├── MonitorReplicationDaoSqlmap.xml │ │ │ ├── MonitorStatDaoSqlmap.xml │ │ │ └── MonitorPersistenceDaoSqlmap.xml │ │ ├── MonitorCpuDao.java │ │ ├── MonitorStatDao.java │ │ ├── MonitorMemoryDao.java │ │ ├── MonitorClientsDao.java │ │ ├── MonitorKeyspaceDao.java │ │ ├── MonitorReplicationDao.java │ │ └── MonitorPersistenceDao.java │ │ ├── spring │ │ ├── MonitorTransaction.java │ │ ├── MonitorTransactionAdvice.java │ │ └── MonitorTransactionManager.java │ │ ├── service │ │ ├── RedisMonitorStatService.java │ │ ├── RedisMonitorCpuService.java │ │ ├── RedisMonitorMemoryService.java │ │ ├── RedisMonitorClientsService.java │ │ ├── RedisMonitorPersistenceService.java │ │ ├── RedisMonitorReplicationService.java │ │ ├── RedisMonitorServerService.java │ │ └── RedisMonitorKeyspaceService.java │ │ ├── start │ │ ├── AbstractMonitorBootStrap.java │ │ ├── RedisProductMonitorBootStrap.java │ │ ├── RedisNodeMonitorBootStrap.java │ │ └── AbstractSpringBootStrap.java │ │ ├── RedisMonitorInfoBean.java │ │ └── RedisMonitorBean.java └── .gitignore ├── redis-cluster-aof ├── .gitignore ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── linda │ │ │ │ └── cluster │ │ │ │ └── redis │ │ │ │ └── aof │ │ │ │ ├── RedisCmd.java │ │ │ │ └── App.java │ │ └── resources │ │ │ └── redis-cmd.txt │ └── test │ │ └── java │ │ └── com │ │ └── linda │ │ └── cluster │ │ └── redis │ │ └── aof │ │ ├── AppTest.java │ │ ├── AofAnalyserTest.java │ │ └── AofObjectTest.java └── pom.xml ├── redis-cluster-scale ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── linda │ │ │ └── cluster │ │ │ └── redis │ │ │ └── scale │ │ │ ├── JedisCommandAnalyzer.java │ │ │ ├── App.java │ │ │ ├── SimpleJedisMonitor.java │ │ │ └── ScaleUtils.java │ └── test │ │ └── java │ │ └── com │ │ └── linda │ │ └── cluster │ │ └── redis │ │ └── scale │ │ ├── MonitorListener.java │ │ └── AppTest.java └── .gitignore ├── redis-cluster-client ├── .gitignore ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── linda │ │ │ │ └── cluster │ │ │ │ └── redis │ │ │ │ └── client │ │ │ │ ├── RedisCallback.java │ │ │ │ ├── cluster │ │ │ │ ├── KeySlotInfo.java │ │ │ │ ├── SimpleJedisTemplate.java │ │ │ │ └── ClusterJedisTemplate.java │ │ │ │ ├── serializer │ │ │ │ ├── RedisSerializer.java │ │ │ │ └── SimpleJdkSerializer.java │ │ │ │ ├── push │ │ │ │ ├── NotSupportRedisOperationException.java │ │ │ │ ├── JedisClusterException.java │ │ │ │ └── JedisClusterService.java │ │ │ │ ├── exception │ │ │ │ ├── ClusterExceptionHandler.java │ │ │ │ └── SimpleExceptionHandler.java │ │ │ │ ├── transaction │ │ │ │ ├── RedisTransaction.java │ │ │ │ └── JedisOperation.java │ │ │ │ ├── JedisClient.java │ │ │ │ ├── RedisResult.java │ │ │ │ └── utils │ │ │ │ └── RedisUtils.java │ │ └── resources │ │ │ ├── redis-cluster-default.conf │ │ │ └── log4j.properties │ └── test │ │ ├── java │ │ └── com │ │ │ └── linda │ │ │ └── cluster │ │ │ └── redis │ │ │ └── client │ │ │ ├── AppTest.java │ │ │ ├── SimpleSlotInfo.java │ │ │ ├── SimpleJedisTemplateTest.java │ │ │ ├── SimpleRedisMigrateTest.java │ │ │ ├── SimpleClientLuaTest.java │ │ │ └── ClusterJedisTemplateTest.java │ │ └── resources │ │ └── log4j.properties └── pom.xml ├── redis-cluster-common ├── .gitignore └── src │ ├── main │ └── java │ │ └── com │ │ └── linda │ │ └── cluster │ │ └── redis │ │ └── common │ │ ├── Service.java │ │ ├── utils │ │ ├── SlotUtils.java │ │ ├── ZkNodeCreateCallback.java │ │ ├── RedisNodeDeleteCallback.java │ │ ├── RedisGetChildrenCallback.java │ │ ├── RedisSetNodeDataCallback.java │ │ ├── RedisGetNodeDataCallback.java │ │ ├── KeyValueUtils.java │ │ ├── IOUtils.java │ │ ├── IntrospectorUtils.java │ │ └── JSONUtils.java │ │ ├── bean │ │ ├── CountBean.java │ │ ├── KeyValueBean.java │ │ ├── ShardingBean.java │ │ ├── RedisProductDataBean.java │ │ ├── RedisServiceConfig.java │ │ ├── RedisZookeeperServiceConfig.java │ │ ├── ClusterStateBean.java │ │ ├── RedisZkData.java │ │ └── HostAndPort.java │ │ ├── spring │ │ └── BeanInit.java │ │ ├── exception │ │ └── RedisZkException.java │ │ ├── sharding │ │ └── Sharding.java │ │ ├── constant │ │ └── RedisZkNodeConstant.java │ │ ├── App.java │ │ └── ClassPathGenerator.java │ └── test │ └── java │ └── com │ └── linda │ └── cluster │ └── redis │ └── common │ └── AppTest.java ├── redis-cluster-keepalived ├── .gitignore ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── linda │ │ │ │ └── cluster │ │ │ │ └── redis │ │ │ │ └── keepalived │ │ │ │ ├── redis │ │ │ │ ├── RedisDataNode.java │ │ │ │ ├── RedisAlivedListener.java │ │ │ │ ├── RedisKeepAlived.java │ │ │ │ ├── SimpleRedisAliveNode.java │ │ │ │ ├── MultiRedisAlivedPingService.java │ │ │ │ └── RedisAliveBase.java │ │ │ │ ├── conf │ │ │ │ ├── RedisProduct.java │ │ │ │ └── RedisZkConf.java │ │ │ │ ├── alived │ │ │ │ ├── AlivedWatcher.java │ │ │ │ └── RedisKeepAliveStartup.java │ │ │ │ └── zk │ │ │ │ ├── ClusterGetChildrenCallback.java │ │ │ │ ├── ClusterNodeDeleteCallback.java │ │ │ │ ├── RedisNodeSetDataCallback.java │ │ │ │ ├── MonitorNodeCreateCallback.java │ │ │ │ └── RedisNodeConfigDataCallback.java │ │ └── resources │ │ │ ├── test │ │ │ └── keepalived-conf.json │ │ │ ├── keepalived-conf.json │ │ │ ├── dev │ │ │ └── keepalived-conf.json │ │ │ └── log4j.properties │ └── test │ │ └── java │ │ └── com │ │ └── linda │ │ └── cluster │ │ └── redis │ │ └── keepalived │ │ ├── conf │ │ └── RedisZkConfTest.java │ │ ├── AppTest.java │ │ └── redis │ │ └── RedisKeepAlivedTest.java └── pom.xml └── redis-cluster-config ├── .gitignore ├── src ├── main │ ├── java │ │ └── com │ │ │ └── linda │ │ │ └── cluster │ │ │ └── redis │ │ │ └── config │ │ │ ├── App.java │ │ │ ├── product │ │ │ └── ZkProductService.java │ │ │ ├── utils │ │ │ └── RedisZkUtils.java │ │ │ └── config │ │ │ └── RedisZkConfig.java │ └── resources │ │ ├── redis-zookeeper-conf.json │ │ └── log4j.properties └── test │ └── java │ └── com │ └── linda │ └── cluster │ └── redis │ └── config │ └── AppTest.java └── pom.xml /redis-conf/redis-cluster/master6666/startup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-server redis.conf 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7771/startup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-server redis.conf 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7772/startup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-server redis.conf 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/master6666/client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 6666 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7771/client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 7771 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7772/client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 7772 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/master6666/shutdown.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 6666 shutdown 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7771/shutdown.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 7771 shutdown 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7772/shutdown.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 7772 shutdown 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/master8000/startup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-server redis.conf 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8101/startup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-server redis.conf 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8102/startup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-server redis.conf 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/master8000/client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 8000 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8101/client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 8101 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8102/client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 8102 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8101/shutdown.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 8101 shutdown 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8102/shutdown.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 8102 shutdown 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/master6666/genaof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "bgrewriteaof" | redis-cli -h 127.0.0.1 -p 6666 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/master6666/import-aof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat redis-aof.aof|redis-cli -h 127.0.0.1 -p 7772 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7771/genaof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "bgrewriteaof" | redis-cli -h 127.0.0.1 -p 7771 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7771/import-aof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat redis-aof.aof|redis-cli -h 127.0.0.1 -p 7771 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7772/genaof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "bgrewriteaof" | redis-cli -h 127.0.0.1 -p 7772 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7772/import-aof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat redis-aof.aof|redis-cli -h 127.0.0.1 -p 7772 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/master8000/shutdown.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-cli -h 127.0.0.1 -p 8000 shutdown 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7771/slaveof-6666.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "slaveof 127.0.0.1 6666" | redis-cli -h 127.0.0.1 -p 7771 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7772/slaveof-6666.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "slaveof 127.0.0.1 6666" | redis-cli -h 127.0.0.1 -p 7772 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/sentinel/sentinel-9000/redis-sentinel-start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-server sentinel.conf --sentinel & 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/sentinel/sentinel-9001/redis-sentinel-start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-server sentinel.conf --sentinel & 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/sentinel/sentinel-9002/redis-sentinel-start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | redis-server sentinel.conf --sentinel & 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/master8000/genaof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "bgrewriteaof" | redis-cli -h 127.0.0.1 -p 8000 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/master8000/import-aof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat redis-aof.aof|redis-cli -h 127.0.0.1 -p 8000 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8101/genaof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "bgrewriteaof" | redis-cli -h 127.0.0.1 -p 8101 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8101/import-aof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat redis-aof.aof|redis-cli -h 127.0.0.1 -p 8101 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8102/genaof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "bgrewriteaof" | redis-cli -h 127.0.0.1 -p 8101 --pipe 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8102/import-aof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat redis-aof.aof|redis-cli -h 127.0.0.1 -p 8102 --pipe 3 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/Product.sql: -------------------------------------------------------------------------------- 1 | create table redis_product( 2 | id bigint primary key auto_increment, 3 | name varchar(50) not null 4 | ); -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8101/slaveof-8000.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "slaveof 127.0.0.1 8000" | redis-cli -h 127.0.0.1 -p 8101 3 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8102/slaveof-8000.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "slaveof 127.0.0.1 8000" | redis-cli -h 127.0.0.1 -p 8102 3 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/master6666/redis-dump.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lindzh/redis-cluster/HEAD/redis-conf/redis-cluster/master6666/redis-dump.rdb -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7771/redis-dump.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lindzh/redis-cluster/HEAD/redis-conf/redis-cluster/slave7771/redis-dump.rdb -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7772/redis-dump.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lindzh/redis-cluster/HEAD/redis-conf/redis-cluster/slave7772/redis-dump.rdb -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8101/redis-dump.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lindzh/redis-cluster/HEAD/redis-conf/redis-sentinel/redis-node-8000/slave8101/redis-dump.rdb -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8102/redis-dump.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lindzh/redis-cluster/HEAD/redis-conf/redis-sentinel/redis-node-8000/slave8102/redis-dump.rdb -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/master8000/redis-dump.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lindzh/redis-cluster/HEAD/redis-conf/redis-sentinel/redis-node-8000/master8000/redis-dump.rdb -------------------------------------------------------------------------------- /redis-cluster-aof/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /src/main/webapp/WEB-INF/classes 4 | /src/main/webapp/WEB-INF/lib 5 | /.settings 6 | .project 7 | .classpath 8 | /.idea 9 | *.iml 10 | *.eml -------------------------------------------------------------------------------- /redis-cluster-scale/src/main/java/com/linda/cluster/redis/scale/JedisCommandAnalyzer.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.scale; 2 | 3 | public interface JedisCommandAnalyzer { 4 | 5 | 6 | } 7 | -------------------------------------------------------------------------------- /redis-cluster-client/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /src/main/webapp/WEB-INF/classes 4 | /src/main/webapp/WEB-INF/lib 5 | /.settings 6 | .project 7 | .classpath 8 | /.idea 9 | *.iml 10 | *.eml -------------------------------------------------------------------------------- /redis-cluster-common/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /src/main/webapp/WEB-INF/classes 4 | /src/main/webapp/WEB-INF/lib 5 | /.settings 6 | .project 7 | .classpath 8 | /.idea 9 | *.iml 10 | *.eml -------------------------------------------------------------------------------- /redis-cluster-scale/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /src/main/webapp/WEB-INF/classes 4 | /src/main/webapp/WEB-INF/lib 5 | /.settings 6 | .project 7 | .classpath 8 | /.idea 9 | *.iml 10 | *.eml -------------------------------------------------------------------------------- /redis-cluster-aof/src/main/java/com/linda/cluster/redis/aof/RedisCmd.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.aof; 2 | 3 | public class RedisCmd { 4 | 5 | private String cmd; 6 | 7 | 8 | 9 | } 10 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/test/resources/mysql.properties: -------------------------------------------------------------------------------- 1 | mysql.driver=com.mysql.jdbc.Driver 2 | mysql.url=jdbc:mysql://127.0.0.1:3306/redis_cluster 3 | mysql.username=redis-cluster 4 | mysql.password=redis-cluster 5 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/resources/dev/mysql.properties: -------------------------------------------------------------------------------- 1 | mysql.driver=com.mysql.jdbc.Driver 2 | mysql.url=jdbc:mysql://127.0.0.1:3306/redis_cluster 3 | mysql.username=redis-cluster 4 | mysql.password=redis-cluster 5 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/resources/test/mysql.properties: -------------------------------------------------------------------------------- 1 | mysql.driver=com.mysql.jdbc.Driver 2 | mysql.url=jdbc:mysql://172.31.136.150:3306/redis_cluster 3 | mysql.username=redis-cluster 4 | mysql.password=redis-cluster 5 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/Cluster.sql: -------------------------------------------------------------------------------- 1 | create table redis_cluster( 2 | id bigint primary key auto_increment, 3 | productId bigint default 0 comment '产品ID', 4 | clusterName varchar(100) not null, 5 | index pdtIdx(productId) 6 | ); -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/Service.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common; 2 | 3 | public interface Service { 4 | 5 | public void startup(); 6 | 7 | public void shutdown(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7772/redis-aof.aof: -------------------------------------------------------------------------------- 1 | *2 2 | $6 3 | SELECT 4 | $1 5 | 0 6 | *3 7 | $3 8 | SET 9 | $7 10 | test123 11 | $6 12 | tttttg 13 | *3 14 | $3 15 | SET 16 | $8 17 | hzlindzh 18 | $19 19 | my name is lindezhi 20 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/master6666/redis-aof.aof: -------------------------------------------------------------------------------- 1 | *2 2 | $6 3 | SELECT 4 | $1 5 | 0 6 | *3 7 | $3 8 | SET 9 | $7 10 | test123 11 | $6 12 | tttttg 13 | *3 14 | $3 15 | SET 16 | $8 17 | hzlindzh 18 | $19 19 | my name is lindezhi 20 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/master8000/redis-aof.aof: -------------------------------------------------------------------------------- 1 | *2 2 | $6 3 | SELECT 4 | $1 5 | 0 6 | *3 7 | $3 8 | SET 9 | $7 10 | test123 11 | $6 12 | tttttg 13 | *3 14 | $3 15 | SET 16 | $8 17 | hzlindzh 18 | $19 19 | my name is lindezhi 20 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8102/redis-aof.aof: -------------------------------------------------------------------------------- 1 | *2 2 | $6 3 | SELECT 4 | $1 5 | 0 6 | *3 7 | $3 8 | SET 9 | $7 10 | test123 11 | $6 12 | tttttg 13 | *3 14 | $3 15 | SET 16 | $8 17 | hzlindzh 18 | $19 19 | my name is lindezhi 20 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/SlotUtils.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | public class SlotUtils { 4 | 5 | public static int slot(long hash){ 6 | long result = hash%256; 7 | return (int)result; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/RedisCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | 6 | public interface RedisCallback { 7 | 8 | public void callback(Jedis jedis,RedisResult collector); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/cluster/KeySlotInfo.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.cluster; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | public interface KeySlotInfo { 6 | 7 | public void info(String key,int slot,Jedis jedis); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/bean/CountBean.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.bean; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class CountBean { 7 | 8 | private int count; 9 | 10 | public void incr(){ 11 | count++; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /redis-cluster-monitor/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /src/main/webapp/WEB-INF/classes 4 | /src/main/webapp/WEB-INF/lib 5 | /src/main/webapp/monitor 6 | /src/main/webapp/monitor.zip 7 | /.settings 8 | .project 9 | .classpath 10 | /.idea 11 | *.iml 12 | *.eml 13 | *.log 14 | *.jar 15 | *.zip 16 | *.class 17 | -------------------------------------------------------------------------------- /redis-cluster-aof/src/main/java/com/linda/cluster/redis/aof/App.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.aof; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/serializer/RedisSerializer.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.serializer; 2 | 3 | public interface RedisSerializer { 4 | 5 | public byte[] serializeKey(String key); 6 | 7 | public byte[] serializeValue(Object value); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /src/main/webapp/WEB-INF/classes 4 | /src/main/webapp/WEB-INF/lib 5 | /src/main/webapp/keepalived 6 | /src/main/webapp/keepalived.zip 7 | /.settings 8 | .project 9 | .classpath 10 | /.idea 11 | *.iml 12 | *.eml 13 | *.log 14 | *.jar 15 | *.zip 16 | *.class 17 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/push/NotSupportRedisOperationException.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.push; 2 | 3 | public class NotSupportRedisOperationException extends RuntimeException{ 4 | 5 | private static final long serialVersionUID = -7017506896565645121L; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /redis-cluster-config/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /src/main/webapp/WEB-INF/classes 4 | /src/main/webapp/WEB-INF/lib 5 | /src/main/webapp/cluster-config 6 | /src/main/webapp/cluster-config.zip 7 | /.settings 8 | .project 9 | .classpath 10 | /.idea 11 | *.iml 12 | *.eml 13 | *.log 14 | *.jar 15 | *.zip 16 | *.class 17 | -------------------------------------------------------------------------------- /redis-cluster-config/src/main/java/com/linda/cluster/redis/config/App.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.config; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/redis/RedisDataNode.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.redis; 2 | 3 | public class RedisDataNode { 4 | 5 | public enum RedisState{ 6 | INIT, 7 | ERROR, 8 | CONNECTING, 9 | CONNECTED, 10 | CLOSE 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /redis-cluster-scale/src/main/java/com/linda/cluster/redis/scale/App.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.scale; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/spring/BeanInit.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.spring; 2 | 3 | /** 4 | * bean life cycle 5 | * @author hzlindzh 6 | * 7 | */ 8 | public interface BeanInit { 9 | 10 | public void init(); 11 | 12 | public void destroy(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/exception/ClusterExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.exception; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | public interface ClusterExceptionHandler { 6 | 7 | public void handleException(Jedis jedis,Exception e); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7771/redis-aof.aof: -------------------------------------------------------------------------------- 1 | *2 2 | $6 3 | SELECT 4 | $1 5 | 0 6 | *3 7 | $3 8 | SET 9 | $6 10 | myname 11 | $8 12 | lindezhi 13 | *3 14 | $3 15 | SET 16 | $8 17 | hzlindzh 18 | $13 19 | 5345634563464 20 | *3 21 | $3 22 | SET 23 | $7 24 | testkey 25 | $18 26 | this is a test key 27 | -------------------------------------------------------------------------------- /redis-cluster-scale/src/main/java/com/linda/cluster/redis/scale/SimpleJedisMonitor.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.scale; 2 | 3 | import redis.clients.jedis.JedisMonitor; 4 | 5 | public class SimpleJedisMonitor extends JedisMonitor{ 6 | 7 | @Override 8 | public void onCommand(String command) { 9 | 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/bean/KeyValueBean.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.bean; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class KeyValueBean { 9 | private String key; 10 | private String value; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/transaction/RedisTransaction.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.transaction; 2 | 3 | public interface RedisTransaction { 4 | 5 | public void startTransaction(); 6 | 7 | public void commitTransaction(); 8 | 9 | public void rollbackTransaction(); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/Product.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import java.util.List; 4 | 5 | import lombok.Data; 6 | 7 | @Data 8 | public class Product { 9 | private long id; 10 | private String name; 11 | 12 | private List clusters; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/redis-node-8000/slave8101/redis-aof.aof: -------------------------------------------------------------------------------- 1 | *2 2 | $6 3 | SELECT 4 | $1 5 | 0 6 | *3 7 | $3 8 | SET 9 | $6 10 | myname 11 | $8 12 | lindezhi 13 | *3 14 | $3 15 | SET 16 | $8 17 | hzlindzh 18 | $13 19 | 5345634563464 20 | *3 21 | $3 22 | SET 23 | $7 24 | testkey 25 | $18 26 | this is a test key 27 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/bean/ShardingBean.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.bean; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class ShardingBean { 9 | 10 | private String bucketName; 11 | private String nodeName; 12 | } 13 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/JedisClient.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client; 2 | 3 | import redis.clients.jedis.BinaryJedisCommands; 4 | import redis.clients.jedis.JedisCommands; 5 | 6 | public interface JedisClient extends BinaryJedisCommands,JedisCommands{ 7 | 8 | public void close(); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/RedisResult.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client; 2 | 3 | 4 | public class RedisResult { 5 | 6 | private Object value; 7 | 8 | public Object getValue() { 9 | return value; 10 | } 11 | 12 | public void setValue(Object value) { 13 | this.value = value; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/transaction/JedisOperation.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.transaction; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | import lombok.Data; 6 | 7 | @Data 8 | public class JedisOperation { 9 | 10 | private String key; 11 | private Method method; 12 | private Object[] args; 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/Cluster.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import java.util.List; 4 | 5 | import lombok.Data; 6 | 7 | @Data 8 | public class Cluster { 9 | private long id; 10 | private long productId; 11 | private String clusterName; 12 | 13 | private List nodes; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/RedisNode.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class RedisNode { 7 | private long id; 8 | private long productId; 9 | private long clusterId; 10 | private String name; 11 | private String host; 12 | private int port; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/push/JedisClusterException.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.push; 2 | 3 | public class JedisClusterException extends RuntimeException{ 4 | 5 | private static final long serialVersionUID = 232516377380788562L; 6 | 7 | public JedisClusterException(String message){ 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorSlaveBean.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class MonitorSlaveBean { 7 | 8 | private String ip; 9 | 10 | private int port; 11 | 12 | private String state; 13 | 14 | private long offset; 15 | 16 | private int lag; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/RedisNode.sql: -------------------------------------------------------------------------------- 1 | create table redis_node( 2 | id bigint primary key auto_increment, 3 | productId bigint default 0 comment '产品ID', 4 | clusterId bigint default 0 comment '集群ID', 5 | name varchar(100) not null comment '节点名称', 6 | host varchar(50) not null comment '主机', 7 | port int default 0 comment '端口', 8 | index pdt(productId), 9 | unique key hostPortIdx(host,port) 10 | ); -------------------------------------------------------------------------------- /redis-cluster-config/src/main/resources/redis-zookeeper-conf.json: -------------------------------------------------------------------------------- 1 | { 2 | "zkhosts":[ 3 | { 4 | "name":"zk-10001", 5 | "host":"10.120.151.105", 6 | "port":10001 7 | }, 8 | { 9 | "name":"zk-10002", 10 | "host":"10.120.151.105", 11 | "port":10002 12 | }, 13 | { 14 | "name":"zk-10003", 15 | "host":"10.120.151.105", 16 | "port":10003 17 | } 18 | ], 19 | "redisBasePath":"/redis-cluster" 20 | } -------------------------------------------------------------------------------- /redis-cluster-client/src/main/resources/redis-cluster-default.conf: -------------------------------------------------------------------------------- 1 | #zookeeper config 2 | servers=zktest1.linda.org:4421,zktest2.linda.org:4421,zktest3.linda.org:4421 3 | path=/redis_failover 4 | chroot=/push-redis/dev 5 | username=push-redis-dev 6 | password=432645645rvcsd 7 | zooTimeout=8000 8 | redisTimeout=8000 9 | retrytime=7 10 | 11 | #redis common config 12 | maxTotal=10 13 | maxIdle=5 14 | maxWaitMillis=10000 15 | testOnBorrow=true -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/ProductDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.linda.cluster.redis.monitor.pojo.Product; 6 | 7 | public interface ProductDao { 8 | 9 | public int add(Product pdt); 10 | 11 | public List getAll(); 12 | 13 | public Product getById(long id); 14 | 15 | public int deleteById(long id); 16 | } 17 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/sentinel/sentinel.conf: -------------------------------------------------------------------------------- 1 | # sentinel monitor master host port quromNum 2 | sentinel monitor node8888 127.0.0.1 8888 1 3 | # fail down after mills master msec 4 | sentinel down-after-milliseconds node8888 5000 5 | # master fail over timeout 6 | sentinel failover-timeout node8888 900000 7 | # master can failover 8 | sentinel can-failover node8888 yes 9 | # parallet sync 10 | sentinel parallel-syncs node8888 2 11 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/bean/RedisProductDataBean.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.bean; 2 | 3 | import java.util.List; 4 | 5 | import lombok.Data; 6 | 7 | import com.linda.cluster.redis.common.sharding.Sharding; 8 | 9 | @Data 10 | public class RedisProductDataBean { 11 | //产品id,监控中映射 12 | private long id; 13 | //sharding映射表 14 | private List sharding; 15 | 16 | private String backup; 17 | } 18 | -------------------------------------------------------------------------------- /redis-cluster-config/src/main/java/com/linda/cluster/redis/config/product/ZkProductService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.config.product; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.zookeeper.ZooKeeper; 6 | 7 | import com.linda.cluster.redis.common.sharding.Sharding; 8 | 9 | 10 | public class ZkProductService { 11 | 12 | private ZooKeeper zookeeper; 13 | private List shardings; 14 | private String productname; 15 | 16 | } 17 | -------------------------------------------------------------------------------- /redis-cluster-config/src/main/java/com/linda/cluster/redis/config/utils/RedisZkUtils.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.config.utils; 2 | 3 | import com.linda.cluster.redis.common.utils.JSONUtils; 4 | import com.linda.cluster.redis.config.config.RedisZkConfig; 5 | 6 | public class RedisZkUtils { 7 | 8 | public static RedisZkConfig toRedisZkConfig(String configString){ 9 | return JSONUtils.fromJson(configString, RedisZkConfig.class); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/spring/MonitorTransaction.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.spring; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.METHOD) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface MonitorTransaction { 11 | 12 | 13 | } 14 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/exception/RedisZkException.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.exception; 2 | 3 | public class RedisZkException extends RuntimeException{ 4 | 5 | private static final long serialVersionUID = 5572744379554261506L; 6 | 7 | public RedisZkException(String message){ 8 | super(message); 9 | } 10 | 11 | public RedisZkException(Throwable throwable){ 12 | super(throwable); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/ClusterDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.linda.cluster.redis.monitor.pojo.Cluster; 6 | 7 | public interface ClusterDao { 8 | 9 | public int add(Cluster cluster); 10 | 11 | public Cluster getById(long clusterId); 12 | 13 | public List getByProductId(long productId); 14 | 15 | public int deleteById(long id); 16 | } 17 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorClients.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class MonitorClients extends MonitorPartBase{ 7 | 8 | private long id; 9 | //连接客户端数量 10 | private int connected_clients; 11 | 12 | private int client_longest_output_list; 13 | 14 | private int client_biggest_input_buf; 15 | //阻塞客户端数量 16 | private int blocked_clients; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/sharding/Sharding.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.sharding; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | * product 节点数据部分存储sharding映射表 9 | * @author linda 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class Sharding { 15 | private int from; 16 | private int to; 17 | private String node; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/test/java/com/linda/cluster/redis/keepalived/conf/RedisZkConfTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.conf; 2 | 3 | import com.linda.cluster.redis.common.utils.JSONUtils; 4 | 5 | public class RedisZkConfTest { 6 | 7 | public static void main(String[] args) { 8 | String filename = "classpath:keepalived-conf.json"; 9 | RedisZkConf zkConf = RedisZkConf.loadRedisZkConf(filename); 10 | System.out.println(JSONUtils.toJson(zkConf)); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorPartBase.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public abstract class MonitorPartBase { 7 | //产品ID 8 | private long productId; 9 | //集群ID 10 | private long clusterId; 11 | //节点ID 12 | private long redisNodeId; 13 | 14 | private long addtime; 15 | 16 | public MonitorPartBase(){ 17 | this.addtime = System.currentTimeMillis(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/redis/RedisAlivedListener.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.redis; 2 | 3 | public interface RedisAlivedListener { 4 | 5 | public void onConnected(RedisAliveBase redis); 6 | 7 | public void onClose(RedisAliveBase redis); 8 | 9 | public void onException(RedisAliveBase redis,Exception e); 10 | 11 | public void onInfo(RedisAliveBase redis,String info); 12 | 13 | public void onPing(RedisAliveBase redis); 14 | } 15 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/ZkNodeCreateCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | import com.linda.cluster.redis.common.bean.CountBean; 4 | 5 | public interface ZkNodeCreateCallback { 6 | 7 | public boolean onNodeExist(String path,byte[] data,CountBean count); 8 | 9 | public boolean onConnectionLoss(String path,byte[] data,CountBean count); 10 | 11 | public boolean onZkException(String path,byte[] data,CountBean count); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /redis-cluster-scale/src/main/java/com/linda/cluster/redis/scale/ScaleUtils.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.scale; 2 | 3 | public class ScaleUtils { 4 | 5 | public static String byte2hex(byte[] buffer) { 6 | StringBuilder sb = new StringBuilder(); 7 | for (int i = 0; i < buffer.length; i++) { 8 | String temp = Integer.toHexString(buffer[i] & 0xFF); 9 | if (temp.length() == 1) { 10 | temp = "0" + temp; 11 | } 12 | sb.append(temp); 13 | } 14 | return sb.toString(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/RedisNodeDeleteCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | import com.linda.cluster.redis.common.bean.CountBean; 4 | 5 | public interface RedisNodeDeleteCallback { 6 | 7 | public boolean onNodeNotExist(String path,int version,CountBean count); 8 | 9 | public boolean onConnectionLoss(String path,int version,CountBean count); 10 | 11 | public boolean onZkException(String path,int version,CountBean count); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/constant/RedisZkNodeConstant.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.constant; 2 | 3 | public interface RedisZkNodeConstant { 4 | 5 | public static final String REDIS_PRODUCT_CUSTER_MONITOR_NODE = "monitors"; 6 | 7 | public static final String REDIS_PORDUCT_CLUSTER_NODE_VOTE_NODE = "votes"; 8 | 9 | public static final String REDIS_CLUSTER_MONITORS_MONITOR_NODE = "monitor-"; 10 | 11 | public static final String REDIS_NODE_NULL_DATA = " "; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/RedisGetChildrenCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.zookeeper.Watcher; 6 | 7 | import com.linda.cluster.redis.common.bean.CountBean; 8 | 9 | public interface RedisGetChildrenCallback { 10 | 11 | public List onConnectionLoss(String path,Watcher watcher,CountBean count); 12 | 13 | public List onZkException(String path,Watcher watcher,CountBean count); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/MonitorServerDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.linda.cluster.redis.monitor.pojo.MonitorServer; 6 | 7 | public interface MonitorServerDao { 8 | 9 | public int add(MonitorServer cpu); 10 | 11 | public List getByProduct(long productId); 12 | 13 | public List getByCluster(long clusterId); 14 | 15 | public MonitorServer getByNode(long nodeId); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /redis-cluster-client/src/test/java/com/linda/cluster/redis/client/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client; 2 | 3 | import redis.clients.jedis.JedisPool; 4 | import redis.clients.jedis.JedisSentinelPool; 5 | import junit.framework.TestCase; 6 | 7 | public class AppTest extends TestCase { 8 | 9 | public static void main(String[] args) { 10 | JedisSentinelPool pool = new JedisSentinelPool("", null); 11 | pool.getResource(); 12 | JedisPool jedisPool = new JedisPool("", 123); 13 | jedisPool.getResource(); 14 | 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorCpu.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class MonitorCpu extends MonitorPartBase { 7 | 8 | private long id; 9 | //redis server sys的cpu使用率 10 | private float used_cpu_sys;//单位???? 11 | //redis server user 的cpu使用率 12 | private float used_cpu_user; 13 | //redis server sys后台进程的cpu使用率 14 | private float used_cpu_sys_children; 15 | //redis server user后台进程的cpu使用率 16 | private float used_cpu_user_children; 17 | } 18 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/App.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | @SuppressWarnings("unused") 13 | Service service = new Service(){ 14 | 15 | @Override 16 | public void startup() { 17 | 18 | } 19 | 20 | @Override 21 | public void shutdown() { 22 | 23 | } 24 | 25 | }; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/RedisSetNodeDataCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | import org.apache.zookeeper.data.Stat; 4 | 5 | import com.linda.cluster.redis.common.bean.CountBean; 6 | 7 | public interface RedisSetNodeDataCallback { 8 | 9 | public Stat onNodeNotExist(String path,byte[] data,int version,CountBean count); 10 | 11 | public Stat onConnectionLoss(String path,byte[] data,int version,CountBean count); 12 | 13 | public Stat onZkException(String path,byte[] data,int version,CountBean count); 14 | } 15 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/conf/RedisProduct.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.conf; 2 | 3 | import java.util.List; 4 | 5 | import lombok.Data; 6 | 7 | /** 8 | * 监控集群列表 9 | * 在监控集群节点下创建子节点monitors:然后哥监控创建e-s节点,监控各个子节点,选举子节点宕机状态,并切换master 10 | * @author linda 11 | * 12 | */ 13 | @Data 14 | public class RedisProduct { 15 | private String productName; 16 | private String zkPassword; 17 | private int pingInterval;//发送ping 命令时间 18 | private int infoInterval;//发送info命令时间 19 | private List clusters; 20 | //默认选举节点宕机数量为监控节点数量的一般 21 | 22 | } 23 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/resources/test/keepalived-conf.json: -------------------------------------------------------------------------------- 1 | { 2 | "zkhosts":[ 3 | { 4 | "name":"zk-10001", 5 | "host":"10.120.151.105", 6 | "port":10001 7 | }, 8 | { 9 | "name":"zk-10002", 10 | "host":"10.120.151.105", 11 | "port":10002 12 | }, 13 | { 14 | "name":"zk-10003", 15 | "host":"10.120.151.105", 16 | "port":10003 17 | } 18 | ], 19 | "zkBasePath":"/redis-cluster", 20 | "products":[ 21 | { 22 | "zkPassword":"123456", 23 | "productName":"yixin12", 24 | "pingInterval":5000, 25 | "infoInterval":5000, 26 | "clusters":[ 27 | "yixin12-cluster1" 28 | ] 29 | } 30 | ] 31 | } -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/RedisNodeDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import com.linda.cluster.redis.monitor.pojo.RedisNode; 8 | 9 | public interface RedisNodeDao { 10 | 11 | public int add(RedisNode node); 12 | 13 | public RedisNode getById(long id); 14 | 15 | public List getByClusterId(@Param("productId")long productId,@Param("clusterId")long clusterId); 16 | 17 | public List getByProductId(long productId); 18 | 19 | public int deleteById(long id); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/service/RedisMonitorStatService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.linda.cluster.redis.monitor.dao.MonitorStatDao; 7 | import com.linda.cluster.redis.monitor.pojo.MonitorStat; 8 | 9 | @Service 10 | public class RedisMonitorStatService { 11 | @Autowired 12 | private MonitorStatDao monitorStatDao; 13 | 14 | public MonitorStat add(MonitorStat stat){ 15 | int add = monitorStatDao.add(stat); 16 | return add>0?stat:null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/resources/keepalived-conf.json: -------------------------------------------------------------------------------- 1 | { 2 | "zkhosts":[ 3 | { 4 | "name":"zk-10001", 5 | "host":"10.120.151.105", 6 | "port":10001 7 | }, 8 | { 9 | "name":"zk-10002", 10 | "host":"10.120.151.105", 11 | "port":10002 12 | }, 13 | { 14 | "name":"zk-10003", 15 | "host":"10.120.151.105", 16 | "port":10003 17 | } 18 | ], 19 | "zkBasePath":"/redis-cluster", 20 | "products":[ 21 | { 22 | "zkPassword":"123456", 23 | "productName":"yixin12", 24 | "pingInterval":5000, 25 | "infoInterval":5000, 26 | "clusters":[ 27 | "yixin12-cluster1", 28 | "yixin12-cluster2" 29 | ] 30 | } 31 | ] 32 | } -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/service/RedisMonitorCpuService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.linda.cluster.redis.monitor.dao.MonitorCpuDao; 7 | import com.linda.cluster.redis.monitor.pojo.MonitorCpu; 8 | 9 | @Service 10 | public class RedisMonitorCpuService { 11 | 12 | @Autowired 13 | private MonitorCpuDao monitorCpuDao; 14 | 15 | public MonitorCpu add(MonitorCpu cpu){ 16 | int add = monitorCpuDao.add(cpu); 17 | return add>0?cpu:null; 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/RedisGetNodeDataCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | import org.apache.zookeeper.Watcher; 4 | 5 | import com.linda.cluster.redis.common.bean.CountBean; 6 | import com.linda.cluster.redis.common.bean.RedisZkData; 7 | 8 | public interface RedisGetNodeDataCallback { 9 | 10 | public RedisZkData onNodeNotExist(String path,byte[] data,Watcher watcher,CountBean count); 11 | 12 | public RedisZkData onConnectionLoss(String path,byte[] data,Watcher watcher,CountBean count); 13 | 14 | public RedisZkData onZkException(String path,byte[] data,Watcher watcher,CountBean count); 15 | } 16 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/MonitorKeyspace.sql: -------------------------------------------------------------------------------- 1 | create table redis_monitor_keyspace( 2 | id bigint primary key auto_increment, 3 | productId bigint not null, 4 | clusterId bigint not null, 5 | redisNodeId bigint not null, 6 | addtime bigint not null, 7 | 8 | databaseId int default 0, 9 | keynum int default 0, 10 | expires int default 0, 11 | avg_ttl int default 0, 12 | 13 | index pdt_cluster_node_time_idx(productId,clusterId,redisNodeId,addtime) 14 | ); 15 | 16 | create index pdtIdx on redis_monitor_keyspace(productId,addtime); 17 | create index clusterIdx on redis_monitor_keyspace(clusterId,addtime); 18 | create index nodeIdx on redis_monitor_keyspace(redisNodeId,addtime); -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/resources/dev/keepalived-conf.json: -------------------------------------------------------------------------------- 1 | { 2 | "zkhosts":[ 3 | { 4 | "name":"zk-10001", 5 | "host":"10.120.151.105", 6 | "port":10001 7 | }, 8 | { 9 | "name":"zk-10002", 10 | "host":"10.120.151.105", 11 | "port":10002 12 | }, 13 | { 14 | "name":"zk-10003", 15 | "host":"10.120.151.105", 16 | "port":10003 17 | } 18 | ], 19 | "zkBasePath":"/redis-cluster", 20 | "products":[ 21 | { 22 | "zkPassword":"123456", 23 | "productName":"lindzh", 24 | "pingInterval":5000, 25 | "infoInterval":5000, 26 | "clusters":[ 27 | "lindzh-cluster1", 28 | "lindzh-cluster2", 29 | "lindzh-backup" 30 | ] 31 | } 32 | ] 33 | } -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/exception/SimpleExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.exception; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | import redis.clients.jedis.Client; 6 | import redis.clients.jedis.Jedis; 7 | 8 | public class SimpleExceptionHandler implements ClusterExceptionHandler{ 9 | 10 | private Logger logger = Logger.getLogger(SimpleExceptionHandler.class); 11 | 12 | @Override 13 | public void handleException(Jedis jedis, Exception e) { 14 | Client client = jedis.getClient(); 15 | logger.error("node exception:"+client.getHost()+":"+client.getPort()+" "+e.getMessage()); 16 | e.printStackTrace(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/service/RedisMonitorMemoryService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.linda.cluster.redis.monitor.dao.MonitorMemoryDao; 7 | import com.linda.cluster.redis.monitor.pojo.MonitorMemory; 8 | 9 | @Service 10 | public class RedisMonitorMemoryService { 11 | 12 | @Autowired 13 | private MonitorMemoryDao monitorMemoryDao; 14 | 15 | public MonitorMemory add(MonitorMemory memory){ 16 | int add = monitorMemoryDao.add(memory); 17 | return add>0?memory:null; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/MonitorCpu.sql: -------------------------------------------------------------------------------- 1 | create table redis_monitor_cpu( 2 | id bigint primary key auto_increment, 3 | productId bigint not null, 4 | clusterId bigint not null, 5 | redisNodeId bigint not null, 6 | addtime bigint not null, 7 | 8 | used_cpu_sys float default 0.0, 9 | used_cpu_user float default 0.0, 10 | used_cpu_sys_children float default 0.0, 11 | used_cpu_user_children float default 0.0, 12 | index pdt_cluster_node_time_idx(productId,clusterId,redisNodeId,addtime) 13 | ); 14 | 15 | create index pdtIdx on redis_monitor_cpu(productId,addtime); 16 | create index clusterIdx on redis_monitor_cpu(clusterId,addtime); 17 | create index nodeIdx on redis_monitor_cpu(redisNodeId,addtime); -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/MonitorClients.sql: -------------------------------------------------------------------------------- 1 | create table redis_monitor_clients( 2 | id bigint primary key auto_increment, 3 | productId bigint not null, 4 | clusterId bigint not null, 5 | redisNodeId bigint not null, 6 | addtime bigint not null, 7 | 8 | connected_clients int default 0, 9 | client_longest_output_list int default 0, 10 | client_biggest_input_buf int default 0, 11 | blocked_clients int default 0, 12 | 13 | index pdt_cluster_node_time_idx(productId,clusterId,redisNodeId,addtime) 14 | ); 15 | 16 | create index pdtIdx on redis_monitor_clients(productId,addtime); 17 | create index clusterIdx on redis_monitor_clients(clusterId,addtime); 18 | create index nodeIdx on redis_monitor_clients(redisNodeId,addtime); -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/service/RedisMonitorClientsService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.linda.cluster.redis.monitor.dao.MonitorClientsDao; 7 | import com.linda.cluster.redis.monitor.pojo.MonitorClients; 8 | 9 | @Service 10 | public class RedisMonitorClientsService { 11 | 12 | @Autowired 13 | private MonitorClientsDao monitorClientsDao; 14 | 15 | public MonitorClients add(MonitorClients clients){ 16 | int add = monitorClientsDao.add(clients); 17 | if(add>0){ 18 | return clients; 19 | } 20 | return null; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/service/RedisMonitorPersistenceService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.linda.cluster.redis.monitor.dao.MonitorPersistenceDao; 7 | import com.linda.cluster.redis.monitor.pojo.MonitorPersistence; 8 | 9 | @Service 10 | public class RedisMonitorPersistenceService { 11 | @Autowired 12 | private MonitorPersistenceDao monitorPersistenceDao; 13 | 14 | public MonitorPersistence add(MonitorPersistence persistence){ 15 | int add = monitorPersistenceDao.add(persistence); 16 | return add>0?persistence:null; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /redis-cluster-config/src/main/java/com/linda/cluster/redis/config/config/RedisZkConfig.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.config.config; 2 | 3 | import java.util.List; 4 | 5 | import lombok.Data; 6 | 7 | import com.linda.cluster.redis.common.bean.HostAndPort; 8 | 9 | @Data 10 | public class RedisZkConfig { 11 | private List zkhosts; 12 | private String redisBasePath; 13 | 14 | public String toZkServer(){ 15 | StringBuilder sb = new StringBuilder(); 16 | int index = 0; 17 | for(HostAndPort zkh:zkhosts){ 18 | sb.append(zkh.getHost()); 19 | sb.append(":"); 20 | sb.append(zkh.getPort()); 21 | if(index0?replication:null; 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /redis-cluster-scale/src/test/java/com/linda/cluster/redis/scale/MonitorListener.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.scale; 2 | 3 | import redis.clients.jedis.Jedis; 4 | import redis.clients.jedis.JedisMonitor; 5 | import redis.clients.jedis.JedisPool; 6 | 7 | public class MonitorListener{ 8 | 9 | public static void main(String[] args) { 10 | String host = "192.168.139.129"; 11 | int port = 7770; 12 | System.out.println("server:"+host+":"+port); 13 | JedisPool pool = new JedisPool(host,port); 14 | Jedis jedis = pool.getResource(); 15 | jedis.monitor(new JedisMonitor(){ 16 | @Override 17 | public void onCommand(String command) { 18 | System.out.println(command); 19 | } 20 | }); 21 | jedis.close(); 22 | pool.destroy(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /redis-cluster-client/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,stdout,store 2 | 3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 | log4j.appender.stdout.Target=System.out 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern=%d %-4r [%t] (%F:%L) %-5p - %m%n 7 | 8 | log4j.appender.store=org.apache.log4j.DailyRollingFileAppender 9 | log4j.appender.store.File=D:\\work\\workspace\\redis-cluster\\redis-cluster-client\\client.log 10 | log4j.appender.store.Append=true 11 | log4j.appender.store.DatePattern ='_'yyyy-MM-dd'.log' 12 | log4j.appender.store.layout=org.apache.log4j.PatternLayout 13 | log4j.appender.store.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4r [%t] (%F:%L) %-5p - %m%n 14 | 15 | -------------------------------------------------------------------------------- /redis-cluster-config/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,stdout,store 2 | 3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 | log4j.appender.stdout.Target=System.out 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern=%d %-4r [%t] (%F:%L) %-5p - %m%n 7 | 8 | log4j.appender.store=org.apache.log4j.DailyRollingFileAppender 9 | log4j.appender.store.File=D:\\work\\workspace\\redis-cluster\\redis-cluster-config\\config.log 10 | log4j.appender.store.Append=true 11 | log4j.appender.store.DatePattern ='_'yyyy-MM-dd'.log' 12 | log4j.appender.store.layout=org.apache.log4j.PatternLayout 13 | log4j.appender.store.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4r [%t] (%F:%L) %-5p - %m%n 14 | 15 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,stdout,store 2 | 3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 | log4j.appender.stdout.Target=System.out 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern=%d %-4r [%t] (%F:%L) %-5p - %m%n 7 | 8 | log4j.appender.store=org.apache.log4j.DailyRollingFileAppender 9 | log4j.appender.store.File=D:\\work\\workspace\\redis-cluster\\redis-cluster-monitor\\monitor.log 10 | log4j.appender.store.Append=true 11 | log4j.appender.store.DatePattern ='_'yyyy-MM-dd'.log' 12 | log4j.appender.store.layout=org.apache.log4j.PatternLayout 13 | log4j.appender.store.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4r [%t] (%F:%L) %-5p - %m%n 14 | 15 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/resources/dev/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,stdout,store 2 | 3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 | log4j.appender.stdout.Target=System.out 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern=%d %-4r [%t] (%F:%L) %-5p - %m%n 7 | 8 | log4j.appender.store=org.apache.log4j.DailyRollingFileAppender 9 | log4j.appender.store.File=D:\\work\\workspace\\redis-cluster\\redis-cluster-monitor\\monitor.log 10 | log4j.appender.store.Append=true 11 | log4j.appender.store.DatePattern ='_'yyyy-MM-dd'.log' 12 | log4j.appender.store.layout=org.apache.log4j.PatternLayout 13 | log4j.appender.store.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4r [%t] (%F:%L) %-5p - %m%n 14 | 15 | -------------------------------------------------------------------------------- /redis-cluster-client/src/test/java/com/linda/cluster/redis/client/SimpleSlotInfo.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | import redis.clients.jedis.Client; 6 | import redis.clients.jedis.Jedis; 7 | 8 | import com.linda.cluster.redis.client.cluster.KeySlotInfo; 9 | 10 | public class SimpleSlotInfo implements KeySlotInfo{ 11 | 12 | private Logger logger = Logger.getLogger(SimpleSlotInfo.class); 13 | 14 | @Override 15 | public void info(String key, int slot, Jedis jedis) { 16 | String redisNode = ""; 17 | if(jedis!=null){ 18 | Client client = jedis.getClient(); 19 | redisNode = client.getHost()+":"+client.getPort(); 20 | } 21 | logger.info("shardingSlot key:"+key+" slot:"+slot+" node:"+redisNode); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,stdout,store 2 | 3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 | log4j.appender.stdout.Target=System.out 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern=%d %-4r [%t] (%F:%L) %-5p - %m%n 7 | 8 | log4j.appender.store=org.apache.log4j.DailyRollingFileAppender 9 | log4j.appender.store.File=D:\\work\\workspace\\redis-cluster\\redis-cluster-keepalived\\keepalived.log 10 | log4j.appender.store.Append=true 11 | log4j.appender.store.DatePattern ='_'yyyy-MM-dd'.log' 12 | log4j.appender.store.layout=org.apache.log4j.PatternLayout 13 | log4j.appender.store.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4r [%t] (%F:%L) %-5p - %m%n 14 | 15 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/resources/test/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,stdout,store 2 | 3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 | log4j.appender.stdout.Target=System.out 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern=%d %-4r [%t] (%F:%L) %-5p - %m%n 7 | 8 | log4j.appender.store=org.apache.log4j.DailyRollingFileAppender 9 | log4j.appender.store.File=/styx/home/hzlindzh/redis-monitors/lindzh-cluster1/monitor1/logs/monitor.log 10 | log4j.appender.store.Append=true 11 | log4j.appender.store.DatePattern ='_'yyyy-MM-dd'.log' 12 | log4j.appender.store.layout=org.apache.log4j.PatternLayout 13 | log4j.appender.store.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4r [%t] (%F:%L) %-5p - %m%n 14 | 15 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/MonitorMemory.sql: -------------------------------------------------------------------------------- 1 | create table redis_monitor_memory( 2 | id bigint primary key auto_increment, 3 | productId bigint not null, 4 | clusterId bigint not null, 5 | redisNodeId bigint not null, 6 | addtime bigint not null, 7 | 8 | used_memory bigint default 0, 9 | used_memory_rss bigint default 0, 10 | used_memory_peak bigint default 0, 11 | used_memory_lua bigint default 0, 12 | mem_fragmentation_ratio float default 0.0, 13 | mem_allocator varchar(20) default null, 14 | index pdt_cluster_node_time_idx(productId,clusterId,redisNodeId,addtime) 15 | ); 16 | 17 | create index pdtIdx on redis_monitor_memory(productId,addtime); 18 | create index clusterIdx on redis_monitor_memory(clusterId,addtime); 19 | create index nodeIdx on redis_monitor_memory(redisNodeId,addtime); -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorReplication.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import java.util.List; 4 | 5 | import lombok.Data; 6 | 7 | @Data 8 | public class MonitorReplication extends MonitorPartBase { 9 | 10 | private long id; 11 | 12 | private String role; 13 | 14 | private int connected_slaves; 15 | 16 | private List slaves; 17 | 18 | private String slaveJson; 19 | 20 | private long master_repl_offset; 21 | 22 | private int repl_backlog_active; 23 | 24 | private long repl_backlog_size; 25 | 26 | private long repl_backlog_first_byte_offset; 27 | 28 | private long repl_backlog_histlen; 29 | 30 | public void addSlave(MonitorSlaveBean slave){ 31 | slaves.add(slave); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/conf/RedisZkConf.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.conf; 2 | 3 | import java.io.InputStream; 4 | import java.util.List; 5 | 6 | import lombok.Data; 7 | 8 | import com.linda.cluster.redis.common.bean.HostAndPort; 9 | import com.linda.cluster.redis.common.utils.IOUtils; 10 | import com.linda.cluster.redis.common.utils.JSONUtils; 11 | 12 | @Data 13 | public class RedisZkConf { 14 | 15 | private String zkBasePath; 16 | private List zkhosts; 17 | private List products; 18 | 19 | public static RedisZkConf loadRedisZkConf(String name){ 20 | InputStream ins = IOUtils.getClassPathInputStream(name); 21 | String json = IOUtils.toString(ins); 22 | return JSONUtils.fromJson(json, RedisZkConf.class); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/ProductDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_product(name) values(#{name}) 9 | 10 | 11 | 14 | 15 | 18 | 19 | 20 | delete from redis_product where id=#{id} 21 | 22 | 23 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorMemory.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class MonitorMemory extends MonitorPartBase { 7 | private long id; 8 | //已使用内存数量 B 9 | private long used_memory; 10 | //分配内存(top 占用内存) 11 | private long used_memory_rss; 12 | //内存峰值 13 | private long used_memory_peak; 14 | //lua使用内存 15 | private long used_memory_lua; 16 | /** 17 | *在理想情况下, used_memory_rss 的值应该只比 used_memory 稍微高一点儿。 18 | 当 rss > used ,且两者的值相差较大时,表示存在(内部或外部的)内存碎片。 19 | 内存碎片的比率可以通过 mem_fragmentation_ratio 的值看出。 20 | 当 used > rss 时,表示 Redis 的部分内存被操作系统换出到交换空间了,在这种情况下,操作可能会产生明显的延迟。 21 | mem_fragmentation_ratio: used_memory_rss 和 used_memory 之间的比率 22 | */ 23 | private float mem_fragmentation_ratio; 24 | 25 | private String mem_allocator; 26 | } 27 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/service/RedisMonitorServerService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.linda.cluster.redis.monitor.dao.MonitorServerDao; 7 | import com.linda.cluster.redis.monitor.pojo.MonitorServer; 8 | 9 | @Service 10 | public class RedisMonitorServerService { 11 | 12 | @Autowired 13 | private MonitorServerDao monitorServerDao; 14 | 15 | public MonitorServer add(MonitorServer server){ 16 | MonitorServer monitorServer = monitorServerDao.getByNode(server.getRedisNodeId()); 17 | if(monitorServer!=null){ 18 | return monitorServer; 19 | } 20 | int add = monitorServerDao.add(server); 21 | return add>0?server:null; 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorStat.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class MonitorStat extends MonitorPartBase{ 7 | 8 | private long id; 9 | 10 | private int total_connections_received; 11 | 12 | private long total_commands_processed; 13 | 14 | private int instantaneous_ops_per_sec; 15 | 16 | private int rejected_connections; 17 | 18 | private int sync_full; 19 | 20 | private int sync_partial_ok; 21 | 22 | private int sync_partial_err; 23 | 24 | private long expired_keys; 25 | 26 | private long evicted_keys; 27 | 28 | private long keyspace_hits; 29 | 30 | private long keyspace_misses; 31 | 32 | private int pubsub_channels; 33 | 34 | private int pubsub_patterns; 35 | 36 | private int latest_fork_usec; 37 | } 38 | -------------------------------------------------------------------------------- /redis-cluster-aof/src/test/java/com/linda/cluster/redis/aof/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.aof; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /redis-cluster-common/src/test/java/com/linda/cluster/redis/common/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /redis-cluster-config/src/test/java/com/linda/cluster/redis/config/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.config; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/test/java/com/linda/cluster/redis/monitor/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/test/java/com/linda/cluster/redis/keepalived/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/ClusterDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_cluster(productId,clusterName) values(#{productId},#{clusterName}) 9 | 10 | 11 | 14 | 15 | 18 | 19 | 20 | delete from redis_cluster where id=#{clusterId} 21 | 22 | 23 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorPersistence.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class MonitorPersistence extends MonitorPartBase { 7 | private long id; 8 | 9 | private long rdb_changes_since_last_save; 10 | 11 | private int rdb_bgsave_in_progress; 12 | 13 | private long rdb_last_save_time; 14 | 15 | private String rdb_last_bgsave_status; 16 | 17 | private long rdb_last_bgsave_time_sec; 18 | 19 | private long rdb_current_bgsave_time_sec; 20 | 21 | private int aof_enabled; 22 | 23 | private int aof_rewrite_in_progress; 24 | 25 | private int aof_rewrite_scheduled; 26 | 27 | private long aof_last_rewrite_time_sec; 28 | 29 | private long aof_current_rewrite_time_sec; 30 | 31 | private String aof_last_bgrewrite_status; 32 | 33 | private String aof_last_write_status; 34 | } 35 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/MonitorReplication.sql: -------------------------------------------------------------------------------- 1 | create table redis_monitor_replication( 2 | id bigint primary key auto_increment, 3 | productId bigint not null, 4 | clusterId bigint not null, 5 | redisNodeId bigint not null, 6 | addtime bigint not null, 7 | 8 | role varchar(10) default null, 9 | connected_slaves int default 0, 10 | slaveJson varchar(100) default null, 11 | master_repl_offset bigint default 0, 12 | repl_backlog_active int default 0, 13 | repl_backlog_size bigint default 0, 14 | repl_backlog_first_byte_offset bigint default 0, 15 | repl_backlog_histlen bigint default 0, 16 | 17 | index pdt_cluster_node_time_idx(productId,clusterId,redisNodeId,addtime) 18 | ); 19 | 20 | create index pdtIdx on redis_monitor_replication(productId,addtime); 21 | create index clusterIdx on redis_monitor_replication(clusterId,addtime); 22 | create index nodeIdx on redis_monitor_replication(redisNodeId,addtime); -------------------------------------------------------------------------------- /redis-cluster-client/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | ### direct log messages to stdout ### 2 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 | log4j.appender.stdout.Target=System.out 4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.stdout.layout.ConversionPattern= %d (%F:%L) [%t] %-5p - %m%n 6 | ###%d %-4r [%t] (%F:%L) %-5p %c%x - %m%n### 7 | 8 | 9 | log4j.appender.dayfile=org.apache.log4j.DailyRollingFileAppender 10 | log4j.appender.dayfile.deleteBackup=true 11 | log4j.appender.dayfile.maxBackupIndex=10 12 | log4j.appender.dayfile.File=./logs/push-redis.log 13 | log4j.appender.dayfile.Append =true 14 | log4j.appender.dayfile.DatePattern ='.'yyyy-MM-dd 15 | log4j.appender.dayfile.Threshold = debug 16 | log4j.appender.dayfile.encoding=UTF-8 17 | log4j.appender.dayfile.layout=org.apache.log4j.PatternLayout 18 | log4j.appender.dayfile.layout.ConversionPattern= %d %-4r [%t] (%F:%L) %-5p %c%x - %m%n -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/start/AbstractMonitorBootStrap.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.start; 2 | 3 | import javax.annotation.Resource; 4 | 5 | import com.linda.cluster.redis.monitor.service.RedisClusterAdminService; 6 | import com.linda.cluster.redis.monitor.service.RedisInfoDataService; 7 | 8 | public class AbstractMonitorBootStrap extends AbstractSpringBootStrap{ 9 | 10 | @Resource 11 | protected RedisClusterAdminService redisClusterAdminService; 12 | @Resource 13 | protected RedisInfoDataService redisInfoDataService; 14 | 15 | public static String getValue(String arg,String prefix){ 16 | if(arg.length()>prefix.length()){ 17 | return arg.substring(prefix.length()); 18 | } 19 | return null; 20 | } 21 | 22 | public static long parseLong(String str,long defaultV){ 23 | if(str!=null){ 24 | return Long.parseLong(str); 25 | } 26 | return defaultV; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /redis-cluster-client/src/test/java/com/linda/cluster/redis/client/SimpleJedisTemplateTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client; 2 | 3 | import com.linda.cluster.redis.client.cluster.JedisTemplate; 4 | import com.linda.cluster.redis.client.cluster.SimpleJedisTemplate; 5 | 6 | public class SimpleJedisTemplateTest { 7 | 8 | public static void main(String[] args) { 9 | JedisTemplate jedisTemplate = new SimpleJedisTemplate("127.0.0.1"); 10 | jedisTemplate.set("testcmd", "534534534"); 11 | String string = jedisTemplate.get("testcmd"); 12 | System.out.println(string); 13 | jedisTemplate.rpush("push", "1"); 14 | jedisTemplate.rpush("push", "2"); 15 | jedisTemplate.rpush("push", "3"); 16 | jedisTemplate.rpush("push", "4"); 17 | jedisTemplate.rpush("push", "5"); 18 | int i = 0; 19 | while(i<5){ 20 | String lpop = jedisTemplate.lpop("push"); 21 | System.out.println(lpop); 22 | i++; 23 | } 24 | jedisTemplate.close(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/service/RedisMonitorKeyspaceService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.linda.cluster.redis.monitor.dao.MonitorKeyspaceDao; 9 | import com.linda.cluster.redis.monitor.pojo.MonitorKeyspace; 10 | 11 | @Service 12 | public class RedisMonitorKeyspaceService { 13 | 14 | @Autowired 15 | private MonitorKeyspaceDao monitorKeyspaceDao; 16 | 17 | public MonitorKeyspace add(List keyspaces){ 18 | MonitorKeyspace merge = this.merge(keyspaces); 19 | if(merge!=null){ 20 | int add = monitorKeyspaceDao.add(merge); 21 | if(add>0){ 22 | return merge; 23 | } 24 | } 25 | return null; 26 | } 27 | 28 | private MonitorKeyspace merge(List keyspaces){ 29 | return keyspaces.get(0); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/sentinel/sentinel-9000/sentinel.conf: -------------------------------------------------------------------------------- 1 | port 9000 2 | # sentinel monitor master host port quromNum 3 | sentinel monitor node8000 127.0.0.1 8101 2 4 | # fail down after mills master msec 5 | sentinel down-after-milliseconds node8000 5000 6 | # master fail over timeout 7 | sentinel failover-timeout node8000 900000 8 | # master can failover 9 | #sentinel can-failover node8000 yes 10 | # parallet sync 11 | sentinel parallel-syncs node8000 2 12 | # Generated by CONFIG REWRITE 13 | dir "/home/linda/soft/redis-sentinel/sentinel/sentinel-9000" 14 | maxclients 4064 15 | maxmemory 3gb 16 | maxmemory-policy noeviction 17 | sentinel config-epoch node8000 1 18 | sentinel leader-epoch node8000 1 19 | sentinel known-slave node8000 127.0.0.1 8000 20 | sentinel known-slave node8000 127.0.0.1 8102 21 | sentinel known-sentinel node8000 127.0.0.1 9001 dfbd2cc02fdbd64ed88f5501b64e7792240dd228 22 | sentinel known-sentinel node8000 127.0.0.1 9002 b0da3ca009cc91467f6dda8183ebb6b594b89a9f 23 | 24 | sentinel current-epoch 1 25 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/sentinel/sentinel-9001/sentinel.conf: -------------------------------------------------------------------------------- 1 | port 9001 2 | # sentinel monitor master host port quromNum 3 | sentinel monitor node8000 127.0.0.1 8101 2 4 | # fail down after mills master msec 5 | sentinel down-after-milliseconds node8000 5000 6 | # master fail over timeout 7 | sentinel failover-timeout node8000 900000 8 | # master can failover 9 | #sentinel can-failover node8000 yes 10 | # parallet sync 11 | sentinel parallel-syncs node8000 2 12 | # Generated by CONFIG REWRITE 13 | dir "/home/linda/soft/redis-sentinel/sentinel/sentinel-9001" 14 | maxclients 4064 15 | maxmemory 3gb 16 | maxmemory-policy noeviction 17 | sentinel config-epoch node8000 1 18 | sentinel leader-epoch node8000 1 19 | sentinel known-slave node8000 127.0.0.1 8000 20 | sentinel known-slave node8000 127.0.0.1 8102 21 | sentinel known-sentinel node8000 127.0.0.1 9000 1d75a8203911983b6cbc2eaf763f4bcec504a5bd 22 | sentinel known-sentinel node8000 127.0.0.1 9002 b0da3ca009cc91467f6dda8183ebb6b594b89a9f 23 | 24 | sentinel current-epoch 1 25 | -------------------------------------------------------------------------------- /redis-conf/redis-sentinel/sentinel/sentinel-9002/sentinel.conf: -------------------------------------------------------------------------------- 1 | port 9002 2 | # sentinel monitor master host port quromNum 3 | sentinel monitor node8000 127.0.0.1 8101 2 4 | # fail down after mills master msec 5 | sentinel down-after-milliseconds node8000 5000 6 | # master fail over timeout 7 | sentinel failover-timeout node8000 900000 8 | # master can failover 9 | #sentinel can-failover node8000 yes 10 | # parallet sync 11 | sentinel parallel-syncs node8000 2 12 | # Generated by CONFIG REWRITE 13 | dir "/home/linda/soft/redis-sentinel/sentinel/sentinel-9002" 14 | maxclients 4064 15 | maxmemory 3gb 16 | maxmemory-policy noeviction 17 | sentinel config-epoch node8000 1 18 | sentinel leader-epoch node8000 1 19 | sentinel known-slave node8000 127.0.0.1 8102 20 | sentinel known-slave node8000 127.0.0.1 8000 21 | sentinel known-sentinel node8000 127.0.0.1 9000 1d75a8203911983b6cbc2eaf763f4bcec504a5bd 22 | sentinel known-sentinel node8000 127.0.0.1 9001 dfbd2cc02fdbd64ed88f5501b64e7792240dd228 23 | 24 | sentinel current-epoch 1 25 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorServer.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class MonitorServer extends MonitorPartBase{ 7 | 8 | private long id; 9 | //redis版本 10 | private String redis_version; 11 | 12 | private String redis_git_sha1; 13 | 14 | private String redis_git_dirty; 15 | //构建ID 16 | private String redis_build_id; 17 | //运行模式 18 | private String redis_mode; 19 | //操作系统 20 | private String os; 21 | //安装架构 22 | private int arch_bits; 23 | //网络分发模型 24 | private String multiplexing_api; 25 | //gcc版本号 26 | private String gcc_version; 27 | //进程ID 28 | private int process_id; 29 | //运行ID 30 | private String run_id; 31 | //端口号 32 | private int tcp_port; 33 | //在线时间 34 | private long uptime_in_seconds; 35 | //在线时间按天计算 36 | private long uptime_in_days; 37 | 38 | private String hz; 39 | 40 | private String lru_clock; 41 | //配置文件 42 | private String config_file; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/MonitorServer.sql: -------------------------------------------------------------------------------- 1 | create table redis_monitor_server( 2 | id bigint primary key auto_increment, 3 | productId bigint not null, 4 | clusterId bigint not null, 5 | redisNodeId bigint not null, 6 | addtime bigint not null, 7 | 8 | redis_version varchar(20) default null, 9 | redis_git_sha1 varchar(20) default null, 10 | redis_git_dirty varchar(20) default null, 11 | redis_build_id varchar(20) default null, 12 | redis_mode varchar(20) default null, 13 | os varchar(20) default null, 14 | arch_bits int default 64, 15 | multiplexing_api varchar(20) default null, 16 | gcc_version varchar(20) default null, 17 | process_id int default 0, 18 | run_id varchar(50) default null, 19 | tcp_port int default 0, 20 | uptime_in_seconds bigint default 0, 21 | uptime_in_days bigint default 0, 22 | hz varchar(20) default null, 23 | lru_clock varchar(50) default null, 24 | config_file varchar(100) default null, 25 | 26 | index pdt_cluster_node_time_idx(productId,clusterId,redisNodeId,addtime) 27 | ); 28 | 29 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/alived/AlivedWatcher.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.alived; 2 | 3 | import org.apache.log4j.Logger; 4 | import org.apache.zookeeper.WatchedEvent; 5 | import org.apache.zookeeper.Watcher; 6 | import org.apache.zookeeper.Watcher.Event.EventType; 7 | import org.apache.zookeeper.Watcher.Event.KeeperState; 8 | 9 | public class AlivedWatcher implements Watcher{ 10 | 11 | private Logger logger = Logger.getLogger(AlivedWatcher.class); 12 | 13 | private Object mutex; 14 | 15 | public AlivedWatcher(Object mutex){ 16 | this.mutex = mutex; 17 | } 18 | 19 | @Override 20 | public void process(WatchedEvent event) { 21 | String path = event.getPath(); 22 | EventType type = event.getType(); 23 | KeeperState state = event.getState(); 24 | if(state==KeeperState.SyncConnected){ 25 | synchronized(mutex){ 26 | mutex.notify(); 27 | } 28 | } 29 | logger.info("keeper event:path "+path+" type:"+type.toString()+" state:"+state.toString()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/MonitorStat.sql: -------------------------------------------------------------------------------- 1 | create table redis_monitor_stat( 2 | id bigint primary key auto_increment, 3 | productId bigint not null, 4 | clusterId bigint not null, 5 | redisNodeId bigint not null, 6 | addtime bigint not null, 7 | 8 | total_connections_received int default 0, 9 | total_commands_processed bigint default 0, 10 | instantaneous_ops_per_sec int default 0, 11 | rejected_connections int default 0, 12 | sync_full int default 0, 13 | sync_partial_ok int default 0, 14 | sync_partial_err int default 0, 15 | expired_keys bigint default 0, 16 | evicted_keys bigint default 0, 17 | keyspace_hits bigint default 0, 18 | keyspace_misses bigint default 0, 19 | pubsub_channels int default 0, 20 | pubsub_patterns int default 0, 21 | latest_fork_usec int default 0, 22 | 23 | index pdt_cluster_node_time_idx(productId,clusterId,redisNodeId,addtime) 24 | ); 25 | 26 | create index pdtIdx on redis_monitor_stat(productId,addtime); 27 | create index clusterIdx on redis_monitor_stat(clusterId,addtime); 28 | create index nodeIdx on redis_monitor_stat(redisNodeId,addtime); 29 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/RedisNodeDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_node(productId,clusterId,name,host,port) values(#{productId},#{clusterId},#{name},#{host},#{port}) 9 | 10 | 11 | 14 | 15 | 18 | 19 | 22 | 23 | 24 | delete from redis_node where id=#{id} 25 | 26 | 27 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/bean/RedisServiceConfig.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.bean; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class RedisServiceConfig { 7 | 8 | private String defaultMasterHost; 9 | private int defaultMasterPort; 10 | private String defaultSlaveHost; 11 | private int defaultSlavePort; 12 | 13 | private String youdaoMasterHost; 14 | private int youdaoMasterPort; 15 | private String youdaoSlaveHost; 16 | private int youdaoSlavePort; 17 | 18 | public enum RedisDomain{ 19 | 20 | Default("default"), 21 | Youdao("youdao"); 22 | 23 | private String domain; 24 | 25 | RedisDomain(String domain){ 26 | this.domain = domain; 27 | } 28 | 29 | public String getDomain(){ 30 | return this.domain; 31 | } 32 | 33 | public static RedisDomain getByDomain(String domain){ 34 | RedisDomain[] values = RedisDomain.values(); 35 | for(RedisDomain v:values){ 36 | if(v.domain.equals(domain)){ 37 | return v; 38 | } 39 | } 40 | return RedisDomain.Default; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /redis-cluster-aof/src/test/java/com/linda/cluster/redis/aof/AofAnalyserTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.aof; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import redis.clients.util.Hashing; 10 | 11 | import com.linda.cluster.redis.common.sharding.Sharding; 12 | 13 | public class AofAnalyserTest { 14 | 15 | public static void main(String[] args) throws IOException { 16 | File aofFile = new File("E:\\redis-aof.aof"); 17 | RedisAofAnalyzer aofAnalyzer = new RedisAofAnalyzer(); 18 | aofAnalyzer.setHashing(Hashing.MD5); 19 | aofAnalyzer.setShardingNode("node-33"); 20 | aofAnalyzer.setShrdingFileBasePath("E:\\aof"); 21 | List shardings = new ArrayList(); 22 | shardings.add(new Sharding(0,100,"node-12")); 23 | shardings.add(new Sharding(101,200,"node-13")); 24 | shardings.add(new Sharding(201,255,"node-14")); 25 | aofAnalyzer.setShardings(shardings); 26 | Map analyse = aofAnalyzer.analyse(aofFile.getAbsolutePath()); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/serializer/SimpleJdkSerializer.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.serializer; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.IOException; 5 | import java.io.ObjectOutputStream; 6 | import java.io.Serializable; 7 | 8 | public class SimpleJdkSerializer implements RedisSerializer{ 9 | 10 | @Override 11 | public byte[] serializeKey(String key) { 12 | return key.getBytes(); 13 | } 14 | 15 | @Override 16 | public byte[] serializeValue(Object value) { 17 | if(value instanceof Serializable){ 18 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); 19 | try { 20 | ObjectOutputStream stream = new ObjectOutputStream(bos); 21 | stream.writeObject(value); 22 | byte[] array = bos.toByteArray(); 23 | bos.close(); 24 | return array; 25 | } catch (IOException e) { 26 | //can't happen 27 | throw new IllegalArgumentException("write object error"); 28 | } 29 | }else{ 30 | throw new IllegalArgumentException(value.getClass()+" must implements Serializable"); 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /redis-cluster-client/src/test/java/com/linda/cluster/redis/client/SimpleRedisMigrateTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client; 2 | 3 | import java.util.List; 4 | 5 | import redis.clients.jedis.Jedis; 6 | import redis.clients.jedis.JedisPool; 7 | import redis.clients.jedis.ScanResult; 8 | 9 | public class SimpleRedisMigrateTest { 10 | 11 | public static void main(String[] args) { 12 | JedisPool jedisPool = new JedisPool("192.168.139.129",7770); 13 | Jedis jedis = jedisPool.getResource(); 14 | String destHost = "192.168.139.129"; 15 | int destPort = 7771; 16 | ScanResult scanResult = jedis.scan("0"); 17 | String cursor = scanResult.getStringCursor(); 18 | List keys = scanResult.getResult(); 19 | while(cursor!=null&&keys!=null&&keys.size()>0){ 20 | System.out.println("----migrate cursor:"+cursor); 21 | for(String key:keys){ 22 | jedis.migrate(destHost, destPort, key, 0, 3000); 23 | System.out.println("migrate:"+key); 24 | } 25 | scanResult = jedis.scan(cursor); 26 | cursor = scanResult.getStringCursor(); 27 | keys = scanResult.getResult(); 28 | } 29 | jedis.close(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/bean/RedisZookeeperServiceConfig.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.bean; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class RedisZookeeperServiceConfig { 7 | private String servers; 8 | private String path; 9 | private String chroot; 10 | private String username; 11 | private String password; 12 | private int zooTimeout; 13 | private int redisTimeout; 14 | private int retrytime; 15 | 16 | @Data 17 | public static class RedisClientBean{ 18 | private String host; 19 | private int port; 20 | private String password; 21 | } 22 | 23 | public enum RedisEnum{ 24 | Master("master"), 25 | Slave("slave"); 26 | 27 | private String name; 28 | 29 | RedisEnum(String name){ 30 | this.name = name; 31 | } 32 | 33 | public String getName(String name){ 34 | return name; 35 | } 36 | 37 | public static RedisEnum getByName(String name){ 38 | RedisEnum[] vs = RedisEnum.values(); 39 | for(RedisEnum v:vs){ 40 | if(v.name.equalsIgnoreCase(name)){ 41 | return v; 42 | } 43 | } 44 | return Master; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/resources/spring-context-config.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/redis/RedisKeepAlived.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.redis; 2 | 3 | import java.util.concurrent.atomic.AtomicBoolean; 4 | 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import com.linda.cluster.redis.common.Service; 9 | 10 | 11 | public class RedisKeepAlived extends RedisAliveBase implements Runnable,Service{ 12 | @Setter 13 | @Getter 14 | private int pingInterval; 15 | 16 | private Thread pingThread; 17 | 18 | private AtomicBoolean stop = new AtomicBoolean(false); 19 | 20 | @Override 21 | public void run() { 22 | while(!stop.get()){ 23 | this.ping(); 24 | super.onPing(this); 25 | try { 26 | Thread.currentThread().sleep(pingInterval); 27 | } catch (InterruptedException e) { 28 | stop.set(true); 29 | break; 30 | } 31 | } 32 | } 33 | 34 | @Override 35 | public void startup() { 36 | this.connect(); 37 | pingThread = new Thread(this); 38 | pingThread.start(); 39 | } 40 | 41 | @Override 42 | public void shutdown() { 43 | stop.set(true); 44 | pingThread.interrupt(); 45 | this.close(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /redis-cluster-client/src/test/java/com/linda/cluster/redis/client/SimpleClientLuaTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client; 2 | 3 | import java.util.ArrayList; 4 | 5 | import redis.clients.jedis.Jedis; 6 | import redis.clients.jedis.JedisPool; 7 | 8 | 9 | public class SimpleClientLuaTest { 10 | 11 | public static void main(String[] args2) { 12 | 13 | JedisPool jedisPool = new JedisPool("192.168.139.129",7770); 14 | Jedis jedis = jedisPool.getResource(); 15 | String lua = "local exist = redis.call('EXISTS',KEYS[1]) "+ 16 | "if exist==1 then "+ 17 | " redis.call('set',KEYS[1],ARGV[1]) "+ 18 | " return '1' "+ 19 | "else return '0' "+ 20 | "end"; 21 | ArrayList keys = new ArrayList(); 22 | keys.add("lin"); 23 | ArrayList args = new ArrayList(); 24 | args.add("hello world"); 25 | Object result = jedis.eval(lua, keys, args); 26 | System.out.println("result:"+result); 27 | 28 | int index = 100000; 29 | while(index<200000){ 30 | String key = "key_"+index; 31 | String value = "val_"+index; 32 | jedis.set(key, value); 33 | index++; 34 | } 35 | jedis.close(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/sql/MonitorPersistence.sql: -------------------------------------------------------------------------------- 1 | create table redis_monitor_persistence( 2 | id bigint primary key auto_increment, 3 | productId bigint not null, 4 | clusterId bigint not null, 5 | redisNodeId bigint not null, 6 | addtime bigint not null, 7 | 8 | rdb_changes_since_last_save bigint default 0, 9 | rdb_bgsave_in_progress int default 0, 10 | rdb_last_save_time bigint default 0, 11 | rdb_last_bgsave_status varchar(10) default 0, 12 | rdb_last_bgsave_time_sec bigint default 0, 13 | rdb_current_bgsave_time_sec bigint default 0, 14 | aof_enabled int default 0, 15 | aof_rewrite_in_progress int default 0, 16 | aof_rewrite_scheduled int default 0, 17 | aof_last_rewrite_time_sec bigint default 0, 18 | aof_current_rewrite_time_sec bigint default 0, 19 | aof_last_bgrewrite_status varchar(10) default 0, 20 | aof_last_write_status varchar(10) default 0, 21 | 22 | index pdt_cluster_node_time_idx(productId,clusterId,redisNodeId,addtime) 23 | ); 24 | 25 | create index pdtIdx on redis_monitor_persistence(productId,addtime); 26 | create index clusterIdx on redis_monitor_persistence(clusterId,addtime); 27 | create index nodeIdx on redis_monitor_persistence(redisNodeId,addtime); -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/zk/ClusterGetChildrenCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.zk; 2 | 3 | import java.util.Collections; 4 | import java.util.List; 5 | 6 | import org.apache.zookeeper.Watcher; 7 | import org.apache.zookeeper.ZooKeeper; 8 | 9 | import com.linda.cluster.redis.common.bean.CountBean; 10 | import com.linda.cluster.redis.common.utils.RedisGetChildrenCallback; 11 | import com.linda.cluster.redis.common.utils.RedisZookeeperUtils; 12 | 13 | public class ClusterGetChildrenCallback implements RedisGetChildrenCallback{ 14 | 15 | private ZooKeeper zooKeeper; 16 | 17 | public ClusterGetChildrenCallback(ZooKeeper zooKeeper){ 18 | this.zooKeeper = zooKeeper; 19 | } 20 | 21 | @Override 22 | public List onConnectionLoss(String path, Watcher watcher,CountBean count) { 23 | if(count.getCount()>5){ 24 | return Collections.emptyList(); 25 | } 26 | return RedisZookeeperUtils.zkGetChildren(zooKeeper, path, watcher, count, this); 27 | } 28 | 29 | @Override 30 | public List onZkException(String path, Watcher watcher,CountBean count) { 31 | return this.onConnectionLoss(path, watcher, count); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/zk/ClusterNodeDeleteCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.zk; 2 | 3 | import org.apache.zookeeper.ZooKeeper; 4 | 5 | import com.linda.cluster.redis.common.bean.CountBean; 6 | import com.linda.cluster.redis.common.utils.RedisNodeDeleteCallback; 7 | import com.linda.cluster.redis.common.utils.RedisZookeeperUtils; 8 | 9 | public class ClusterNodeDeleteCallback implements RedisNodeDeleteCallback{ 10 | 11 | private ZooKeeper zooKeeper; 12 | 13 | public ClusterNodeDeleteCallback(ZooKeeper zooKeeper){ 14 | this.zooKeeper = zooKeeper; 15 | } 16 | 17 | @Override 18 | public boolean onNodeNotExist(String path, int version, CountBean count) { 19 | return true; 20 | } 21 | 22 | @Override 23 | public boolean onConnectionLoss(String path, int version,CountBean count) { 24 | int cc = count.getCount(); 25 | if(cc>5){ 26 | return false; 27 | } 28 | return RedisZookeeperUtils.zkDeleteNode(zooKeeper, version, path, count, this); 29 | } 30 | 31 | @Override 32 | public boolean onZkException(String path, int version, CountBean count) { 33 | return this.onConnectionLoss(path, version, count); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/zk/RedisNodeSetDataCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.zk; 2 | 3 | import org.apache.zookeeper.ZooKeeper; 4 | import org.apache.zookeeper.data.Stat; 5 | 6 | import com.linda.cluster.redis.common.bean.CountBean; 7 | import com.linda.cluster.redis.common.utils.RedisSetNodeDataCallback; 8 | import com.linda.cluster.redis.common.utils.RedisZookeeperUtils; 9 | 10 | public class RedisNodeSetDataCallback implements RedisSetNodeDataCallback{ 11 | 12 | private ZooKeeper zooKeeper; 13 | 14 | public RedisNodeSetDataCallback(ZooKeeper zooKeeper){ 15 | this.zooKeeper = zooKeeper; 16 | } 17 | 18 | @Override 19 | public Stat onNodeNotExist(String path, byte[] data, int version,CountBean count) { 20 | return null; 21 | } 22 | 23 | @Override 24 | public Stat onConnectionLoss(String path, byte[] data, int version,CountBean count) { 25 | if(count.getCount()>5){ 26 | return null; 27 | }else{ 28 | return RedisZookeeperUtils.zkSetNodeData(zooKeeper, path, version, data, count, this); 29 | } 30 | } 31 | 32 | @Override 33 | public Stat onZkException(String path, byte[] data, int version,CountBean count) { 34 | return this.onConnectionLoss(path, data, version, count); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/ClassPathGenerator.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common; 2 | 3 | import java.io.File; 4 | 5 | public class ClassPathGenerator { 6 | 7 | public static void main(String[] args) { 8 | String base = "D:\\work\\workspace\\redis-cluster\\redis-cluster-monitor\\src\\main\\webapp\\monitor\\lib"; 9 | String prefix = "\"$BASEDIR\"\\"; 10 | String seperator = ":"; 11 | File file = new File(base); 12 | String files = ClassPathGenerator.genClassPath(file, prefix, seperator); 13 | System.out.println(files); 14 | } 15 | 16 | public static String genClassPath(File file,String prefix,String seperator){ 17 | StringBuilder sb = new StringBuilder(); 18 | if(file.isDirectory()){ 19 | String sprefix = prefix+file.getName()+"\\"; 20 | File[] files = file.listFiles(); 21 | int index = 0; 22 | for(File f:files){ 23 | sb.append(genClassPath(f,sprefix,seperator)); 24 | if(index0){ 26 | sb.append(seperator); 27 | } 28 | } 29 | index++; 30 | } 31 | }else{ 32 | String name = file.getName(); 33 | if(name.endsWith(".jar")){ 34 | sb.append(prefix); 35 | sb.append(file.getName()); 36 | } 37 | } 38 | return sb.toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/MonitorCpuDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import com.linda.cluster.redis.monitor.pojo.MonitorCpu; 8 | 9 | public interface MonitorCpuDao { 10 | 11 | public int add(MonitorCpu cpu); 12 | 13 | public List getByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 14 | 15 | public int getCountByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end); 16 | 17 | public List getByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 18 | 19 | public int getCountByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end); 20 | 21 | public List getByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 22 | 23 | public int getCountByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end); 24 | } 25 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/MonitorStatDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import com.linda.cluster.redis.monitor.pojo.MonitorStat; 8 | 9 | public interface MonitorStatDao { 10 | 11 | public int add(MonitorStat stat); 12 | 13 | public List getByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 14 | 15 | public int getCountByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end); 16 | 17 | public List getByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 18 | 19 | public int getCountByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end); 20 | 21 | public List getByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 22 | 23 | public int getCountByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end); 24 | } 25 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/MonitorMemoryDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import com.linda.cluster.redis.monitor.pojo.MonitorMemory; 8 | 9 | public interface MonitorMemoryDao { 10 | 11 | public int add(MonitorMemory memory); 12 | 13 | public List getByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 14 | 15 | public int getCountByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end); 16 | 17 | public List getByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 18 | 19 | public int getCountByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end); 20 | 21 | public List getByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 22 | 23 | public int getCountByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/RedisMonitorInfoBean.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor; 2 | 3 | import java.util.List; 4 | 5 | import lombok.Data; 6 | 7 | import com.linda.cluster.redis.monitor.pojo.Cluster; 8 | import com.linda.cluster.redis.monitor.pojo.MonitorClients; 9 | import com.linda.cluster.redis.monitor.pojo.MonitorCpu; 10 | import com.linda.cluster.redis.monitor.pojo.MonitorKeyspace; 11 | import com.linda.cluster.redis.monitor.pojo.MonitorMemory; 12 | import com.linda.cluster.redis.monitor.pojo.MonitorPersistence; 13 | import com.linda.cluster.redis.monitor.pojo.MonitorReplication; 14 | import com.linda.cluster.redis.monitor.pojo.MonitorServer; 15 | import com.linda.cluster.redis.monitor.pojo.MonitorStat; 16 | import com.linda.cluster.redis.monitor.pojo.Product; 17 | import com.linda.cluster.redis.monitor.pojo.RedisNode; 18 | 19 | @Data 20 | public class RedisMonitorInfoBean { 21 | private Product product; 22 | private Cluster cluster; 23 | private RedisNode node; 24 | private MonitorClients clients; 25 | private MonitorCpu cpu; 26 | private List keyspaces; 27 | private MonitorMemory memory; 28 | private MonitorPersistence persistence; 29 | private MonitorReplication replication; 30 | private MonitorServer server; 31 | private MonitorStat stat; 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/pojo/MonitorKeyspace.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.pojo; 2 | 3 | 4 | public class MonitorKeyspace extends MonitorPartBase { 5 | 6 | private long id; 7 | 8 | private int databaseId; 9 | 10 | private int keys; 11 | private int expires; 12 | private int avg_ttl; 13 | 14 | private int keynum; 15 | 16 | public long getId() { 17 | return id; 18 | } 19 | 20 | public void setId(long id) { 21 | this.id = id; 22 | } 23 | 24 | public int getDatabaseId() { 25 | return databaseId; 26 | } 27 | 28 | public void setDatabaseId(int databaseId) { 29 | this.databaseId = databaseId; 30 | } 31 | 32 | public int getKeys() { 33 | return keys; 34 | } 35 | 36 | public void setKeys(int keys) { 37 | this.keynum = keys; 38 | this.keys = keys; 39 | } 40 | 41 | public int getExpires() { 42 | return expires; 43 | } 44 | 45 | public void setExpires(int expires) { 46 | this.expires = expires; 47 | } 48 | 49 | public int getAvg_ttl() { 50 | return avg_ttl; 51 | } 52 | 53 | public void setAvg_ttl(int avg_ttl) { 54 | this.avg_ttl = avg_ttl; 55 | } 56 | 57 | public int getKeynum() { 58 | return keynum; 59 | } 60 | 61 | public void setKeynum(int keynum) { 62 | this.keys = keynum; 63 | this.keynum = keynum; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/MonitorClientsDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import com.linda.cluster.redis.monitor.pojo.MonitorClients; 8 | 9 | public interface MonitorClientsDao { 10 | 11 | public int add(MonitorClients clients); 12 | 13 | public List getByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 14 | 15 | public int getCountByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end); 16 | 17 | public List getByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 18 | 19 | public int getCountByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end); 20 | 21 | public List getByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 22 | 23 | public int getCountByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/utils/RedisUtils.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.utils; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.List; 6 | 7 | import redis.clients.jedis.HostAndPort; 8 | 9 | import com.linda.cluster.redis.common.sharding.Sharding; 10 | 11 | public class RedisUtils { 12 | 13 | public static String toString(List hosts){ 14 | int size = hosts.size(); 15 | StringBuilder sb = new StringBuilder(); 16 | int index = 0; 17 | for(HostAndPort host:hosts){ 18 | sb.append(host.getHost()); 19 | sb.append(":"); 20 | sb.append(host.getPort()); 21 | if(index getAListNotInBList(Collection a,Collection b){ 41 | List list = new ArrayList(); 42 | for(String s:a){ 43 | if(!b.contains(s)){ 44 | list.add(s); 45 | } 46 | } 47 | return list; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/redis/SimpleRedisAliveNode.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.redis; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.apache.log4j.Logger; 7 | 8 | import com.linda.cluster.redis.common.bean.HostAndPort; 9 | import com.linda.cluster.redis.common.utils.RedisZookeeperUtils; 10 | 11 | public class SimpleRedisAliveNode extends RedisAliveBase{ 12 | 13 | private Logger logger = Logger.getLogger(SimpleRedisAliveNode.class); 14 | 15 | public boolean slaveOf(HostAndPort hostAndPort){ 16 | try{ 17 | if(hostAndPort!=null){ 18 | logger.info(this.redisHost.getName()+" slaveof "+hostAndPort.getName()); 19 | jedis.slaveof(hostAndPort.getHost(), hostAndPort.getPort()); 20 | }else{ 21 | logger.info(this.redisHost.getName()+" slaveof no one"); 22 | jedis.slaveofNoOne(); 23 | } 24 | return true; 25 | }catch(Exception e){ 26 | super.onException(this, e); 27 | } 28 | return false; 29 | } 30 | 31 | public List getSlaves(){ 32 | List slaves = new ArrayList(); 33 | try{ 34 | String info = jedis.info(); 35 | return RedisZookeeperUtils.getSlaves(info); 36 | }catch(Exception e){ 37 | super.onException(this, e); 38 | } 39 | return slaves; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /redis-cluster-client/src/test/java/com/linda/cluster/redis/client/ClusterJedisTemplateTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import redis.clients.jedis.JedisPoolConfig; 7 | 8 | import com.linda.cluster.redis.client.cluster.ClusterJedisTemplate; 9 | import com.linda.cluster.redis.common.bean.HostAndPort; 10 | 11 | public class ClusterJedisTemplateTest { 12 | 13 | public static void main(String[] args) { 14 | String basePath = "/redis-cluster"; 15 | String product = "yixin12"; 16 | String password = null; 17 | List zkhost = new ArrayList(); 18 | zkhost.add(new HostAndPort("10.120.151.105",10001)); 19 | zkhost.add(new HostAndPort("10.120.151.105",10002)); 20 | zkhost.add(new HostAndPort("10.120.151.105",10003)); 21 | JedisPoolConfig config = new JedisPoolConfig(); 22 | ClusterJedisTemplate template = new ClusterJedisTemplate(config, zkhost, product, password, basePath); 23 | template.setSlotInfo(new SimpleSlotInfo()); 24 | String keyPrefix = "test-"; 25 | int index = 100; 26 | while(index<200){ 27 | String key = keyPrefix+index; 28 | String value = "this is "+key; 29 | template.set(key, value); 30 | index++; 31 | } 32 | System.out.println("----------------------------"); 33 | template.close(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/zk/MonitorNodeCreateCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.zk; 2 | 3 | import org.apache.zookeeper.CreateMode; 4 | import org.apache.zookeeper.ZooKeeper; 5 | 6 | import com.linda.cluster.redis.common.bean.CountBean; 7 | import com.linda.cluster.redis.common.utils.RedisZookeeperUtils; 8 | import com.linda.cluster.redis.common.utils.ZkNodeCreateCallback; 9 | 10 | public class MonitorNodeCreateCallback implements ZkNodeCreateCallback{ 11 | 12 | private ZooKeeper zooKeeper; 13 | 14 | private CreateMode createMode; 15 | 16 | public MonitorNodeCreateCallback(ZooKeeper zooKeeper,CreateMode createMode){ 17 | this.zooKeeper = zooKeeper; 18 | this.createMode = createMode; 19 | } 20 | 21 | @Override 22 | public boolean onNodeExist(String path, byte[] data, CountBean count) { 23 | //can't happen 24 | return false; 25 | } 26 | 27 | @Override 28 | public boolean onConnectionLoss(String path, byte[] data,CountBean count) { 29 | if(count.getCount()>5){ 30 | return false; 31 | } 32 | return RedisZookeeperUtils.zkCreateNode(zooKeeper, path, data,createMode, count, this); 33 | } 34 | 35 | @Override 36 | public boolean onZkException(String path, byte[] data, CountBean count) { 37 | return this.onConnectionLoss(path, data, count); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/zk/RedisNodeConfigDataCallback.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.zk; 2 | 3 | import org.apache.zookeeper.Watcher; 4 | import org.apache.zookeeper.ZooKeeper; 5 | 6 | import com.linda.cluster.redis.common.bean.CountBean; 7 | import com.linda.cluster.redis.common.bean.RedisZkData; 8 | import com.linda.cluster.redis.common.utils.RedisGetNodeDataCallback; 9 | import com.linda.cluster.redis.common.utils.RedisZookeeperUtils; 10 | 11 | public class RedisNodeConfigDataCallback implements RedisGetNodeDataCallback{ 12 | 13 | private ZooKeeper zooKeeper; 14 | 15 | public RedisNodeConfigDataCallback(ZooKeeper zooKeeper){ 16 | this.zooKeeper = zooKeeper; 17 | } 18 | 19 | @Override 20 | public RedisZkData onNodeNotExist(String path, byte[] data,Watcher watcher, CountBean count) { 21 | return null; 22 | } 23 | 24 | @Override 25 | public RedisZkData onConnectionLoss(String path, byte[] data,Watcher watcher, CountBean count) { 26 | if(count.getCount()>5){ 27 | return null; 28 | } 29 | return RedisZookeeperUtils.zkGetNodeData(zooKeeper, watcher, path, data, count, this); 30 | } 31 | 32 | @Override 33 | public RedisZkData onZkException(String path, byte[] data,Watcher watcher, CountBean count) { 34 | return this.onConnectionLoss(path, data, watcher, count); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/MonitorKeyspaceDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import com.linda.cluster.redis.monitor.pojo.MonitorKeyspace; 8 | 9 | public interface MonitorKeyspaceDao { 10 | 11 | //先合并再加入DB 12 | public int add(MonitorKeyspace keyspace); 13 | 14 | public List getByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 15 | 16 | public int getCountByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end); 17 | 18 | public List getByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 19 | 20 | public int getCountByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end); 21 | 22 | public List getByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 23 | 24 | public int getCountByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end); 25 | } 26 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/MonitorReplicationDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import com.linda.cluster.redis.monitor.pojo.MonitorReplication; 8 | 9 | public interface MonitorReplicationDao { 10 | 11 | public int add(MonitorReplication replication); 12 | 13 | public List getByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 14 | 15 | public int getCountByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end); 16 | 17 | public List getByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 18 | 19 | public int getCountByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end); 20 | 21 | public List getByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 22 | 23 | public int getCountByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end); 24 | } 25 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/MonitorPersistenceDao.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import com.linda.cluster.redis.monitor.pojo.MonitorPersistence; 8 | 9 | public interface MonitorPersistenceDao { 10 | 11 | public int add(MonitorPersistence persistence); 12 | 13 | public List getByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 14 | 15 | public int getCountByProductAndTime(@Param("productId")long productId,@Param("start")long start,@Param("end")long end); 16 | 17 | public List getByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 18 | 19 | public int getCountByClusterAndTime(@Param("clusterId")long clusterId,@Param("start")long start,@Param("end")long end); 20 | 21 | public List getByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end,@Param("limit")int limit,@Param("offset")int offset); 22 | 23 | public int getCountByNodeAndTime(@Param("nodeId")long nodeId,@Param("start")long start,@Param("end")long end); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/spring/MonitorTransactionAdvice.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.spring; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | import org.apache.log4j.Logger; 6 | import org.aspectj.lang.ProceedingJoinPoint; 7 | import org.aspectj.lang.annotation.Around; 8 | import org.aspectj.lang.annotation.Aspect; 9 | import org.aspectj.lang.reflect.MethodSignature; 10 | import org.springframework.beans.BeansException; 11 | import org.springframework.context.ApplicationContext; 12 | import org.springframework.context.ApplicationContextAware; 13 | import org.springframework.stereotype.Component; 14 | 15 | @Component 16 | @Aspect 17 | public class MonitorTransactionAdvice implements ApplicationContextAware{ 18 | 19 | private Logger logger = Logger.getLogger(MonitorTransactionAdvice.class); 20 | 21 | @Around("execution(@com.linda.cluster.redis.monitor.spring.MonitorTransaction * *(..))") 22 | public Object processThread(ProceedingJoinPoint pjp) throws Throwable{ 23 | Object result = null; 24 | Object[] args = pjp.getArgs(); 25 | MethodSignature signature = (MethodSignature) pjp.getSignature(); 26 | Method method = signature.getMethod(); 27 | return result; 28 | } 29 | 30 | @Override 31 | public void setApplicationContext(ApplicationContext apc) 32 | throws BeansException { 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/test/java/com/linda/cluster/redis/keepalived/redis/RedisKeepAlivedTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.redis; 2 | 3 | import com.linda.cluster.redis.common.bean.HostAndPort; 4 | 5 | public class RedisKeepAlivedTest { 6 | 7 | public static void main(String[] args) { 8 | final RedisKeepAlived alived = new RedisKeepAlived(); 9 | alived.setPingInterval(5000); 10 | alived.setRedisHost(new HostAndPort("test","127.0.0.1",6379, false, null,null,null)); 11 | alived.addRedisListener(new RedisAlivedListener(){ 12 | @Override 13 | public void onConnected(RedisAliveBase redis) { 14 | System.out.println("connected:"+redis.getRedisHost().toString()); 15 | } 16 | 17 | @Override 18 | public void onClose(RedisAliveBase redis) { 19 | System.out.println("closed:"+redis.getRedisHost().toString()); 20 | } 21 | 22 | @Override 23 | public void onException(RedisAliveBase redis,Exception e) { 24 | e.printStackTrace(); 25 | System.out.println("exception:"+redis.getRedisHost().toString()); 26 | } 27 | 28 | @Override 29 | public void onInfo(RedisAliveBase redis, String info) { 30 | System.out.println("info:"+redis.getRedisHost().toString()+" info:"+info); 31 | } 32 | 33 | @Override 34 | public void onPing(RedisAliveBase redis) { 35 | 36 | } 37 | }); 38 | alived.startup(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /redis-cluster-aof/src/test/java/com/linda/cluster/redis/aof/AofObjectTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.aof; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | 8 | import com.linda.cluster.redis.common.utils.IOUtils; 9 | 10 | public class AofObjectTest { 11 | 12 | public static void main(String[] args) throws IOException { 13 | File aofFile = new File("E:\\redis-aof.aof"); 14 | InputStream fis = IOUtils.getFileInputStream(aofFile.getAbsolutePath()); 15 | File file = new File("E:\\redis-aof.backup"); 16 | if(file.exists()){ 17 | file.delete(); 18 | file.createNewFile(); 19 | } 20 | FileOutputStream fos = new FileOutputStream(file); 21 | int len = 1024*1024; 22 | AofObject aof = new AofObject(len); 23 | long fidx = 0; 24 | long flen = aofFile.length(); 25 | // byte[] buf = new byte[len]; 26 | while(fidx0){ 30 | fidx+=read; 31 | // aof.write(buf,0,read); 32 | while(aof.HasObject()){ 33 | System.out.println(aof.getKey()); 34 | System.out.println(aof.getOperation()); 35 | System.out.println("----------------------------"); 36 | fos.write(aof.aofBytes()); 37 | } 38 | } 39 | } 40 | fos.close(); 41 | fis.close(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/start/RedisProductMonitorBootStrap.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.start; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | import com.linda.cluster.redis.monitor.RedisProductMonitor; 6 | import com.linda.cluster.redis.monitor.pojo.Product; 7 | 8 | public class RedisProductMonitorBootStrap extends AbstractMonitorBootStrap { 9 | private static Logger logger = Logger.getLogger(RedisProductMonitorBootStrap.class); 10 | 11 | public RedisProductMonitor newProductMonitor(long productId){ 12 | Product product = redisClusterAdminService.getProduct(productId, true); 13 | if(product==null){ 14 | throw new IllegalArgumentException("product can't be null"); 15 | } 16 | return new RedisProductMonitor(product,redisInfoDataService,redisClusterAdminService); 17 | } 18 | 19 | public static void main(String[] args) { 20 | long productId = -1; 21 | if(args!=null){ 22 | for(String arg:args){ 23 | if(arg.startsWith("-p")){ 24 | productId = parseLong(getValue(arg,"-p"),productId); 25 | } 26 | } 27 | } 28 | if(productId<0){ 29 | throw new IllegalArgumentException("invalid product parameter"); 30 | } 31 | RedisProductMonitorBootStrap strap = new RedisProductMonitorBootStrap(); 32 | strap.startup(); 33 | logger.info("resource inject success"); 34 | RedisProductMonitor monitor = strap.newProductMonitor(productId); 35 | monitor.startup(); 36 | logger.info("monitor startup productId:"+productId); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/KeyValueUtils.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Properties; 6 | 7 | import com.linda.cluster.redis.common.bean.KeyValueBean; 8 | 9 | public class KeyValueUtils { 10 | 11 | public static KeyValueBean toKeyValue(String line,String split){ 12 | if(line!=null){ 13 | int idx = line.indexOf(split); 14 | if(idx>0&&idx keyvalues){ 24 | if(keyvalues!=null&&keyvalues.size()>0){ 25 | Properties props = new Properties(); 26 | for(KeyValueBean keyvalue:keyvalues){ 27 | props.setProperty(keyvalue.getKey(), keyvalue.getValue()); 28 | } 29 | return props; 30 | } 31 | return null; 32 | } 33 | 34 | public static List toKeyValue(String line,String kvSplit,String seperator){ 35 | List list = new ArrayList(); 36 | if(line!=null){ 37 | String[] splits = line.trim().split(seperator); 38 | if(splits!=null){ 39 | for(String kv:splits){ 40 | KeyValueBean bean = KeyValueUtils.toKeyValue(kv, kvSplit); 41 | if(bean!=null){ 42 | list.add(bean); 43 | } 44 | } 45 | } 46 | } 47 | return list; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/spring/MonitorTransactionManager.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.spring; 2 | 3 | import org.apache.log4j.Logger; 4 | import org.springframework.beans.BeansException; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.context.ApplicationContextAware; 7 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 8 | import org.springframework.transaction.TransactionDefinition; 9 | import org.springframework.transaction.support.DefaultTransactionStatus; 10 | 11 | public class MonitorTransactionManager extends DataSourceTransactionManager implements ApplicationContextAware{ 12 | 13 | private static final long serialVersionUID = -3177438254181907166L; 14 | 15 | private Logger logger = Logger.getLogger(MonitorTransactionManager.class); 16 | 17 | @Override 18 | protected void doBegin(Object transaction, TransactionDefinition definition) { 19 | super.doBegin(transaction, definition); 20 | logger.info("---------start---transaction--------"); 21 | } 22 | 23 | @Override 24 | protected void doCommit(DefaultTransactionStatus status) { 25 | super.doCommit(status); 26 | } 27 | 28 | @Override 29 | protected void doRollback(DefaultTransactionStatus status) { 30 | super.doRollback(status); 31 | logger.info("---------rollback---transaction--------"); 32 | } 33 | 34 | @Override 35 | public void setApplicationContext(ApplicationContext applicationContext) 36 | throws BeansException { 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/MonitorServerDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_monitor_server(productId,clusterId,redisNodeId,addtime,redis_version,redis_git_sha1, 9 | redis_git_dirty,redis_build_id,redis_mode,os,arch_bits,multiplexing_api,gcc_version,process_id,run_id, 10 | tcp_port,uptime_in_seconds,uptime_in_days,hz,lru_clock,config_file) 11 | values(#{productId},#{clusterId},#{redisNodeId},#{addtime},#{redis_version},#{redis_git_sha1}, 12 | #{redis_git_dirty},#{redis_build_id},#{redis_mode},#{os},#{arch_bits},#{multiplexing_api},#{gcc_version}, 13 | #{process_id},#{run_id},#{tcp_port},#{uptime_in_seconds},#{uptime_in_days},#{hz},#{lru_clock}, 14 | #{config_file}) 15 | 16 | 17 | 20 | 21 | 24 | 25 | 28 | 29 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/test/java/com/linda/cluster/redis/monitor/RedisNodeMonitorTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor; 2 | 3 | import redis.clients.jedis.HostAndPort; 4 | import redis.clients.jedis.Jedis; 5 | 6 | import com.linda.cluster.redis.monitor.pojo.Cluster; 7 | import com.linda.cluster.redis.monitor.pojo.Product; 8 | import com.linda.cluster.redis.monitor.pojo.RedisNode; 9 | import com.linda.cluster.redis.monitor.service.RedisInfoDataService; 10 | 11 | public class RedisNodeMonitorTest { 12 | 13 | public static void main(String[] args) { 14 | 15 | long productId = 1234; 16 | long clusterId = 42342; 17 | long nodeId = 432432; 18 | 19 | String host = "yixin12.server.163.org"; 20 | int port = 22121; 21 | 22 | Product product = new Product(); 23 | product.setId(productId); 24 | product.setName("yixin"); 25 | 26 | Cluster cluster = new Cluster(); 27 | cluster.setClusterName("ha-queue1"); 28 | cluster.setProductId(productId); 29 | cluster.setId(clusterId); 30 | 31 | RedisNode node = new RedisNode(); 32 | node.setClusterId(clusterId); 33 | node.setHost(host); 34 | node.setId(nodeId); 35 | node.setPort(port); 36 | node.setProductId(productId); 37 | node.setName("ha-queue1-node1"); 38 | 39 | HostAndPort redisHostAndPort = new HostAndPort(host, port); 40 | Jedis jedis = new Jedis(redisHostAndPort.getHost(),redisHostAndPort.getPort()); 41 | //jedis.connect(); 42 | 43 | RedisMonitor monitor = new RedisMonitor(); 44 | monitor.setInfoDataService(new RedisInfoDataService()); 45 | 46 | monitor.infoNode(jedis, product, cluster, node); 47 | 48 | jedis.close(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/cluster/SimpleJedisTemplate.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.cluster; 2 | 3 | import redis.clients.jedis.Jedis; 4 | import redis.clients.jedis.JedisPool; 5 | import redis.clients.jedis.JedisPoolConfig; 6 | 7 | public class SimpleJedisTemplate extends JedisTemplate{ 8 | 9 | private JedisPool jedisPool; 10 | private JedisPoolConfig jedisPoolConfig; 11 | private String host; 12 | private int port = 6379;//default redis server port 13 | 14 | public SimpleJedisTemplate(String host,int port){ 15 | this.host = host; 16 | this.port = port; 17 | this.init(); 18 | } 19 | 20 | public SimpleJedisTemplate(String host){ 21 | this.host = host; 22 | this.init(); 23 | } 24 | 25 | public SimpleJedisTemplate(JedisPoolConfig jedisPoolConfig,String host,int port){ 26 | this.host = host; 27 | this.port = port; 28 | this.jedisPoolConfig = jedisPoolConfig; 29 | this.init(); 30 | } 31 | 32 | private void init(){ 33 | if(jedisPoolConfig==null){ 34 | jedisPool = new JedisPool(host,port); 35 | }else{ 36 | jedisPool = new JedisPool(jedisPoolConfig,host,port); 37 | } 38 | } 39 | 40 | @Override 41 | protected Jedis getResource(String key) { 42 | return jedisPool.getResource(); 43 | } 44 | 45 | @Override 46 | protected void returnResource(Jedis jedis) { 47 | jedisPool.returnResource(jedis); 48 | } 49 | 50 | @Override 51 | protected void returnBrokenResource(Jedis jedis) { 52 | jedisPool.returnBrokenResource(jedis); 53 | } 54 | 55 | @Override 56 | protected Jedis getResource(byte[] key) { 57 | return jedisPool.getResource(); 58 | } 59 | 60 | @Override 61 | public void close() { 62 | jedisPool.destroy(); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/start/RedisNodeMonitorBootStrap.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.start; 2 | 3 | import org.apache.log4j.Logger; 4 | 5 | import com.linda.cluster.redis.monitor.RedisNodeMonitor; 6 | 7 | public class RedisNodeMonitorBootStrap extends AbstractMonitorBootStrap{ 8 | 9 | private static Logger logger = Logger.getLogger(RedisNodeMonitorBootStrap.class); 10 | 11 | public RedisNodeMonitor newNodeMonitor(long productId,long clusterId,long nodeId){ 12 | RedisNodeMonitor monitor = new RedisNodeMonitor(productId,clusterId,nodeId); 13 | monitor.setRedisClusterAdminService(redisClusterAdminService); 14 | monitor.setRedisInfoDataService(redisInfoDataService); 15 | return monitor; 16 | } 17 | 18 | public static void main(String[] args) { 19 | long productId = -1; 20 | long clusterId = -1; 21 | long nodeId = -1; 22 | if(args!=null){ 23 | for(String arg:args){ 24 | if(arg.startsWith("-p")){ 25 | productId = parseLong(getValue(arg,"-p"),productId); 26 | }else if(arg.startsWith("-c")){ 27 | clusterId = parseLong(getValue(arg,"-c"),clusterId); 28 | }else if(arg.startsWith("-n")){ 29 | nodeId = parseLong(getValue(arg,"-n"),nodeId); 30 | } 31 | } 32 | } 33 | if(productId<0||clusterId<0||nodeId<0){ 34 | throw new IllegalArgumentException("invalid product cluster node parameter"); 35 | } 36 | RedisNodeMonitorBootStrap strap = new RedisNodeMonitorBootStrap(); 37 | strap.startup(); 38 | logger.info("resource inject success"); 39 | RedisNodeMonitor monitor = strap.newNodeMonitor(productId, clusterId, nodeId); 40 | monitor.startup(); 41 | logger.info("monitor startup productId:"+productId+" clusterId:"+clusterId+" nodeId:"+nodeId); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/bean/ClusterStateBean.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.bean; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | 8 | import org.apache.zookeeper.data.Stat; 9 | import org.codehaus.jackson.annotate.JsonIgnore; 10 | import org.omg.PortableServer.POAManagerPackage.State; 11 | 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class ClusterStateBean { 15 | 16 | //集群是否存活 17 | @Getter 18 | @Setter 19 | private boolean alive; 20 | @Getter 21 | @Setter 22 | private String master; 23 | @Getter 24 | @Setter 25 | @JsonIgnore 26 | private Stat stat; 27 | 28 | @Override 29 | public int hashCode() { 30 | final int prime = 31; 31 | int result = 1; 32 | result = prime * result + (alive ? 1231 : 1237); 33 | result = prime * result + ((master == null) ? 0 : master.hashCode()); 34 | return result; 35 | } 36 | @Override 37 | public boolean equals(Object obj) { 38 | if (this == obj) 39 | return true; 40 | if (obj == null) 41 | return false; 42 | if (getClass() != obj.getClass()) 43 | return false; 44 | ClusterStateBean other = (ClusterStateBean) obj; 45 | if (alive != other.alive) 46 | return false; 47 | if (master == null) { 48 | if (other.master != null) 49 | return false; 50 | } else if (!master.equals(other.master)) 51 | return false; 52 | return true; 53 | } 54 | 55 | public static ClusterStateBean copyState(ClusterStateBean bean) { 56 | if(bean!=null){ 57 | ClusterStateBean stateBean = new ClusterStateBean(); 58 | stateBean.alive = bean.alive; 59 | stateBean.master = bean.master; 60 | stateBean.stat = bean.stat; 61 | return stateBean; 62 | } 63 | return null; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/push/JedisClusterService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.push; 2 | 3 | import java.lang.reflect.Field; 4 | import java.util.Collections; 5 | import java.util.List; 6 | import java.util.Set; 7 | 8 | import redis.clients.jedis.HostAndPort; 9 | import redis.clients.jedis.JedisCluster; 10 | import redis.clients.jedis.JedisClusterConnectionHandler; 11 | import redis.clients.jedis.JedisPoolConfig; 12 | 13 | import com.linda.cluster.redis.common.bean.RedisZookeeperServiceConfig; 14 | import com.linda.cluster.redis.common.bean.ShardingBean; 15 | import com.linda.cluster.redis.common.spring.BeanInit; 16 | 17 | 18 | /** 19 | * jedis cluster client for push platform redis cluster 20 | * @author hzlindzh 21 | * 22 | */ 23 | public class JedisClusterService extends JedisCluster implements BeanInit{ 24 | 25 | private JedisClusterSlotConnectionHandler myClusterConnectionHandler; 26 | 27 | public JedisClusterService(JedisPoolConfig redisPoolConfig,RedisZookeeperServiceConfig config,List buckets){ 28 | super(Collections.EMPTY_SET); 29 | myClusterConnectionHandler = new JedisClusterSlotConnectionHandler(redisPoolConfig,config,buckets); 30 | this.setSuperConnectionHandler(myClusterConnectionHandler); 31 | } 32 | 33 | private void setSuperConnectionHandler(JedisClusterConnectionHandler handler){ 34 | try { 35 | Field connectionHandlerField = JedisCluster.class.getDeclaredField("connectionHandler"); 36 | connectionHandlerField.setAccessible(true); 37 | connectionHandlerField.set(this, handler); 38 | } catch (Exception e) { 39 | e.printStackTrace(); 40 | } 41 | } 42 | 43 | private JedisClusterService(Set nodes) { 44 | super(nodes); 45 | } 46 | 47 | @Override 48 | public void init() { 49 | 50 | } 51 | 52 | @Override 53 | public void destroy() { 54 | myClusterConnectionHandler.destroy(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/RedisMonitorBean.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import lombok.Getter; 7 | 8 | import com.linda.cluster.redis.common.bean.KeyValueBean; 9 | 10 | public class RedisMonitorBean { 11 | 12 | @Getter 13 | private List serverProperties = new ArrayList(); 14 | @Getter 15 | private List clientProperties = new ArrayList(); 16 | @Getter 17 | private List memoryProperties = new ArrayList(); 18 | @Getter 19 | private List persistentProperties = new ArrayList(); 20 | @Getter 21 | private List statProperties = new ArrayList(); 22 | @Getter 23 | private List replicationProperties = new ArrayList(); 24 | @Getter 25 | private List cpuProperties = new ArrayList(); 26 | @Getter 27 | private List keyspaceProperties = new ArrayList(); 28 | 29 | public void addServerKeyValue(KeyValueBean bean){ 30 | serverProperties.add(bean); 31 | } 32 | 33 | public void addClientKeyValue(KeyValueBean bean){ 34 | clientProperties.add(bean); 35 | } 36 | 37 | public void addMemoryKeyValue(KeyValueBean bean){ 38 | memoryProperties.add(bean); 39 | } 40 | 41 | public void addPersistentKeyValue(KeyValueBean bean){ 42 | persistentProperties.add(bean); 43 | } 44 | 45 | public void addStatKeyValue(KeyValueBean bean){ 46 | statProperties.add(bean); 47 | } 48 | 49 | public void addReplicationKeyValue(KeyValueBean bean){ 50 | replicationProperties.add(bean); 51 | } 52 | 53 | public void addCpuKeyValue(KeyValueBean bean){ 54 | cpuProperties.add(bean); 55 | } 56 | 57 | public void addKeyspaceKeyValue(KeyValueBean bean){ 58 | keyspaceProperties.add(bean); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /redis-cluster-scale/src/test/java/com/linda/cluster/redis/scale/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.scale; 2 | 3 | import java.io.Serializable; 4 | 5 | import lombok.Data; 6 | import redis.clients.jedis.Jedis; 7 | import redis.clients.jedis.JedisPool; 8 | 9 | import com.linda.cluster.redis.client.serializer.SimpleJdkSerializer; 10 | 11 | @Data 12 | public class AppTest implements Serializable{ 13 | 14 | private static final long serialVersionUID = -4903359646373299487L; 15 | private String name; 16 | 17 | public AppTest(String name){ 18 | this.name = name; 19 | } 20 | 21 | public static void main(String[] args) throws InterruptedException { 22 | SimpleJdkSerializer serializer = new SimpleJdkSerializer(); 23 | String host = "192.168.139.129"; 24 | int port = 7770; 25 | JedisPool pool = new JedisPool(host,port); 26 | Jedis jedis = pool.getResource(); 27 | String byteKey = "byte-"; 28 | String keyPrefix = "key-"; 29 | String listPrefix = "list-"; 30 | String setPrefix = "set-"; 31 | int index = 16000; 32 | int end = 16100; 33 | while(index 2 | 5 | 6 | 7 | 8 | insert into redis_monitor_keyspace(productId,clusterId,redisNodeId,addtime,databaseId,keynum,expires,avg_ttl) 9 | values(#{productId},#{clusterId},#{redisNodeId},#{addtime},#{databaseId},#{keynum},#{expires},#{avg_ttl}) 10 | 11 | 12 | 15 | 16 | 19 | 20 | 23 | 24 | 27 | 28 | 31 | 32 | 35 | 36 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/MonitorCpuDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_monitor_cpu(productId,clusterId,redisNodeId,addtime,used_cpu_sys,used_cpu_user,used_cpu_sys_children,used_cpu_user_children) 9 | values(#{productId},#{clusterId},#{redisNodeId},#{addtime},#{used_cpu_sys},#{used_cpu_user},#{used_cpu_sys_children},#{used_cpu_user_children}) 10 | 11 | 12 | 15 | 16 | 19 | 20 | 23 | 24 | 27 | 28 | 31 | 32 | 35 | 36 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/MonitorClientsDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_monitor_clients(productId,clusterId,redisNodeId,addtime,connected_clients,client_longest_output_list,client_biggest_input_buf,blocked_clients) 9 | values(#{productId},#{clusterId},#{redisNodeId},#{addtime},#{connected_clients},#{client_longest_output_list},#{client_biggest_input_buf},#{blocked_clients}) 10 | 11 | 12 | 15 | 16 | 19 | 20 | 23 | 24 | 27 | 28 | 31 | 32 | 35 | 36 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/MonitorMemoryDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_monitor_memory(productId,clusterId,redisNodeId,addtime,used_memory,used_memory_rss,used_memory_peak,used_memory_lua,mem_fragmentation_ratio,mem_allocator) 9 | values(#{productId},#{clusterId},#{redisNodeId},#{addtime},#{used_memory},#{used_memory_rss},#{used_memory_peak},#{used_memory_lua},#{mem_fragmentation_ratio},#{mem_allocator}) 10 | 11 | 12 | 15 | 16 | 19 | 20 | 23 | 24 | 27 | 28 | 31 | 32 | 35 | 36 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/bean/RedisZkData.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.bean; 2 | 3 | import lombok.Data; 4 | 5 | import org.apache.zookeeper.data.Stat; 6 | 7 | @Data 8 | public class RedisZkData { 9 | 10 | private Stat stat = new Stat(); 11 | private byte[] data; 12 | 13 | public long getCzxid() { 14 | return stat.getCzxid(); 15 | } 16 | 17 | public void setCzxid(long m_) { 18 | stat.setCzxid(m_); 19 | } 20 | 21 | public long getMzxid() { 22 | return stat.getMzxid(); 23 | } 24 | 25 | public void setMzxid(long m_) { 26 | stat.setMzxid(m_); 27 | } 28 | 29 | public long getCtime() { 30 | return stat.getCtime(); 31 | } 32 | 33 | public void setCtime(long m_) { 34 | stat.setCtime(m_); 35 | } 36 | 37 | public long getMtime() { 38 | return stat.getMtime(); 39 | } 40 | 41 | public void setMtime(long m_) { 42 | stat.setMtime(m_); 43 | } 44 | 45 | public int getVersion() { 46 | return stat.getVersion(); 47 | } 48 | 49 | public void setVersion(int m_) { 50 | stat.setVersion(m_); 51 | } 52 | 53 | public int getCversion() { 54 | return stat.getCversion(); 55 | } 56 | 57 | public void setCversion(int m_) { 58 | stat.setCversion(m_); 59 | } 60 | 61 | public int getAversion() { 62 | return stat.getAversion(); 63 | } 64 | 65 | public void setAversion(int m_) { 66 | stat.setAversion(m_); 67 | } 68 | 69 | public long getEphemeralOwner() { 70 | return stat.getEphemeralOwner(); 71 | } 72 | 73 | public void setEphemeralOwner(long m_) { 74 | stat.setEphemeralOwner(m_); 75 | } 76 | 77 | public int getDataLength() { 78 | return stat.getDataLength(); 79 | } 80 | 81 | public void setDataLength(int m_) { 82 | stat.setDataLength(m_); 83 | } 84 | 85 | public int getNumChildren() { 86 | return stat.getNumChildren(); 87 | } 88 | 89 | public void setNumChildren(int m_) { 90 | stat.setNumChildren(m_); 91 | } 92 | 93 | public long getPzxid() { 94 | return stat.getPzxid(); 95 | } 96 | 97 | public void setPzxid(long m_) { 98 | stat.setPzxid(m_); 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/test/java/com/linda/cluster/redis/monitor/RedisAdminServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor; 2 | 3 | import java.util.List; 4 | 5 | import javax.annotation.Resource; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import com.linda.cluster.redis.common.Service; 11 | import com.linda.cluster.redis.common.utils.JSONUtils; 12 | import com.linda.cluster.redis.monitor.pojo.Cluster; 13 | import com.linda.cluster.redis.monitor.pojo.Product; 14 | import com.linda.cluster.redis.monitor.pojo.RedisNode; 15 | import com.linda.cluster.redis.monitor.service.RedisClusterAdminService; 16 | import com.linda.cluster.redis.monitor.start.AbstractSpringBootStrap; 17 | 18 | public class RedisAdminServiceTest extends AbstractSpringBootStrap implements Service{ 19 | 20 | @Resource 21 | private RedisClusterAdminService redisClusterAdminService; 22 | @Resource 23 | private RedisMonitor redisMonitor; 24 | 25 | @Before 26 | public void startup() { 27 | super.startup(); 28 | } 29 | 30 | public void shutdown() { 31 | super.shutdown(); 32 | } 33 | 34 | @Test 35 | public void addProductNode() throws InterruptedException{ 36 | long productId = 4; 37 | long clusterId = 4; 38 | String host = "10.120.151.105"; 39 | int port = 12303; 40 | 41 | // Product product = new Product(); 42 | // product.setName("lindzh"); 43 | // Product addp = redisClusterAdminService.addProduct(product); 44 | // productId = addp.getId(); 45 | // 46 | // Cluster cluster = new Cluster(); 47 | // cluster.setClusterName("lindzh-cluster1"); 48 | // cluster.setProductId(productId); 49 | // Cluster addc = redisClusterAdminService.addCluster(cluster); 50 | // clusterId = addc.getId(); 51 | 52 | RedisNode node = new RedisNode(); 53 | node.setProductId(productId); 54 | node.setClusterId(clusterId); 55 | node.setHost(host); 56 | node.setPort(port); 57 | node.setName("node-12301"); 58 | RedisNode addn = redisClusterAdminService.addRedisNode(node); 59 | 60 | List list = redisClusterAdminService.getAllProducts(true); 61 | System.out.println(JSONUtils.toJson(list)); 62 | // redisMonitor.startMonitor(); 63 | // Thread.currentThread().sleep(1000000L); 64 | } 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /redis-cluster-aof/src/main/resources/redis-cmd.txt: -------------------------------------------------------------------------------- 1 | append, 2 | auth, 3 | bgrewriteaof, 4 | bgsave, 5 | bitcount, 6 | bitop, 7 | blpop, 8 | brpop, 9 | brpoplpush, 10 | client-kill, 11 | client-list, 12 | client-getname, 13 | client-setname, 14 | config-get, 15 | config-set, 16 | config-resetstat, 17 | dbsize, 18 | debug-object, 19 | debug-segfault, 20 | decr, 21 | decrby, 22 | del, 23 | discard, 24 | dump, 25 | echo, 26 | eval, 27 | evalsha, 28 | exec, 29 | exists, 30 | expire, 31 | expireat, 32 | flushall, 33 | flushdb, 34 | get, 35 | getbit, 36 | getrange, 37 | getset, 38 | hdel, 39 | hexists, 40 | hget, 41 | hgetall, 42 | hincrby, 43 | hincrbyfloat, 44 | hkeys, 45 | hlen, 46 | hmget, 47 | hmset, 48 | hset, 49 | hsetnx, 50 | hvals, 51 | incr, 52 | incrby, 53 | incrbyfloat, 54 | info, 55 | keys, 56 | lastsave, 57 | lindex, 58 | linsert, 59 | llen, 60 | lpop, 61 | lpush, 62 | lpushx, 63 | lrange, 64 | lrem, 65 | lset, 66 | ltrim, 67 | mget, 68 | migrate, 69 | monitor, 70 | move, 71 | mset, 72 | msetnx, 73 | multi, 74 | object, 75 | persist, 76 | pexpire, 77 | pexpireat, 78 | ping, 79 | psetex, 80 | psubscribe, 81 | pttl, 82 | publish, 83 | punsubscribe, 84 | quit, 85 | randomkey, 86 | rename, 87 | renamenx, 88 | restore, 89 | rpop, 90 | rpoplpush, 91 | rpush, 92 | rpushx, 93 | sadd, 94 | save, 95 | scard, 96 | script-exists, 97 | script-flush, 98 | script-kill, 99 | script-load, 100 | sdiff, 101 | sdiffstore, 102 | select, 103 | set, 104 | setbit, 105 | setex, 106 | setnx, 107 | setrange, 108 | shutdown, 109 | sinter, 110 | sinterstore, 111 | sismember, 112 | slaveof, 113 | slowlog, 114 | smembers, 115 | smove, 116 | sort, 117 | spop, 118 | srandmember, 119 | srem, 120 | strlen, 121 | subscribe, 122 | sunion, 123 | sunionstore, 124 | sync, 125 | time, 126 | ttl, 127 | type, 128 | unsubscribe, 129 | unwatch, 130 | watch, 131 | zadd, 132 | zcard, 133 | zcount, 134 | zincrby, 135 | zinterstore, 136 | zrange, 137 | zrangebyscore, 138 | zrank, 139 | zrem, 140 | zremrangebyrank, 141 | zremrangebyscore, 142 | zrevrange, 143 | zrevrangebyscore, 144 | zrevrank, 145 | zscore, 146 | zunionstore, 147 | pubsub, 148 | config-rewrite, 149 | client-pause, 150 | hscan, 151 | scan, 152 | sscan, 153 | zscan -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/redis/MultiRedisAlivedPingService.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.redis; 2 | 3 | import java.util.concurrent.CopyOnWriteArrayList; 4 | import java.util.concurrent.atomic.AtomicBoolean; 5 | 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | 9 | import org.apache.log4j.Logger; 10 | 11 | import com.linda.cluster.redis.common.Service; 12 | import com.linda.cluster.redis.common.bean.HostAndPort; 13 | 14 | public class MultiRedisAlivedPingService implements Service,Runnable{ 15 | 16 | @Getter 17 | @Setter 18 | private int interval = 3000; 19 | private Thread thread; 20 | 21 | private AtomicBoolean stop = new AtomicBoolean(false); 22 | 23 | private CopyOnWriteArrayList redisNodes = new CopyOnWriteArrayList(); 24 | 25 | private Logger logger = Logger.getLogger(MultiRedisAlivedPingService.class); 26 | 27 | public void addRedisNode(SimpleRedisAliveNode node){ 28 | this.init(node); 29 | redisNodes.add(node); 30 | } 31 | 32 | public SimpleRedisAliveNode getByHostAndPort(HostAndPort hostAndPort){ 33 | for(SimpleRedisAliveNode redisNode:redisNodes){ 34 | if(redisNode.getRedisHost()==hostAndPort){ 35 | return redisNode; 36 | } 37 | } 38 | return null; 39 | } 40 | 41 | public void remodeNode(SimpleRedisAliveNode node){ 42 | redisNodes.remove(node); 43 | } 44 | 45 | @Override 46 | public void startup() { 47 | thread = new Thread(this); 48 | thread.start(); 49 | } 50 | 51 | private void init(RedisAliveBase node){ 52 | node.connect(); 53 | } 54 | 55 | @Override 56 | public void shutdown() { 57 | stop.set(true); 58 | thread.interrupt(); 59 | for(RedisAliveBase node:redisNodes){ 60 | node.close(); 61 | } 62 | } 63 | 64 | @Override 65 | public void run() { 66 | logger.info("redis ping thread start"); 67 | while(!stop.get()){ 68 | for(RedisAliveBase node:redisNodes){ 69 | try{ 70 | node.ping(); 71 | }catch(Exception e){ 72 | logger.error(e.getClass()+" "+e.getMessage()+" node:"+node.getRedisHost().getHost()+":"+node.getRedisHost().getPort()); 73 | } 74 | } 75 | try { 76 | Thread.currentThread().sleep(interval); 77 | } catch (InterruptedException e) { 78 | break; 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/IOUtils.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.FileNotFoundException; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.io.InputStreamReader; 10 | import java.util.Properties; 11 | 12 | import org.apache.log4j.Logger; 13 | 14 | public class IOUtils { 15 | 16 | private static Logger logger = Logger.getLogger(IOUtils.class); 17 | public static final String CLASS_PATH_PREFIX = "classpath:"; 18 | public static final String FILE_PATH_PREFIX = "file:"; 19 | 20 | public static InputStream getClassPathInputStream(String name){ 21 | if(name.startsWith(CLASS_PATH_PREFIX)){ 22 | name = name.substring(CLASS_PATH_PREFIX.length()); 23 | } 24 | return IOUtils.class.getClassLoader().getResourceAsStream(name); 25 | } 26 | 27 | public static InputStream getFileInputStream(String filename){ 28 | if(filename.startsWith(FILE_PATH_PREFIX)){ 29 | filename = filename.substring(FILE_PATH_PREFIX.length()); 30 | } 31 | File file = new File(filename); 32 | try { 33 | return new FileInputStream(file); 34 | } catch (FileNotFoundException e) { 35 | logger.error("FileNotFoundException "+e.getMessage()); 36 | } 37 | return null; 38 | } 39 | 40 | public static Properties loadProperties(InputStream ins){ 41 | Properties properties = new Properties(); 42 | try { 43 | properties.load(ins); 44 | } catch (IOException e) { 45 | logger.error("load properties io error"); 46 | } 47 | return properties; 48 | } 49 | 50 | public static void closeInputStream(InputStream ins){ 51 | try { 52 | ins.close(); 53 | } catch (IOException e) { 54 | logger.error("close inputstream io error"); 55 | } 56 | } 57 | 58 | public static String toString(InputStream ins){ 59 | StringBuilder builder = new StringBuilder(); 60 | BufferedReader reader = new BufferedReader(new InputStreamReader(ins)); 61 | try{ 62 | String line = reader.readLine(); 63 | while(line!=null){ 64 | builder.append(line); 65 | line = reader.readLine(); 66 | } 67 | }catch(Exception e){ 68 | logger.error("load inputStream read error"); 69 | } 70 | return builder.toString(); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/MonitorReplicationDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_monitor_replication(productId,clusterId,redisNodeId,addtime,role,connected_slaves,slaveJson,master_repl_offset,repl_backlog_active,repl_backlog_size, 9 | repl_backlog_first_byte_offset,repl_backlog_histlen) 10 | values(#{productId},#{clusterId},#{redisNodeId},#{addtime},#{role},#{connected_slaves},#{slaveJson},#{master_repl_offset},#{repl_backlog_active},#{repl_backlog_size}, 11 | #{repl_backlog_first_byte_offset},#{repl_backlog_histlen}) 12 | 13 | 14 | 17 | 18 | 21 | 22 | 25 | 26 | 29 | 30 | 33 | 34 | 37 | 38 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/resources/monitor-start.bat: -------------------------------------------------------------------------------- 1 | set BASEDIR=D:\work\workspace\redis-cluster\redis-cluster-monitor\src\main\webapp\monitor 2 | 3 | set CLASSPATH=.;%BASEDIR%\lib\activation-1.1.jar;%BASEDIR%\lib\aopalliance-1.0.jar;%BASEDIR%\lib\aspectjrt-1.7.1.jar;%BASEDIR%\lib\aspectjweaver-1.7.1.jar;%BASEDIR%\lib\bcprov-jdk15on-1.48.jar;%BASEDIR%\lib\c3p0-0.9.2.1.jar;%BASEDIR%\lib\commons-beanutils-1.8.3.jar;%BASEDIR%\lib\commons-codec-1.8.jar;%BASEDIR%\lib\commons-collections-3.2.1.jar;%BASEDIR%\lib\commons-dbcp-1.4.jar;%BASEDIR%\lib\commons-digester-1.8.jar;%BASEDIR%\lib\commons-email-1.2.jar;%BASEDIR%\lib\commons-fileupload-1.3.1.jar;%BASEDIR%\lib\commons-io-2.4.jar;%BASEDIR%\lib\commons-lang-2.6.jar;%BASEDIR%\lib\commons-lang3-3.1.jar;%BASEDIR%\lib\commons-logging-1.1.1.jar;%BASEDIR%\lib\commons-pool-1.5.4.jar;%BASEDIR%\lib\commons-pool2-2.0.jar;%BASEDIR%\lib\commons-validator-1.4.0.jar;%BASEDIR%\lib\fastjson-1.1.32.jar;%BASEDIR%\lib\freemarker-2.3.19.jar;%BASEDIR%\lib\guava-15.0.jar;%BASEDIR%\lib\jackson-annotations-2.2.0.jar;%BASEDIR%\lib\jackson-core-2.2.0.jar;%BASEDIR%\lib\jackson-core-asl-1.9.13.jar;%BASEDIR%\lib\jackson-databind-2.2.0.jar;%BASEDIR%\lib\jackson-mapper-asl-1.9.13.jar;%BASEDIR%\lib\jcl-over-slf4j-1.6.2.jar;%BASEDIR%\lib\jedis-2.4.2.jar;%BASEDIR%\lib\jline-0.9.94.jar;%BASEDIR%\lib\log4j-1.2.16.jar;%BASEDIR%\lib\lombok-0.10.8.jar;%BASEDIR%\lib\mail-1.4.1.jar;%BASEDIR%\lib\mchange-commons-java-0.2.3.4.jar;%BASEDIR%\lib\mybatis-3.2.7.jar;%BASEDIR%\lib\mybatis-spring-1.1.1.jar;%BASEDIR%\lib\mysql-connector-java-5.1.21.jar;%BASEDIR%\lib\netty-3.2.2.Final.jar;%BASEDIR%\lib\quartz-1.8.6.jar;%BASEDIR%\lib\redis-cluster-common-0.0.1.jar;%BASEDIR%\lib\slf4j-api-1.6.2.jar;%BASEDIR%\lib\slf4j-log4j12-1.6.2.jar;%BASEDIR%\lib\spring-aop-3.2.2.RELEASE.jar;%BASEDIR%\lib\spring-beans-3.2.2.RELEASE.jar;%BASEDIR%\lib\spring-context-3.2.2.RELEASE.jar;%BASEDIR%\lib\spring-context-support-3.2.2.RELEASE.jar;%BASEDIR%\lib\spring-core-3.2.2.RELEASE.jar;%BASEDIR%\lib\spring-expression-3.2.2.RELEASE.jar;%BASEDIR%\lib\spring-jdbc-3.1.1.RELEASE.jar;%BASEDIR%\lib\spring-jms-3.2.2.RELEASE.jar;%BASEDIR%\lib\spring-oxm-3.2.2.RELEASE.jar;%BASEDIR%\lib\spring-tx-3.1.1.RELEASE.jar;%BASEDIR%\lib\spring-web-3.2.2.RELEASE.jar;%BASEDIR%\lib\spring-webmvc-3.2.2.RELEASE.jar;%BASEDIR%\lib\xpp3_min-1.1.4c.jar;%BASEDIR%\lib\xstream-1.3.1.jar;%BASEDIR%\lib\zookeeper-3.4.5.jar 4 | 5 | java com.linda.cluster.redis.monitor.start.RedisNodeMonitorBootStrap -p1 -c1 -n1 6 | 7 | pause -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/resources/sqlmap-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/resources/monitor-start.sh: -------------------------------------------------------------------------------- 1 | path=$(dirname $0) 2 | 3 | BASEDIR="$path/../lib" 4 | 5 | export CLASSPATH=.:"$BASEDIR"/lib/activation-1.1.jar:"$BASEDIR"/lib/aopalliance-1.0.jar:"$BASEDIR"/lib/aspectjrt-1.7.1.jar:"$BASEDIR"/lib/aspectjweaver-1.7.1.jar:"$BASEDIR"/lib/bcprov-jdk15on-1.48.jar:"$BASEDIR"/lib/c3p0-0.9.2.1.jar:"$BASEDIR"/lib/commons-beanutils-1.8.3.jar:"$BASEDIR"/lib/commons-codec-1.8.jar:"$BASEDIR"/lib/commons-collections-3.2.1.jar:"$BASEDIR"/lib/commons-dbcp-1.4.jar:"$BASEDIR"/lib/commons-digester-1.8.jar:"$BASEDIR"/lib/commons-email-1.2.jar:"$BASEDIR"/lib/commons-fileupload-1.3.1.jar:"$BASEDIR"/lib/commons-io-2.4.jar:"$BASEDIR"/lib/commons-lang-2.6.jar:"$BASEDIR"/lib/commons-lang3-3.1.jar:"$BASEDIR"/lib/commons-logging-1.1.1.jar:"$BASEDIR"/lib/commons-pool-1.5.4.jar:"$BASEDIR"/lib/commons-pool2-2.0.jar:"$BASEDIR"/lib/commons-validator-1.4.0.jar:"$BASEDIR"/lib/fastjson-1.1.32.jar:"$BASEDIR"/lib/freemarker-2.3.19.jar:"$BASEDIR"/lib/guava-15.0.jar:"$BASEDIR"/lib/jackson-annotations-2.2.0.jar:"$BASEDIR"/lib/jackson-core-2.2.0.jar:"$BASEDIR"/lib/jackson-core-asl-1.9.13.jar:"$BASEDIR"/lib/jackson-databind-2.2.0.jar:"$BASEDIR"/lib/jackson-mapper-asl-1.9.13.jar:"$BASEDIR"/lib/jcl-over-slf4j-1.6.2.jar:"$BASEDIR"/lib/jedis-2.4.2.jar:"$BASEDIR"/lib/jline-0.9.94.jar:"$BASEDIR"/lib/log4j-1.2.16.jar:"$BASEDIR"/lib/lombok-0.10.8.jar:"$BASEDIR"/lib/mail-1.4.1.jar:"$BASEDIR"/lib/mchange-commons-java-0.2.3.4.jar:"$BASEDIR"/lib/mybatis-3.2.7.jar:"$BASEDIR"/lib/mybatis-spring-1.1.1.jar:"$BASEDIR"/lib/mysql-connector-java-5.1.21.jar:"$BASEDIR"/lib/netty-3.2.2.Final.jar:"$BASEDIR"/lib/quartz-1.8.6.jar:"$BASEDIR"/lib/redis-cluster-common-0.0.1.jar:"$BASEDIR"/lib/slf4j-api-1.6.2.jar:"$BASEDIR"/lib/slf4j-log4j12-1.6.2.jar:"$BASEDIR"/lib/spring-aop-3.2.2.RELEASE.jar:"$BASEDIR"/lib/spring-beans-3.2.2.RELEASE.jar:"$BASEDIR"/lib/spring-context-3.2.2.RELEASE.jar:"$BASEDIR"/lib/spring-context-support-3.2.2.RELEASE.jar:"$BASEDIR"/lib/spring-core-3.2.2.RELEASE.jar:"$BASEDIR"/lib/spring-expression-3.2.2.RELEASE.jar:"$BASEDIR"/lib/spring-jdbc-3.1.1.RELEASE.jar:"$BASEDIR"/lib/spring-jms-3.2.2.RELEASE.jar:"$BASEDIR"/lib/spring-oxm-3.2.2.RELEASE.jar:"$BASEDIR"/lib/spring-tx-3.1.1.RELEASE.jar:"$BASEDIR"/lib/spring-web-3.2.2.RELEASE.jar:"$BASEDIR"/lib/spring-webmvc-3.2.2.RELEASE.jar:"$BASEDIR"/lib/xpp3_min-1.1.4c.jar:"$BASEDIR"/lib/xstream-1.3.1.jar:"$BASEDIR"/lib/zookeeper-3.4.5.jar 6 | 7 | java com.linda.cluster.redis.monitor.start.RedisNodeMonitorBootStrap -p4 -c4 -n4 -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/MonitorStatDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_monitor_stat(productId,clusterId,redisNodeId,addtime,total_connections_received,total_commands_processed,instantaneous_ops_per_sec,rejected_connections, 9 | sync_full,sync_partial_ok,sync_partial_err,expired_keys,evicted_keys,keyspace_hits,keyspace_misses,pubsub_channels,pubsub_patterns,latest_fork_usec) 10 | values(#{productId},#{clusterId},#{redisNodeId},#{addtime},#{total_connections_received},#{total_commands_processed},#{instantaneous_ops_per_sec},#{rejected_connections}, 11 | #{sync_full},#{sync_partial_ok},#{sync_partial_err},#{expired_keys},#{evicted_keys},#{keyspace_hits},#{keyspace_misses},#{pubsub_channels},#{pubsub_patterns},#{latest_fork_usec}) 12 | 13 | 14 | 17 | 18 | 21 | 22 | 25 | 26 | 29 | 30 | 33 | 34 | 37 | 38 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/start/AbstractSpringBootStrap.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.monitor.start; 2 | 3 | import java.lang.reflect.Field; 4 | import java.util.ArrayList; 5 | import java.util.Collections; 6 | import java.util.List; 7 | 8 | import javax.annotation.Resource; 9 | 10 | import org.springframework.beans.BeansException; 11 | import org.springframework.context.ApplicationContext; 12 | import org.springframework.context.support.ClassPathXmlApplicationContext; 13 | 14 | import com.linda.cluster.redis.common.Service; 15 | 16 | public class AbstractSpringBootStrap implements Service{ 17 | 18 | private ApplicationContext apc; 19 | 20 | private List configLocations; 21 | 22 | private void loadConfigLocations(){ 23 | configLocations = new ArrayList(); 24 | configLocations.add("classpath:mybatis-config.xml"); 25 | configLocations.add("classpath:spring-context-config.xml"); 26 | } 27 | 28 | private void setIoc(){ 29 | List fields = getResourceFields(this.getClass()); 30 | for(Field field:fields){ 31 | Resource resource = field.getAnnotation(Resource.class); 32 | if(resource!=null){ 33 | try { 34 | field.setAccessible(true); 35 | field.set(this,apc.getBean(field.getType())); 36 | } catch (BeansException e) { 37 | e.printStackTrace(); 38 | } catch (IllegalArgumentException e) { 39 | e.printStackTrace(); 40 | } catch (IllegalAccessException e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | } 45 | } 46 | 47 | private List getResourceFields(Class clazz){ 48 | ArrayList list = new ArrayList(); 49 | if(clazz!=AbstractSpringBootStrap.class&&clazz!=Object.class){ 50 | Field[] fields = clazz.getDeclaredFields(); 51 | for(Field field:fields){ 52 | Resource resource = field.getAnnotation(Resource.class); 53 | if(resource!=null){ 54 | list.add(field); 55 | } 56 | } 57 | }else{ 58 | return Collections.EMPTY_LIST; 59 | } 60 | list.addAll(getResourceFields(clazz.getSuperclass())); 61 | return list; 62 | } 63 | 64 | public void initSuper(){ 65 | loadConfigLocations(); 66 | apc = new ClassPathXmlApplicationContext(configLocations.toArray(new String[0])); 67 | setIoc(); 68 | } 69 | 70 | 71 | public T getBean(String name,Class type){ 72 | return apc.getBean(name, type); 73 | } 74 | 75 | @Override 76 | public void startup() { 77 | this.initSuper(); 78 | } 79 | 80 | @Override 81 | public void shutdown() { 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /redis-conf/redis-cluster/slave7772/redislog.log: -------------------------------------------------------------------------------- 1 | [4233] 18 Nov 23:33:41.018 # You requested maxclients of 10000 requiring at least 10032 max file descriptors. 2 | [4233] 18 Nov 23:33:41.019 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted. 3 | [4233] 18 Nov 23:33:41.019 # Current maximum open files is 1024. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'. 4 | [4233] 18 Nov 23:33:41.019 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now. 5 | _._ 6 | _.-``__ ''-._ 7 | _.-`` `. `_. ''-._ Redis 2.8.8 (00000000/0) 32 bit 8 | .-`` .-```. ```\/ _.,_ ''-._ 9 | ( ' , .-` | `, ) Running in stand alone mode 10 | |`-._`-...-` __...-.``-._|'` _.-'| Port: 7772 11 | | `-._ `._ / _.-' | PID: 4233 12 | `-._ `-._ `-./ _.-' _.-' 13 | |`-._`-._ `-.__.-' _.-'_.-'| 14 | | `-._`-._ _.-'_.-' | http://redis.io 15 | `-._ `-._`-.__.-'_.-' _.-' 16 | |`-._`-._ `-.__.-' _.-'_.-'| 17 | | `-._`-._ _.-'_.-' | 18 | `-._ `-._`-.__.-'_.-' _.-' 19 | `-._ `-.__.-' _.-' 20 | `-._ _.-' 21 | `-.__.-' 22 | 23 | [4233] 18 Nov 23:33:41.019 # Server started, Redis version 2.8.8 24 | [4233] 18 Nov 23:33:41.019 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 25 | [4233] 18 Nov 23:33:41.019 * The server is now ready to accept connections on port 7772 26 | [4233] 18 Nov 23:34:13.679 # User requested shutdown... 27 | [4233] 18 Nov 23:34:13.680 * Saving the final RDB snapshot before exiting. 28 | [4233] 18 Nov 23:34:13.729 * DB saved on disk 29 | [4233] 18 Nov 23:34:13.729 * Removing the pid file. 30 | [4233] 18 Nov 23:34:13.729 # Redis is now ready to exit, bye bye... 31 | -------------------------------------------------------------------------------- /redis-cluster-client/src/main/java/com/linda/cluster/redis/client/cluster/ClusterJedisTemplate.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.client.cluster; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | import redis.clients.jedis.Jedis; 8 | import redis.clients.jedis.JedisPoolConfig; 9 | import redis.clients.util.Hashing; 10 | 11 | import com.linda.cluster.redis.common.bean.HostAndPort; 12 | import com.linda.cluster.redis.common.utils.SlotUtils; 13 | 14 | 15 | public class ClusterJedisTemplate extends JedisTemplate{ 16 | 17 | private ClusterRedisConnectionHandler connectionHandler; 18 | 19 | private KeySlotInfo slotInfo; 20 | 21 | private Hashing hashing = Hashing.MD5; 22 | 23 | private Logger logger = Logger.getLogger(ClusterJedisTemplate.class); 24 | 25 | public ClusterJedisTemplate(JedisPoolConfig poolConfig,List zkhosts,String product){ 26 | connectionHandler = new ClusterRedisConnectionHandler(poolConfig,zkhosts,product,null,null); 27 | } 28 | 29 | public ClusterJedisTemplate(JedisPoolConfig poolConfig,List zkhosts,String product,String password){ 30 | connectionHandler = new ClusterRedisConnectionHandler(poolConfig,zkhosts,product,password,null); 31 | } 32 | 33 | public ClusterJedisTemplate(JedisPoolConfig poolConfig,List zkhosts,String product,String password,String basepath){ 34 | connectionHandler = new ClusterRedisConnectionHandler(poolConfig,zkhosts,product,password,basepath); 35 | } 36 | 37 | public KeySlotInfo getSlotInfo() { 38 | return slotInfo; 39 | } 40 | 41 | public void setSlotInfo(KeySlotInfo slotInfo) { 42 | this.slotInfo = slotInfo; 43 | } 44 | 45 | @Override 46 | protected Jedis getResource(String key) { 47 | long hash = hashing.hash(key); 48 | int slot = SlotUtils.slot(hash); 49 | Jedis jedis = connectionHandler.getConnectionFromSlot(slot); 50 | if(slotInfo!=null){ 51 | slotInfo.info(key, slot, jedis); 52 | } 53 | return jedis; 54 | } 55 | 56 | @Override 57 | protected void returnResource(Jedis jedis) { 58 | connectionHandler.returnConnection(jedis); 59 | } 60 | 61 | @Override 62 | protected void returnBrokenResource(Jedis jedis) { 63 | connectionHandler.returnBrokenConnection(jedis); 64 | } 65 | 66 | @Override 67 | protected Jedis getResource(byte[] key) { 68 | long hash = hashing.hash(key); 69 | int slot = SlotUtils.slot(hash); 70 | logger.debug("key:"+key+" slot:"+slot); 71 | return connectionHandler.getConnectionFromSlot(slot); 72 | } 73 | 74 | @Override 75 | public void close() { 76 | connectionHandler.close(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /redis-cluster-monitor/src/main/java/com/linda/cluster/redis/monitor/dao/sqlmap/MonitorPersistenceDaoSqlmap.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into redis_monitor_persistence(productId,clusterId,redisNodeId,addtime,rdb_changes_since_last_save,rdb_bgsave_in_progress,rdb_last_save_time,rdb_last_bgsave_status, 9 | rdb_last_bgsave_time_sec,rdb_current_bgsave_time_sec,aof_enabled,aof_rewrite_in_progress,aof_rewrite_scheduled,aof_last_rewrite_time_sec,aof_current_rewrite_time_sec, 10 | aof_last_bgrewrite_status,aof_last_write_status) 11 | values(#{productId},#{clusterId},#{redisNodeId},#{addtime},#{rdb_changes_since_last_save},#{rdb_bgsave_in_progress},#{rdb_last_save_time},#{rdb_last_bgsave_status}, 12 | #{rdb_last_bgsave_time_sec},#{rdb_current_bgsave_time_sec},#{aof_enabled},#{aof_rewrite_in_progress},#{aof_rewrite_scheduled},#{aof_last_rewrite_time_sec}, 13 | #{aof_current_rewrite_time_sec},#{aof_last_bgrewrite_status},#{aof_last_write_status}) 14 | 15 | 16 | 19 | 20 | 23 | 24 | 27 | 28 | 31 | 32 | 35 | 36 | 39 | 40 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/IntrospectorUtils.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | import java.lang.reflect.Field; 4 | import java.util.Arrays; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.Properties; 8 | 9 | import org.apache.log4j.Logger; 10 | 11 | /** 12 | * utils for java bean field set from properties config file 13 | * @author hzlindzh 14 | * 15 | */ 16 | public class IntrospectorUtils { 17 | 18 | private static Logger logger = Logger.getLogger(IntrospectorUtils.class); 19 | 20 | public static T getInstance(Class clazz,Properties properties){ 21 | try { 22 | T object = clazz.newInstance(); 23 | IntrospectorUtils.setProperties(object, properties); 24 | return object; 25 | } catch (InstantiationException e) { 26 | logger.error("InstantiationException "+e.getMessage()); 27 | } catch (IllegalAccessException e) { 28 | logger.error("IllegalAccessException "+e.getMessage()); 29 | } 30 | return null; 31 | } 32 | 33 | public static void setProperties(Object obj,Properties prop){ 34 | List fields = getAllDeclearedFields(obj.getClass()); 35 | for(Field f:fields){ 36 | String v = prop.getProperty(f.getName()); 37 | if(v!=null){ 38 | try { 39 | v = v.trim(); 40 | f.setAccessible(true); 41 | f.set(obj, convertValue(f.getType(), v)); 42 | } catch (IllegalArgumentException e) { 43 | logger.error("IllegalArgumentException field "+f.getName()+" "+obj.getClass()+" "+e.getMessage()); 44 | } catch (IllegalAccessException e) { 45 | logger.error("IllegalAccessException field "+f.getName()+" "+obj.getClass()+" "+e.getMessage()); 46 | } 47 | } 48 | } 49 | } 50 | 51 | public static List getAllDeclearedFields(Class clazz){ 52 | List list = new LinkedList(); 53 | if(clazz!=Object.class){ 54 | list.addAll(Arrays.asList(clazz.getDeclaredFields())); 55 | list.addAll(getAllDeclearedFields(clazz.getSuperclass())); 56 | } 57 | return list; 58 | } 59 | 60 | public static Object convertValue(Class need,String value){ 61 | if(need==byte.class||need==Byte.class){ 62 | return Byte.parseByte(value); 63 | }else if(need==short.class||need==Short.class){ 64 | return Short.parseShort(value); 65 | }else if(need==int.class||need==Integer.class){ 66 | return Integer.parseInt(value); 67 | }else if(need==long.class||need==Long.class){ 68 | return Long.parseLong(value); 69 | }else if(need==boolean.class||need==Boolean.class){ 70 | return Boolean.parseBoolean(value); 71 | }else if(need==float.class||need==Float.class){ 72 | return Float.parseFloat(value); 73 | }else if(need==double.class||need==Double.class){ 74 | return Double.parseDouble(value); 75 | }else if(need==char.class||need==Character.class){ 76 | return value.trim().charAt(0); 77 | }else{ 78 | return value; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/alived/RedisKeepAliveStartup.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.alived; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import org.apache.log4j.Logger; 9 | import org.apache.zookeeper.ZooKeeper; 10 | 11 | import com.linda.cluster.redis.common.bean.HostAndPort; 12 | import com.linda.cluster.redis.common.utils.IOUtils; 13 | import com.linda.cluster.redis.common.utils.JSONUtils; 14 | import com.linda.cluster.redis.common.utils.RedisZookeeperUtils; 15 | import com.linda.cluster.redis.keepalived.conf.RedisProduct; 16 | import com.linda.cluster.redis.keepalived.conf.RedisZkConf; 17 | import com.linda.cluster.redis.keepalived.zk.RedisZkClusterAliveService; 18 | 19 | public class RedisKeepAliveStartup { 20 | 21 | private static Logger logger = Logger.getLogger(RedisKeepAliveStartup.class); 22 | 23 | public static void main(String[] args) throws IOException, InterruptedException { 24 | String file = "classpath:keepalived-conf.json"; 25 | InputStream ins = IOUtils.getClassPathInputStream(file); 26 | if(ins==null){ 27 | logger.info("conf file:"+file+" can't find"); 28 | System.exit(0); 29 | } 30 | String confJson = IOUtils.toString(ins); 31 | ins.close(); 32 | RedisZkConf zkConf = JSONUtils.fromJson(confJson, RedisZkConf.class); 33 | if(zkConf==null){ 34 | logger.info("load conf file:"+file+" error"); 35 | System.exit(0); 36 | }else{ 37 | String base = zkConf.getZkBasePath(); 38 | List products = zkConf.getProducts(); 39 | List zkhosts = zkConf.getZkhosts(); 40 | String server = RedisZookeeperUtils.toString(zkhosts); 41 | Object sync = new Object(); 42 | logger.info("start to connect to zk service"); 43 | ZooKeeper keeper = new ZooKeeper(server,10000,new AlivedWatcher(sync)); 44 | logger.info("wait for zk connect"); 45 | synchronized(sync){ 46 | sync.wait(); 47 | } 48 | logger.info("zk connected"); 49 | ArrayList clusterMonitors = new ArrayList(); 50 | if(products!=null&&products.size()>0){ 51 | for(RedisProduct product:products){ 52 | List clusters = product.getClusters(); 53 | for(String cluster:clusters){ 54 | RedisZkClusterAliveService clusterAlive = new RedisZkClusterAliveService(keeper,base,product.getProductName(),cluster,product.getPingInterval()); 55 | clusterMonitors.add(clusterAlive); 56 | } 57 | } 58 | } 59 | if(clusterMonitors.size()>0){ 60 | logger.info("cluster monitor cluster size:"+clusterMonitors.size()); 61 | for(RedisZkClusterAliveService alive:clusterMonitors){ 62 | alive.startup(); 63 | } 64 | logger.info("start up monitor success"); 65 | }else{ 66 | logger.info("cluster monitor clusters 0 start to exit"); 67 | keeper.close(); 68 | } 69 | } 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/bean/HostAndPort.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.bean; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | 8 | import org.apache.zookeeper.data.Stat; 9 | import org.codehaus.jackson.annotate.JsonIgnore; 10 | 11 | @AllArgsConstructor 12 | @NoArgsConstructor 13 | public class HostAndPort { 14 | 15 | @Getter 16 | @Setter 17 | private String name; 18 | @Getter 19 | @Setter 20 | private String host; 21 | @Getter 22 | @Setter 23 | private int port; 24 | @Getter 25 | @Setter 26 | private boolean alive;//节点是否存活,用于keepalived info 27 | @Getter 28 | @Setter 29 | private String master; 30 | @Getter 31 | @Setter 32 | @JsonIgnore 33 | private HostAndPort next; 34 | @Getter 35 | @Setter 36 | @JsonIgnore 37 | private Stat stat; 38 | 39 | public HostAndPort(String host,int port){ 40 | this.host = host; 41 | this.port = port; 42 | } 43 | 44 | public Object clone() throws CloneNotSupportedException { 45 | HostAndPort hostAndPort = new HostAndPort(); 46 | hostAndPort.name = name; 47 | hostAndPort.host = host; 48 | hostAndPort.port = port; 49 | hostAndPort.alive = alive; 50 | hostAndPort.master = master; 51 | hostAndPort.stat = stat; 52 | hostAndPort.next = null; 53 | return hostAndPort; 54 | } 55 | 56 | @Override 57 | public int hashCode() { 58 | final int prime = 31; 59 | int result = 1; 60 | result = prime * result + (alive ? 1231 : 1237); 61 | result = prime * result + ((host == null) ? 0 : host.hashCode()); 62 | result = prime * result + ((master == null) ? 0 : master.hashCode()); 63 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 64 | result = prime * result + port; 65 | return result; 66 | } 67 | 68 | public void copyFileds(HostAndPort hh){ 69 | this.name = hh.name; 70 | this.master = hh.master; 71 | this.host = hh.host; 72 | this.port = hh.port; 73 | this.alive = hh.alive; 74 | this.stat = hh.stat; 75 | } 76 | 77 | @Override 78 | public boolean equals(Object obj) { 79 | if (this == obj) 80 | return true; 81 | if (obj == null) 82 | return false; 83 | if (getClass() != obj.getClass()) 84 | return false; 85 | HostAndPort other = (HostAndPort) obj; 86 | if (alive != other.alive) 87 | return false; 88 | if (host == null) { 89 | if (other.host != null) 90 | return false; 91 | } else if (!host.equals(other.host)) 92 | return false; 93 | if (master == null) { 94 | if (other.master != null) 95 | return false; 96 | } else if (!master.equals(other.master)) 97 | return false; 98 | if (name == null) { 99 | if (other.name != null) 100 | return false; 101 | } else if (!name.equals(other.name)) 102 | return false; 103 | if (port != other.port) 104 | return false; 105 | return true; 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /redis-cluster-common/src/main/java/com/linda/cluster/redis/common/utils/JSONUtils.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.common.utils; 2 | 3 | import org.apache.commons.lang.StringUtils; 4 | import org.apache.log4j.Logger; 5 | import org.codehaus.jackson.JsonNode; 6 | import org.codehaus.jackson.map.ObjectMapper; 7 | import org.codehaus.jackson.type.TypeReference; 8 | 9 | 10 | public class JSONUtils { 11 | 12 | private static ObjectMapper objMapper = new ObjectMapper(); 13 | private static Logger logger = Logger.getLogger(JSONUtils.class); 14 | 15 | public static String toJson(Object obj) { 16 | try { 17 | return objMapper.writeValueAsString(obj); 18 | } catch (Exception e) { 19 | logger.error("json write exception " + obj.toString()); 20 | } 21 | return null; 22 | } 23 | 24 | public static T fromJson(String json, Class clz) { 25 | if (StringUtils.isBlank(json)) { 26 | return null; 27 | } 28 | try { 29 | return objMapper.readValue(json, clz); 30 | } catch (Exception e) { 31 | e.printStackTrace(); 32 | logger.error("read json exception " + json); 33 | } 34 | return null; 35 | } 36 | 37 | public static T fromJson(String json, TypeReference typeReference) { 38 | if (StringUtils.isBlank(json)) { 39 | return null; 40 | } 41 | try { 42 | return objMapper.readValue(json, typeReference); 43 | } catch (Exception e) { 44 | logger.error("read json exception " + json); 45 | } 46 | return null; 47 | } 48 | 49 | /** 50 | * 只能取出不包含数组的部分 51 | * @param json 52 | * @param propertyName 53 | * @param clazz 54 | * @return 55 | */ 56 | public static T getProperty(String json, String propertyName, Class clazz) { 57 | try { 58 | JsonNode readTree = objMapper.readTree(json); 59 | String[] hirarchyProperties = propertyName.split("\\."); 60 | int i = 0; 61 | for (String property : hirarchyProperties) { 62 | i++; 63 | if (readTree.has(property)) { 64 | readTree = readTree.get(property); 65 | } else { 66 | if (i != hirarchyProperties.length) { 67 | logger.error("encounter unexpected internal Property:" + propertyName + " json: " + json + " clz:" + clazz.getName()); 68 | } else { 69 | return null; 70 | } 71 | } 72 | } 73 | if (clazz == Long.class) { 74 | return (T) new Long(readTree.asLong()); 75 | } else if (clazz == Integer.class) { 76 | return (T) new Integer(readTree.asInt()); 77 | } else if (clazz == Float.class) { 78 | return (T) new Float(readTree.asDouble()); 79 | } else if (clazz == Double.class) { 80 | return (T) new Double(readTree.asDouble()); 81 | } else if (clazz == String.class) { 82 | return (T) readTree.asText(); 83 | } else if (clazz == Boolean.class) { 84 | return (T) Boolean.valueOf(readTree.asBoolean()); 85 | } else { 86 | return (T) readTree; 87 | } 88 | } catch (Exception e) { 89 | logger.error("getProperty properites:" + propertyName + " json: " + json + " clz:" + clazz.getName(), e); 90 | } 91 | return null; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/src/main/java/com/linda/cluster/redis/keepalived/redis/RedisAliveBase.java: -------------------------------------------------------------------------------- 1 | package com.linda.cluster.redis.keepalived.redis; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import redis.clients.jedis.Jedis; 9 | 10 | import com.linda.cluster.redis.common.bean.HostAndPort; 11 | import com.linda.cluster.redis.keepalived.redis.RedisDataNode.RedisState; 12 | 13 | public abstract class RedisAliveBase implements RedisAlivedListener{ 14 | 15 | protected List listeners = new ArrayList(); 16 | protected Jedis jedis; 17 | @Setter 18 | @Getter 19 | protected HostAndPort redisHost; 20 | @Getter 21 | protected RedisState state = RedisState.INIT; 22 | 23 | private int failCount = 0; 24 | 25 | private void init(){ 26 | jedis = new Jedis(redisHost.getHost(),redisHost.getPort()); 27 | } 28 | 29 | public void connect(){ 30 | state = RedisState.CONNECTING; 31 | try{ 32 | this.init(); 33 | jedis.connect(); 34 | state = RedisState.CONNECTED; 35 | this.onConnected(this); 36 | failCount = 0; 37 | }catch(Exception e){ 38 | state = RedisState.ERROR; 39 | this.onException(this,e); 40 | } 41 | } 42 | 43 | public void close(){ 44 | state = RedisState.CLOSE; 45 | try{ 46 | jedis.close(); 47 | failCount = 0; 48 | }catch(Exception e){ 49 | state = RedisState.ERROR; 50 | this.onException(this,e); 51 | } 52 | } 53 | 54 | public void info(){ 55 | try{ 56 | String info = jedis.info(); 57 | this.onInfo(this, info); 58 | failCount = 0; 59 | }catch(Exception e){ 60 | state = RedisState.ERROR; 61 | this.onException(this,e); 62 | this.connect(); 63 | } 64 | } 65 | 66 | public void ping(){ 67 | try{ 68 | this.jedis.ping(); 69 | this.onPing(this); 70 | failCount = 0; 71 | }catch(Exception e){ 72 | state = RedisState.ERROR; 73 | this.onException(this,e); 74 | this.connect(); 75 | } 76 | } 77 | 78 | public void addRedisListener(RedisAlivedListener listener){ 79 | listeners.add(listener); 80 | } 81 | 82 | @Override 83 | public void onConnected(RedisAliveBase redis) { 84 | for(RedisAlivedListener listener:listeners){ 85 | listener.onConnected(redis); 86 | } 87 | } 88 | 89 | @Override 90 | public void onClose(RedisAliveBase redis) { 91 | for(RedisAlivedListener listener:listeners){ 92 | listener.onClose(redis); 93 | } 94 | } 95 | 96 | @Override 97 | public void onException(RedisAliveBase redis,Exception e) { 98 | failCount++; 99 | if(this.failCount<5){ 100 | for(RedisAlivedListener listener:listeners){ 101 | listener.onException(redis,e); 102 | } 103 | } 104 | } 105 | 106 | @Override 107 | public void onInfo(RedisAliveBase redis,String info) { 108 | for(RedisAlivedListener listener:listeners){ 109 | listener.onInfo(redis, info); 110 | } 111 | } 112 | 113 | @Override 114 | public void onPing(RedisAliveBase redis) { 115 | for(RedisAlivedListener listener:listeners){ 116 | listener.onPing(redis); 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /redis-cluster-aof/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.linda 6 | redis-cluster-aof 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | redis-cluster-aof 11 | http://maven.apache.org 12 | 13 | 14 | 0.0.1 15 | UTF-8 16 | 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.11 23 | test 24 | 25 | 26 | com.linda 27 | redis-cluster-common 28 | ${redis-cluster-common.version} 29 | 30 | 31 | log4j 32 | log4j 33 | 1.2.16 34 | 35 | 36 | redis.clients 37 | jedis 38 | 2.4.2 39 | 40 | 41 | 42 | org.apache.zookeeper 43 | zookeeper 44 | 3.4.5 45 | 46 | 47 | 48 | com.alibaba 49 | fastjson 50 | 1.1.32 51 | 52 | 53 | org.projectlombok 54 | lombok 55 | 0.10.8 56 | 57 | 58 | 59 | 60 | 61 | 62 | maven-source-plugin 63 | 2.1 64 | 65 | true 66 | 67 | 68 | 69 | attach-sources 70 | compile 71 | jar-no-fork 72 | 73 | 74 | 75 | 76 | maven-deploy-plugin 77 | 78 | 79 | deploy 80 | deploy 81 | deploy 82 | 83 | 84 | 85 | 86 | maven-compiler-plugin 87 | 3.1 88 | 89 | 1.6 90 | 1.6 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /redis-cluster-config/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.linda 6 | redis-cluster-config 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | redis-cluster-config 11 | http://maven.apache.org 12 | 13 | 14 | 0.0.1 15 | UTF-8 16 | 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.11 23 | test 24 | 25 | 26 | com.linda 27 | redis-cluster-common 28 | ${redis-cluster-common.version} 29 | 30 | 31 | log4j 32 | log4j 33 | 1.2.16 34 | 35 | 36 | redis.clients 37 | jedis 38 | 2.4.2 39 | 40 | 41 | 42 | org.apache.zookeeper 43 | zookeeper 44 | 3.4.5 45 | 46 | 47 | 48 | com.alibaba 49 | fastjson 50 | 1.1.32 51 | 52 | 53 | org.projectlombok 54 | lombok 55 | 0.10.8 56 | 57 | 58 | 59 | 60 | 61 | 62 | maven-source-plugin 63 | 2.1 64 | 65 | true 66 | 67 | 68 | 69 | attach-sources 70 | compile 71 | jar-no-fork 72 | 73 | 74 | 75 | 76 | maven-deploy-plugin 77 | 78 | 79 | deploy 80 | deploy 81 | deploy 82 | 83 | 84 | 85 | 86 | maven-compiler-plugin 87 | 3.1 88 | 89 | 1.6 90 | 1.6 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /redis-cluster-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.linda 6 | redis-cluster-client 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | redis-cluster-client 11 | http://maven.apache.org 12 | 13 | 14 | 0.0.1 15 | UTF-8 16 | 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.11 24 | test 25 | 26 | 27 | com.linda 28 | redis-cluster-common 29 | ${redis-cluster-common.version} 30 | 31 | 32 | log4j 33 | log4j 34 | 1.2.16 35 | 36 | 37 | redis.clients 38 | jedis 39 | 2.4.2 40 | 41 | 42 | 43 | org.apache.zookeeper 44 | zookeeper 45 | 3.4.5 46 | 47 | 48 | 49 | com.alibaba 50 | fastjson 51 | 1.1.32 52 | 53 | 54 | org.projectlombok 55 | lombok 56 | 0.10.8 57 | 58 | 59 | 60 | 61 | 62 | 63 | maven-source-plugin 64 | 2.1 65 | 66 | true 67 | 68 | 69 | 70 | attach-sources 71 | compile 72 | jar-no-fork 73 | 74 | 75 | 76 | 77 | maven-deploy-plugin 78 | 79 | 80 | deploy 81 | deploy 82 | deploy 83 | 84 | 85 | 86 | 87 | maven-compiler-plugin 88 | 3.1 89 | 90 | 1.6 91 | 1.6 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /redis-cluster-keepalived/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.linda 6 | redis-cluster-keepalived 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | redis-cluster-keepalived 11 | http://maven.apache.org 12 | 13 | 14 | 0.0.1 15 | UTF-8 16 | 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.11 23 | test 24 | 25 | 26 | com.linda 27 | redis-cluster-common 28 | ${redis-cluster-common.version} 29 | 30 | 31 | log4j 32 | log4j 33 | 1.2.16 34 | 35 | 36 | redis.clients 37 | jedis 38 | 2.4.2 39 | 40 | 41 | 42 | org.apache.zookeeper 43 | zookeeper 44 | 3.4.5 45 | 46 | 47 | 48 | com.alibaba 49 | fastjson 50 | 1.1.32 51 | 52 | 53 | org.projectlombok 54 | lombok 55 | 0.10.8 56 | 57 | 58 | 59 | 60 | 61 | 62 | maven-source-plugin 63 | 2.1 64 | 65 | true 66 | 67 | 68 | 69 | attach-sources 70 | compile 71 | jar-no-fork 72 | 73 | 74 | 75 | 76 | maven-deploy-plugin 77 | 78 | 79 | deploy 80 | deploy 81 | deploy 82 | 83 | 84 | 85 | 86 | maven-compiler-plugin 87 | 3.1 88 | 89 | 1.6 90 | 1.6 91 | 92 | 93 | 94 | 95 | 96 | --------------------------------------------------------------------------------