配置Redisjava
爲了方便管理,咱們將統一的配置放到 cluster.conf 文件中,別的文件引入該文件:node
bind 127.0.0.1 protected-mode yes port 6370 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "./redis.log" databases 100 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename master.rdb dir ./ #slaveof 10.1.1.105 6379 #masterauth admin123 #將次 Redis 實例中啓用 Redis Cluster 支持。不然,實例一般做爲獨立實例啓動 cluster-enabled yes #這是集羣中的節點可以失聯的最大時間,超過這個時間,該節點就會被認爲故障。 cluster-node-timeout 15000 #此配置文件不能人工編輯,它是集羣節點自動維護的文件,主要用於記錄集羣中有哪些節點、他們的狀態以及一些持久化參數等,方便在重啓時恢復這些狀態 cluster-config-file master-cluster.config slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 #密碼,這裏能夠不設置,設置的話啓動集羣管理的時候會報錯,下文會講解錯誤的解決方法 requirepass admin123 appendonly no appendfilename "appendonly6370.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 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 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
文件目錄以下:web
└── cluster ├── 6370 │ ├── 6370.conf │ ├── master-cluster.config │ ├── master.rdb │ └── redis.log ├── 6371 │ ├── 6371.conf │ ├── master-cluster.config │ ├── master.rdb │ └── redis.log ├── 6372 │ ├── 6372.conf │ ├── master-cluster.config │ └── redis.log ├── 6373 │ ├── 6373.conf │ ├── master-cluster.config │ └── redis.log ├── 6374 │ ├── 6374.conf │ ├── master-cluster.config │ └── redis.log ├── 6375 │ ├── 6375.conf │ ├── master-cluster.config │ └── redis.log └── cluster.conf
其中文件夾名爲端口號,咱們只拿 6370 看一眼看(別的只是修改一下端口號):redis
include /root/redis/cluster/cluster.conf port 6370
怎麼樣是否是很簡單呢?緩存
咱們在每一個端口的目錄下啓動各自的 redis :ruby
[coderknock 6370]# redis-server 6370.conf
按照上面的模式咱們將設置的 6 個 redis 啓動。app
安裝 rubyless
[root@iZ28glf0u1jZ ~]# sudo yum install ruby Loaded plugins: security Setting up Install Process base | 3.7 kB 00:00 elrepo | 2.9 kB 00:00 epel | 4.3 kB 00:00 extras | 3.3 kB 00:00 fpco | 2.9 kB 00:00 updates | 3.4 kB 00:00 updates/primary_db | 817 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package ruby.i686 0:1.8.7.374-5.el6 will be installed --> Processing Dependency: ruby-libs = 1.8.7.374-5.el6 for package: ruby-1.8.7.374-5.el6.i686 --> Processing Dependency: libruby.so.1.8 for package: ruby-1.8.7.374-5.el6.i686 --> Running transaction check ---> Package ruby-libs.i686 0:1.8.7.374-5.el6 will be installed --> Processing Dependency: libreadline.so.5 for package: ruby-libs-1.8.7.374-5.el6.i686 --> Running transaction check ---> Package compat-readline5.i686 0:5.2-17.1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================ Package Arch Version Repository Size ============================================================================================================================================ Installing: ruby i686 1.8.7.374-5.el6 base 538 k Installing for dependencies: compat-readline5 i686 5.2-17.1.el6 base 128 k ruby-libs i686 1.8.7.374-5.el6 base 1.6 M Transaction Summary ============================================================================================================================================ Install 3 Package(s) Total download size: 2.3 M Installed size: 7.8 M Is this ok [y/N]: 也 Is this ok [y/N]: y Downloading Packages: (1/3): compat-readline5-5.2-17.1.el6.i686.rpm | 128 kB 00:00 (2/3): ruby-1.8.7.374-5.el6.i686.rpm | 538 kB 00:00 (3/3): ruby-libs-1.8.7.374-5.el6.i686.rpm | 1.6 MB 00:01 -------------------------------------------------------------------------------------------------------------------------------------------- Total 1.1 MB/s | 2.3 MB 00:02 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : compat-readline5-5.2-17.1.el6.i686 1/3 Installing : ruby-libs-1.8.7.374-5.el6.i686 2/3 Installing : ruby-1.8.7.374-5.el6.i686 3/3 Verifying : compat-readline5-5.2-17.1.el6.i686 1/3 Verifying : ruby-1.8.7.374-5.el6.i686 2/3 Verifying : ruby-libs-1.8.7.374-5.el6.i686 3/3 Installed: ruby.i686 0:1.8.7.374-5.el6 Dependency Installed: compat-readline5.i686 0:5.2-17.1.el6 ruby-libs.i686 0:1.8.7.374-5.el6 Complete!
安裝 rubygemssocket
[root@iZ28glf0u1jZ ~]# yum -y install ruby rubygems Loaded plugins: security Setting up Install Process Package ruby-1.8.7.374-5.el6.i686 already installed and latest version Resolving Dependencies --> Running transaction check ---> Package rubygems.noarch 0:1.3.7-5.el6 will be installed --> Processing Dependency: ruby-rdoc for package: rubygems-1.3.7-5.el6.noarch --> Running transaction check ---> Package ruby-rdoc.i686 0:1.8.7.374-5.el6 will be installed --> Processing Dependency: ruby-irb = 1.8.7.374-5.el6 for package: ruby-rdoc-1.8.7.374-5.el6.i686 --> Running transaction check ---> Package ruby-irb.i686 0:1.8.7.374-5.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================================================= Package Arch Version Repository Size ======================================================================================================================================================================================================= Installing: rubygems noarch 1.3.7-5.el6 base 207 k Installing for dependencies: ruby-irb i686 1.8.7.374-5.el6 base 318 k ruby-rdoc i686 1.8.7.374-5.el6 base 381 k Transaction Summary ======================================================================================================================================================================================================= Install 3 Package(s) Total download size: 905 k Installed size: 3.0 M Downloading Packages: (1/3): ruby-irb-1.8.7.374-5.el6.i686.rpm | 318 kB 00:00 (2/3): ruby-rdoc-1.8.7.374-5.el6.i686.rpm | 381 kB 00:00 (3/3): rubygems-1.3.7-5.el6.noarch.rpm | 207 kB 00:00 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 1.7 MB/s | 905 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : ruby-irb-1.8.7.374-5.el6.i686 1/3 Installing : ruby-rdoc-1.8.7.374-5.el6.i686 2/3 Installing : rubygems-1.3.7-5.el6.noarch 3/3 Verifying : ruby-rdoc-1.8.7.374-5.el6.i686 1/3 Verifying : rubygems-1.3.7-5.el6.noarch 2/3 Verifying : ruby-irb-1.8.7.374-5.el6.i686 3/3 Installed: rubygems.noarch 0:1.3.7-5.el6 Dependency Installed: ruby-irb.i686 0:1.8.7.374-5.el6 ruby-rdoc.i686 0:1.8.7.374-5.el6 Complete!
安裝配置集羣管理tcp
安裝 ruby 下的 redis 管理工具
gem install redis
啓動集羣管理
找到 redis 源碼包中 src 下的 redis-trib.rb 或者下載一個該文件也能夠
[coderknock redis]# /root/redis-3.2.0/src/redis-trib.rb create --replicas 1 127.0.0.1:6370 127.0.0.1:6371 127.0.0.1:6372 127.0.0.1:6373 127.0.0.1:6374 127.0.0.1:6375 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:6370 127.0.0.1:6371 127.0.0.1:6372 Adding replica 127.0.0.1:6373 to 127.0.0.1:6370 Adding replica 127.0.0.1:6374 to 127.0.0.1:6371 Adding replica 127.0.0.1:6375 to 127.0.0.1:6372 M: f9acf4403c84c23ee9a79952e1f26a02cca40647 127.0.0.1:6370 slots:0-5460 (5461 slots) master M: f070ba8fedc4e9240c35b33684554080d0e2f7da 127.0.0.1:6371 slots:5461-10922 (5462 slots) master M: ae1502fbdfe2823dff2836ff61664da5affbd7e4 127.0.0.1:6372 slots:10923-16383 (5461 slots) master S: b1c4a2eeff66c8d5f1997d3a3210120552f7bc72 127.0.0.1:6373 replicates f9acf4403c84c23ee9a79952e1f26a02cca40647 S: 8d661ae944b842b21739aff41ce7005ed30ccaad 127.0.0.1:6374 replicates f070ba8fedc4e9240c35b33684554080d0e2f7da S: 429fea95c1428e6cfa39a3a0b0d67fa8e2c606d2 127.0.0.1:6375 replicates ae1502fbdfe2823dff2836ff61664da5affbd7e4 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 127.0.0.1:6370) M: f9acf4403c84c23ee9a79952e1f26a02cca40647 127.0.0.1:6370 slots:0-5460 (5461 slots) master M: f070ba8fedc4e9240c35b33684554080d0e2f7da 127.0.0.1:6371 slots:5461-10922 (5462 slots) master M: ae1502fbdfe2823dff2836ff61664da5affbd7e4 127.0.0.1:6372 slots:10923-16383 (5461 slots) master M: b1c4a2eeff66c8d5f1997d3a3210120552f7bc72 127.0.0.1:6373 slots: (0 slots) master replicates f9acf4403c84c23ee9a79952e1f26a02cca40647 M: 8d661ae944b842b21739aff41ce7005ed30ccaad 127.0.0.1:6374 slots: (0 slots) master replicates f070ba8fedc4e9240c35b33684554080d0e2f7da M: 429fea95c1428e6cfa39a3a0b0d67fa8e2c606d2 127.0.0.1:6375 slots: (0 slots) master replicates ae1502fbdfe2823dff2836ff61664da5affbd7e4 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
這樣就啓動成功了。
[ERR] Sorry, can't connect to node 127.0.0.1:6370
出現這個可能的緣由是 ruby 版本過低,升級以前安裝的那幾個東西。還有可能就是設置了 redis 密碼的緣故。
找到 usr/lib/ruby/gems/1.8/gems/redis-3.3.3/lib/redis,編輯 client.rb:
require "redis/errors" require "socket" require "cgi" class Redis class Client DEFAULTS = { :url => lambda { ENV["REDIS_URL"] }, :scheme => "redis", :host => "127.0.0.1", :port => 6379, :path => nil, :timeout => 5.0, :password => "admin123",//這裏設置你的密碼 :db => 0, :driver => nil, :id => nil, :tcp_keepalive => 0, :reconnect_attempts => 1, :inherit_socket => false } def options Marshal.load(Marshal.dump(@options)) end def scheme @options[:scheme] end def host @options[:host] end def port @options[:port] end def path @options[:path]
而後就能夠正常啓動了。
測試
集羣中使用 redis-cli 應該 使用 -c 參數啓動,而且不支持 select 等操做(具體能夠查看官網)
[coderknock redis]# redis-cli -c -p 6371 -a admin123 127.0.0.1:6371> keys * (empty list or set) 127.0.0.1:6371> get admin -> Redirected to slot [1358] located at 127.0.0.1:6370 "123" 127.0.0.1:6370> set admin aa123 OK 127.0.0.1:6370> get admin "aa123" 127.0.0.1:6370> quit [coderknock redis]# redis-cli -c -p 6370 -a admin123 127.0.0.1:6370> get admin "aa123" 127.0.0.1:6370> [coderknock redis]# redis-cli -c -p 6373 -a admin123 127.0.0.1:6373> get admin -> Redirected to slot [1358] located at 127.0.0.1:6370 "aa123" 127.0.0.1:6370> set admin 73 OK 127.0.0.1:6370> get admin "73" 127.0.0.1:6370> quit [coderknock redis]# redis-cli -c -p 6370 -a admin123 127.0.0.1:6370> get admin "73"
Java Jedis 訪問
import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPoolConfig; import java.util.HashSet; import java.util.Set; /** * * * @author 三產 * @version 1.0 * @date 2017-05-25 * @QQGroup 213732117 * @website http://www.coderknock.com * @copyright Copyright 2017 拿客 coderknock.com All rights reserved. * @since JDK 1.8 */ public class Test { /** * 獲取配置的主從 Redis * * @param serverInfo * @return */ private static Set
更多免費教程請加入Redis緩存技術交流組:288724942