《redis集群搭建过程及在java中的使用.docx》由会员分享,可在线阅读,更多相关《redis集群搭建过程及在java中的使用.docx(28页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、redis集群搭建教程及在JAVA中的使用、redis单机版的安装:1 .环境配置Redis是c开发的,因此安装redis需要c语言的编译环境,即需要安装gcc如何查看是否安装gcc1 gcc -v如果没有gcc,那么需要在线安装.命令如下yum install gcc-c+.下载redis安装包1.1 输入以下命令下载版本的redis (redis从3.0版本后开始支持集群) $ wget :/download, redis, io/releases/redis-3. 0. 0. tar, gz解压redis压缩包,解压命令如下1 进入到redis目录下笔者这里的解压文件是redis-300
2、,大家可以根据自己的需求进行改名或者使用其他版本的redis进入redis目录后,归翻开目录下文件,如下所示:119.23.245.4:22 - rootiZwz9bjiawhqzf40o410tjZ: -/redis-3.0.0 - Xshell 6文件(E)要圾(D 查看工具 选项卡(fi)窗口他)帮助(H)rootiZwz9bjiawhqz f40o410t j Z:root0iZwz9bjiawhqzf40o410tjZ:rootiZwz9bjiawhqzf40o416tjZ:-#root(aiZwz9bj iawhqzf 40o410t jZ:rootiZwz9bjiawhqzf40
3、o410tjZ:-#redis-3.0.0 root0iZwz9bjiawhqzf40o410tjZ:*#rootiZwz9bjiawhqz f40o410t j Z:root0iZwz9bjiawhqzf40o410tjZ:rootiZwz9bjiawhqzf40o416tjZ:-#root(aiZwz9bj iawhqzf 40o410t jZ:rootiZwz9bjiawhqzf40o410tjZ:-#redis-3.0.0 root0iZwz9bjiawhqzf40o410tjZ:*#IsIsIstarXsmaka相关命令存放处zxf pdis-3.0.0.tar.gzroot(aiZ
4、wz9bi iawhqzf40o410t Z :-ldis -3.0.0# Is60-RELEASENOTES COPYINGBUGSCONTRIBUTINGdepsMakefile redis.confMANIFESTO runtestINSTALL READMEredis配置文件runtest-sentinel testsruntest-cluster srcsentinel, conf.编译后的源义d rwx3rootroot4096Mar26 12:06.cache/drwxr-xr-x2rootroot4096Mar26 12:66Pip/-rw-r-r-1rootroot148Au
5、g17 2015.profile-rw-r-r-1rootroot205Sep24 16:22.pydistutils.cfgdrwxr-xr-x2rootroot4096Mar27 17:49.rpmdb/drwx2rootroot4096Sep24 16:22.ssh/drwxr-xr-x2rootroot4096Mar27 17:54.via/-rw1rootroot138Sep24 16:34.Xauthority 2119.23.245.4:221119.23.245.4:22root(aiZwz9bj iawhqzf40o410tjZ: */redis -3.6.6# |SSH2
6、xterm r 110x21 21,45 2 CAr NUM将cluster-enabled yes的注释翻开将redis-cluster/redisO1 文件复制5份到redis-cluste唱录下(redis02-redis06),创立6个redis实例,模拟Redis集 群的6个节点。然后将其余5个文件下的redis.conf里面的端口号分别修改为7002-7006。分别如下列图所示:创立redis02-06目录分别修改redis.con俅彳牛端口号为7002-7006drwxr-xr-x 2 root root 4096 Oct 1 14:51 redis06root0izwz9ef5
7、sjam9pvfyiz26lz redis-cluster)# cd redis02root0izwz9ef5sjam9pvfyiz26lz redis02# ll total 8540drwxr-xr-x 2 root root 4096 Oct 1 14:51 redis06root0izwz9ef5sjam9pvfyiz26lz redis-cluster)# cd redis02root0izwz9ef5sjam9pvfyiz26lz redis02# ll total 8540-rwxr-xr-x1rootroot2675968Oct114:56rcdis-benchnark-rwx
8、r-xr-x1rootroot25166Oct114:50redis-check-aof-rwxr-xr-x1rootroot56668Oct114:56redis-check-duap-rwxr-xr-x1rootroot2266112Oct114:50redis-cli-rw-r-r-1rootroot41402Oct114:56redis.confIrwxrwxrwx1rootroot12Oct114:50redis-sentinel - redis-server-rwxr-xr-x1rootroot4331846Oct114:56redis-serverroot(aizwz9ef5sj
9、am9pvfyiz26lz iredis02# vimredis.conf(rootizwz9ef5sjam9pvfyiz26lz redis62# vim redis.confroot0izwz9ef5sjam9pvfyiz26lz (root0izwz9ef5sjam9pvfyiz261z root0izwz9ef5sjam9pvfyiz261z root(aizwz9ef5s jam9pvf yiz26Xz root0izwz9ef5sjam9pvfyiz261zredis62# cd . redis-cluster# redis-cluster# redis-cluster# redi
10、s-cluster#别修改端口号为7002-7006vim vim vim vimredis03/redis.conf redis04/redis.conf redis05/redis.conf redis06/redis.confdrwxr-xr-x 2rootroot 4096 Oct114:27 redisOldrwxr-xr-x 2rootroot 4096 Oct115:61 redise?drwxr-xr-x 2rootroot 4696 Oct115:62 redis63drwxr-xr-x 2rootroot 4696 Oct115:02 redis04drwxr-xr-x 2
11、rootroot 4096 Oct115:62 redis65drwxr-xr-x 2rootroot 4696 Oct115:62 redis660621925122root0izwz9ef5sjam9pvfyiz26lz redis-cluster# |I root亚zwzgetbsJam9pvtyizblz redls-clusterJr Il total 24SSH2 xterm r 127x27 27,47 1 会话A NUM2345678910111213141516171819cd.3.4 接着启动所有redis节点,由于个启动太麻烦了,所以在这里创立一个批量启动red is节点
12、的脚本文件,命令为start-all.sh,文件内容如下:cd redisOl ./redis-server redis.conf cd .cd redis02 ./redis-server redis.conf cdcd redis03 ./redis-server redis.conf cdcd redis04 ./redis-server redis.conf cd .cd redis05 ./redis-server redis.conf cd .cd redis06 ./redis-server redis.conf创立好启动脚本文件之后,需要修改该脚本的权限,使之能够执行,指令如下
13、:chmod +x start-all.sh47.106.219.251:22 - rootizwz9ef5sjam9pvfyiz26lz:/usr/1ocal/redis-cluster - Xshell 6OX文件(E)案辑查看M 工具(I)选项卡窗口他)帮助(H)*4W :乂 加D *rootizwz9ef5sjam9pvfyiz261z redis-cluster# vim start-all.sh147.106.219.251:22+drwxr-xr-x 2 root root drwxr-xr-x 2 root root drwxr-xr-x 2 root root drwxr-x
14、r-x 2 root root drwxr-xr-x 2 root root drwxr-xr-x 2 root root4096 Oct 1 14:27 redisOl4096 Oct 1 15:01 redis024096 Oct 1 15:02 redis634096 Oct 1 15:02 redis044096 Oct 1 15:02 redis054096 Oct 1 15:02 redis06A(root0izwz9ef5sjam9pvfyiz26lz redis-cluster# lltotal 28drv/xr-xr-x2rootroot4096Oct114:27redisO
15、ldrwxr-xr-x2rootroot4096Oct115:01redis02drwxr-xr-x2rootroot4096Oct115:02redis03drwxr-xr-x2rootroot4096Oct115:02redis04drwxr-xr-x2rootroot4096Oct115:02redis65d rwxr-xr-x2rootroot4096Oct115:02redis06j修改脚本文件的权限-rw-r- -r-1rootroot259Oct115:15start-all.shroot0izwz9ef5sjam9pvfyiz26lz redis-cluster)# chmod
16、 +x start-all.shI Fbot(aiwz9Gt5sjam9pvtyiz261 z y ed is -c I ust er J # llroot0izwz9ef5sjam9pvfyiz26lz redis-cluster# |total 28drwxr-xr-x 2 root root 4096 Oct 1 14:27 redisOldrwxr-xr-x 2 root root 4096 Oct 1 15:01 redis02drwxr-xr-x 2 root root 4096 Oct 1 15:02 redis03drv/xr-xr-x 2 root root 4096 Oct
17、 1 15:02 redis04.,.” , 、 ,drwxr-xr-x 2 root root 4096 Oct 1 15:02 redis051 drwxr-xr-x 2 root root 4096 Oct 1 15:62 redis06-rv/xr-xr-x 1 root root 259 Oct 1 15:15 start-all.shSSH2 xterm r 127x27 - 27,4? 1 人 NUM3.5 执行start-all.sh脚本,启动6个redis节点4b. 47.106.219.251:22 - rootizwz9ef5sjam9pvfyiz26lz:/usr/1o
18、cal/redis-duster - Xshell 6 X文件(F) (E)查看(V)工具(T)选项卡(B)窗口 (W)帮助(H) 牛兜 B 厂:口 a ssh:/47.106.219.251:22(root(aizwz9ef5sjam9pvfyiz261z redis-cluster# chmod *x start-all.sh147.106.219.251:22drwxr-xr-x2rootroot4096Oct114:27redisOld rwxr-xr-x2rootroot4096Oct115:01redis02d rwxr-xr-x2rootroot4096Oct115:02redi
19、s03drwxr-xr-x2rootroot4096Oct115:02redis04d rwxr-xr-x2rootroot4096Oct115:02redis85d rwxr-xr-x2rootroot4096Oct115:02redis06-rw-r-r-1rootroot259Oct115:15start-all.shroot(aizwz9ef 5s jam9pvfyiz26lz redis -cluster# 11total 28drwxr-xr-x2rootroot4096Oct114:27redis81drwxr-xr-x2rootroot4096Oct115:01redis02d
20、rwxr-xr-x2rootroot4096Oct115:02redis03d rwxr-xr-x2rootroot4096Oct115:02redis04drwxr-xr-x2rootroot4096Oct115:62redis65执行脚本,启动所有redis节点d rwxr-xr-x2rootroot4096Oct115:02redis66-rwxr-xr-x 1 root root 259 Oct 1 15:15 start-all.sh ” (root0izwz9ef5sjam9pvfyiz261,z redis-clusterl# ./start-all.shroot(aizwz9e
21、f5sjam9pvfyiz261z redis-cluster# ps aux|grep redis(rvut(pizwz9ef55 jani9pvfyiz26lz r edis -cluster )#root14750.60.81468529604?Ssl15:200:60./redis-servor *:7O01cluster木,白二后一A % q Mlroot14780.66.81488528936?Ssl15:20e:ee./redis-server *:7062(clusterroot14820.00.81408529008?Ssl15:200:00./redis-server a:
22、7063(cluster)已启动成功root14910.0e.81408529004?Ssl15:200:60./redis-server *:7064clusterroot14940.00.81408529004?Ssl15:20o:eo./redis-server :7865clusterroot1561o.e0.81408528972?Ssl15:20o:ee./redis-server *:7006clusterroot1512o.e0.21126682328pts/6S+15:200:60grep -color=auto redisSSH2 xterm r 127x27 : 27,4
23、7 1 蛙.A- NUM3.6 okf至此6个redis节点启动成功,接下来正式开启搭建集群,以上都是准备条件。大家不要觉得图片多看起来冗长所 以觉得麻烦,其实以上步骤也就一句话的事情:创立6个redis实例(6个节点)并启动。要搭建集群的话,需要使用一个工具(脚本文件),这个工具在redis解压文件的源代码里。因为这个工具是一个ruby脚 本文件,所以这个工具的运行需要ruby的运行环境,就相当于java语言的运行需要在jvm上。所以需要安装ruby,指令如 下:1 j yum install ruby然后需要把ruby相关的包安装到服务器,我这里用的是redis-3.0.0.gem,大家需
24、要注意的是:red is的版本和ruby包的版 本最好保持一致。gem 文件的下载地址: s:/rubygems, org/gems/redis/redis 4.1,2A Ruby client that tries to match Redis API one-to-one, while still providing an idiomatic interface.DEVELOPMENT 依系(3):em-synchrony = 0hiredis = 0mocha = 0版本列表:4.1.2 - May 30, 2019 (56.0 KB)4.1.1 - May 06, 2019 (55.5
25、 KB)4.1.0 - December 13, 2018 (54.5 KB)4.1.0.beta1 August 13, 2018(113.5 KB)403 - October 31,2018 (113.5 KB)显示所有(65个版本)-通过点击红框这里选择你需要的版本 s:/rubygems.org/gems/redis/versions- January zi, zui4 (bb.u3.0.6 - November 07, 2013 (64.5 KB)3.0.5 - October 02, 2013 (62.0 KB)3.0.4 - April 23, 2013 (60.5 KB)3.0
26、.3 - March 01,2013 (60.5 KB)3.0.2 - October 05, 2012 (59.0 KB)3.0, June 02. 2012 (57.0 KB)3.0.0 - May 23, 2012 (56.5 KB)3.0.0.rc2 - May 15, 2012 (52.5 KB)3.0.0.rc1 March 10, 2012 (47.5 KB)2.2.2 - August 04, 2011 (41.0 KB)2.2.1 - June 08, 2011 (41.0 KB)2.2.0 March 29, 2011 (39.5 KB)我们需要的时300版本,点击进入许可
27、s:无需要的RUBY版本:无相关链接:主页 源代码 文档 Bug追踪 下载at 订阅 RSS举报15诉 反向依赖所有者:由2 4皤噂作者:Ezra Zygmuntowicz, Taylor Weibley, Matthew Clark, Brian McKinney, Salvatore Sanfilippo, Luca Guidi, Michel Martens, Damian Janowski, Pieter NoordhuisSHA 256 CHECKSUM:e7433ef0e819715d98a860a4730bc95e92bb6cf4b4d95a8fd6f788597f90d2dc一
28、PREVIOUS VERSIONNEXT VERSION 一在网页下方,有下载按钮,点击就可以下载下来了 然后开始安装:安装命令如下:1 3.9上一步中已经把ruby工具所需要的运行环境和ruby包安装好了,接下来需要把这个ruby脚本工具复制到 usr/local/redis-cluster目录下。那么这个ruby脚本工具在哪里呢?之前提到过,在redis解压文件的源代码里,即 redis/src 目录下的 redis-trib.rb 文件。-1 B八I 一人1 一人-rw-rw-r-X11 UU L root1 UU L root乙乙UU X Xz.75651ID YAprJU1X / 2
29、015i du儿1 X .、一,。经12694/1269447.106.219.251:7662 47.10SSH2 xterm r 127x27 127,47 2 蛙 CA NUM147.106.219.251:22 2 47.106.219.251:22+B B B B B B B k k k k k k M9 2 9 9 8 7 6 00 2 3 2 2 9 3Loaded plugins: fastestmirror, langpacksRepository base is listed more than once in the configurationRepository upd
30、ates is listed more than once in the configuration Repository extras is listed more than once in the configuration base pel extras updates (1/3): epel/x86_64/group_gz (2/3): epel/x86_64/updateinfo (3/3): epel/x86_64/primary Loading mirror speeds from cached hostfile eplPackage ruby-2.6.0.648-29.4.al
31、7.x86_64 already installed and latest version Nothing to do root0izwz9ef5sjam9pvfyiz26lz redis-cluster# ll00:00:0660:00:0060:06:6660:00:6060:00:0000:00:06eo:oe:oe00:00:0660:00:0060:06:6660:00:6060:00:0000:00:06eo:oe:oetotal 76drwxr-xr-x2rootroot4096Oct115:20redisOldrwxr-xr-x2rootroot4096Oct115:20red
32、is62drwxr-xr-x2rootroot4696Oct115:20redis63drwxr-xr-x2rootroot4096Oct115:20redis04集群所搭建所需的脚本工具drwxr-xr-x2rootroot4096Oct115:20redis65,/drwxr-xr-x2rootroot4096Oct115:26redis06/更用该工具搭建集群-rwxr-xr-x1rootroot48141Oct116:69redis-trib.rb-rwxr-xr-x1rootroot259Oct115:15start-all.sh(root0izwz9Gf5sJam9pvfyiz26
33、lz redis-cluster# ./redis-trib.rb create -replicas 1 47.106.219.251:7061 6.219.251:7603 47.166.219.251:7004 47.106.219.251:7005 47.106.219.251:7006中途有个地方需要手动输入yes即可至此,Redi集群搭建成功!大家注意最后一段文字,显示了每个节点所分配的slots (哈希槽),这里总共6个节点,其中 3个是从节点,所以3个主节点分别映射了0-5460、5461-10922. 10933-16383soltso3.11最后连接集群节点,连接任意一个即可
34、:1 redisOl/redis-cli -p 7001 -c注意:一定要加上-c,不然节点之间是无法自动蹦专的!如下列图可以看到,存储的数据(key-value)是均匀分配到不同 的节点的:四、结语呼长所口气终于搭建好了 Redis集群。整个过程其实挺简单,本篇主要正对入门级别的小伙伴,插入了很多图片,所以显得冗长,希望大家多多理解,如果不当 之处,还望及时指正 最后,加上两条redis集群基本命令:1 .查看当前集群信息1 cluster info.查看集群里有多少个节点1 cluster nodes三、redis做成开机自启服务: 进入init目录$ cd /etc/init.d/$ I
35、s创立redis-cluster.sh自启脚本$ touch redis-cluster.sh$ vim redis-cluster.sh编辑.sh脚本#!/bin/bash#chkconfig: 123456 80 90#description: redis-cluster.shcd /usr/local/redis-cluster/redisOl ./redis-server redis.confcd /usr/local/redis-cluster/redis02./redis-server redis.confcd /usr/local/redis-cluster/redis03./r
36、edis-server redis.confcd /usr/local/redis-cluster/redis04./redis-server redis.confcd /usr/local/redis-cluster/redis05./redis-server redis.confcd /usr/local/redis-cluster/redis06./redis-server redis.conf注意:前三行不能缺少其中2345是默认启动级别,级别有0-6共7个级别。等级0表示:表不关机等级1表示:单用户模式等级2表示:无网络连接的多用户命令行模式等级3表示:有网络连接的多用户命令行模式等
37、级4表示:不可用等级5表示:带图形界面的多用户模式等级6表示:重新启动15是启动优先级,95是停止优先级,优先级范围是0100,数字越大,优先级越 低。保存退出。将.sh文件加入系统级文件中$ chkconfig -add redis-cluster.sh对.sh文件授权$ chmod +x redis-cluster.sh将.sh做成开机自启$ systemctI enable redis-cluster.sh 关闭redis服务$ systemctI stop redis-cluster.sh 开启redis服务$ systemctl start redis-cluster.sh四、Jav
38、a 使用 redis:安装开始在Java中使用Redis前,我们需要确保已经安装了edis服务及Java redis驱动,且你的机器上能正常使用Java。Java的安装配置可以参 考我们的Java开发环境配置接下来让我们安装Java redis驱动:首先你需要下载驱动包下载jedis.jar,确保下载最新驱动包。在你的classpath中包含该驱动包。下载地址:maven依赖:redis.clientsjedis2.9.0建立redis连接池import java.util.ArrayList;import java.util.List;import redis.clients.jedis.D
39、edis;import redis.clients.jedis.DedisPool;import redis.clients.jedis.JedisPoolConfig;import redis.clients.jedis.DedisShardlnfo;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;public class RedisPool /非切片客户端链接对象private Jedis jedis;/非切片链接池对象private DedisPool jedisPool;/切片客户端链接对象private ShardedDedis shardedJedis;/切片链接池private ShardedDedisPool shardedJedisPool;private String ip = 192.168.224.105;private int port = 7001;public RedisPool()initializePool();initializeSh
限制150内