Redis版本:6.0.6 系统:Centos 7.8
Redis安装 安装编译依赖工具 yum install -y gcc gcc-c++ jemalloc centos-release-scl devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
(此命令执行2次)
scl enable devtoolset-9 bash
进行编译安装: make PREFIX=/usr/local/redis install
安装好后,redis 主目录为 /usr/local/redis
创建配置目录 mkdir -p /usr/local/redis/{data,etc,log}
//创建完后的结构
[root@xxx redis-6.0.6]# tree /usr/local/redis/
/usr/local/redis/
├── bin
│ ├── redis-benchmark
│ ├── redis-check-aof
│ ├── redis-check-rdb
│ ├── redis-cli
│ ├── redis-sentinel -> redis-server
│ └── redis-server
├── data
├── etc
└── log
根据实际情况创建配置文件 举例创建3主3从,单台服务器不同端口去创建(如不同服务器则修改对应ip 即可)。 端口从 6379 ~ 6384
配置文件位置 /usr/local/redis/etc/63xx/redis.conf
1 2 3 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 protected-mode no port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /usr/local/redis/etc/6379.pid loglevel notice logfile /usr/local/redis/log/6379.log databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump-6379.rdb rdb-del-sync-files no dir /usr/local/redis/data/ masterauth ThisisPass99! replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-diskless-load disabled repl-disable-tcp-nodelay no replica-priority 100 acllog-max-len 128 requirepass ThisisPass99! maxmemory 2gb maxmemory-policy volatile-lru lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no lazyfree-lazy-user-del no appendonly yes appendfilename "appendonly-6379.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 5000 cluster-announce-ip 172.25.62.195 cluster-announce-port 6379 cluster-announce-bus-port 16379 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 50 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes jemalloc-bg-thread yes
启动各个节点 1 /usr/local/redis/bin/redis-server /usr/local/redis/etc/63xx/redis.conf
各个节点启动后:
1 2 3 4 5 6 7 [root@xxx redis]# ps -ef|grep redis root 14459 1 0 18:38 ? 00:00:00 ./bin/redis-server *:6379 [cluster] root 14470 1 0 18:38 ? 00:00:00 ./bin/redis-server *:6380 [cluster] root 14476 1 0 18:38 ? 00:00:00 ./bin/redis-server *:6381 [cluster] root 14482 1 0 18:38 ? 00:00:00 ./bin/redis-server *:6382 [cluster] root 14489 1 0 18:38 ? 00:00:00 ./bin/redis-server *:6383 [cluster] root 14495 1 0 18:38 ? 00:00:00 ./bin/redis-server *:6384 [cluster]
创建集群 ./bin/redis-cli --cluster create --cluster-replicas 1 0.0.0.0:6379 0.0.0.0:6380 0.0.0.0:6381 0.0.0.0:6382 0.0.0.0:6383 0.0.0.0:6384 -a ThisisPass99!
输出
1 2 3 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 [root@xxx redis]# ./bin/redis-cli --cluster create --cluster-replicas 1 0.0.0.0:6379 0.0.0.0:6380 0.0.0.0:6381 0.0.0.0:6382 0.0.0.0:6383 0.0.0.0:6384 -a ThisisPass99! Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 0.0.0.0:6383 to 0.0.0.0:6379 Adding replica 0.0.0.0:6384 to 0.0.0.0:6380 Adding replica 0.0.0.0:6382 to 0.0.0.0:6381 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 6a7c5727cad6adbf72ab48cb916c41b7af3ae457 0.0.0.0:6379 slots:[0-5460] (5461 slots) master M: 6078150eb72c898296d719756c4f713e0dfd445c 0.0.0.0:6380 slots:[5461-10922] (5462 slots) master M: 7864aef8fbe43c9f29040905780d610be9c3cc08 0.0.0.0:6381 slots:[10923-16383] (5461 slots) master S: 9929b770b836881e78c06b128c64461b89c3063f 0.0.0.0:6382 replicates 6a7c5727cad6adbf72ab48cb916c41b7af3ae457 S: 41f352f038a8a109e2696e12491cd793cd5feef2 0.0.0.0:6383 replicates 6078150eb72c898296d719756c4f713e0dfd445c S: 01cc17f43586df37011540ece2c01512f4126c8f 0.0.0.0:6384 replicates 7864aef8fbe43c9f29040905780d610be9c3cc08 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join >>> Performing Cluster Check (using node 0.0.0.0:6379) M: 6a7c5727cad6adbf72ab48cb916c41b7af3ae457 0.0.0.0:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 7864aef8fbe43c9f29040905780d610be9c3cc08 172.25.62.195:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 6078150eb72c898296d719756c4f713e0dfd445c 172.25.62.195:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 41f352f038a8a109e2696e12491cd793cd5feef2 172.25.62.195:6383 slots: (0 slots) slave replicates 6078150eb72c898296d719756c4f713e0dfd445c S: 01cc17f43586df37011540ece2c01512f4126c8f 172.25.62.195:6384 slots: (0 slots) slave replicates 7864aef8fbe43c9f29040905780d610be9c3cc08 S: 9929b770b836881e78c06b128c64461b89c3063f 172.25.62.195:6382 slots: (0 slots) slave replicates 6a7c5727cad6adbf72ab48cb916c41b7af3ae457 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
集群检查 连接任意节点 ./bin/redis-cli -c -p 6379
授权 auth ThisisPass99!
cluster相关命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 CLUSTER INFO 127.0.0.1:6379> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:519 cluster_stats_messages_pong_sent:535 cluster_stats_messages_sent:1054 cluster_stats_messages_ping_received:530 cluster_stats_messages_pong_received:519 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:1054
项目Spring Boot 集成redis 1. 手动调用 a)增加依赖 1 2 3 4 5 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>1.5.0.RELEASE</version> </dependency>
b)增加配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 spring: application: name: redis-demo redis: database: 0 password: ThisisPass99! timeout: 10000 cluster: nodes: 172.25 .62 .195 :6379,172.25.62.195:6380,172.25.62.195:6381,172.25.62.195:6382,172.25.62.195:6383,172.25.62.195:6384 max-redirects: 3 jedis: pool: max-active: 600 max-wait: 2000 max-idle: 600 min-idle: 10
c) 调用redisTemplate方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @RestController @RequestMapping("/redis") public class RedisController { @Autowired private RedisTemplate redisTemplate; @ResponseBody @RequestMapping("/set") public String set (@RequestParam(required = false) String key, @RequestParam(required = false) String value) { redisTemplate.opsForValue().set(key, value); return "ok" ; } @ResponseBody @RequestMapping("/get") public String get (@RequestParam(required = false) String key) { return (String)redisTemplate.opsForValue().get(key); } }
2. 作为缓存使用 直接作为缓存使用,有注解的方法,可以很好的对代码无入侵,配置好后基本无感知。
a)额外增加依赖 1 2 3 4 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
b)增加配置类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Configuration @EnableAutoConfiguration public class RedisCacheConfig extends CachingConfigurerSupport { @Autowired private JedisConnectionFactory jedisConnectionFactory; @Bean public CacheManager cacheManager () { RedisCacheManager cacheManager = new RedisCacheManager ( RedisCacheWriter.nonLockingRedisCacheWriter(jedisConnectionFactory), this .getRedisCacheConfigurationWithTtl(60 * 60 * 3 ), this .getRedisCacheConfigurationMap() ); return cacheManager; } }
c)需要缓存的地方增加缓存注解 @Cacheable 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Service public class StudentServiceImpl implements StudentService { @Override @Cacheable(cacheNames = "student", key = "#p0") public Student findByName (String name) { Student student = new Student (); student.setName(name); student.setAge(new Random ().nextInt(40 )); return student; } @Override @Cacheable(cacheNames = "student", key = "#p0") public Student findByName2 (String name) { Student student = new Student (); student.setName(name); student.setAge(new Random ().nextInt(40 )); return student; } }
附 - 相关代码及配置文件 redis 下载链接 相关代码及配置文件Github地址