redis在Windows下以后台服务一键搭建集群(单机--伪集群)

时间:2023-03-08 16:48:25
redis在Windows下以后台服务一键搭建集群(单机--伪集群)

redis在Windows下以后台服务一键搭建集群(单机--伪集群)

一、概述

此教程介绍如何在windows系统中同一台机器上布置redis伪集群,同时要以后台服务的模式运行。布置以脚本的形式,一键完成。关于在多台机器上用脚本搭建集群参见另一博文《redis在Windows下以后台服务一键搭建集群(多机)》。

二、准备的工具与环境

2.1资源下载

以下准备的工具,想要了解详细过程的,可以逐一按下面的分步说明,一一下载。当然,不想麻烦的,这里有一个包含所有工具的完整的包,直接下载即可,csdn下载链接:http://download.csdn.net/download/mingojiang/10186166

1.下载Redis for windows的最新版本,解压到c:\redis目录下备用。github下载链接:https://github.com/MSOpenTech/redis/releases

2.下载ruby环境RubyInstaller,这是ruby的运行环境。官网下载:http://rubyinstaller.org/downloads/ 我选的是“Ruby 2.3.3 (x64)”。

3,安装 GEM,Redis的ruby支持环境。由于GFW的问题,GEM的源在国内不可用,可以@翻##墙,提供一下免费&&翻@#墙工具,百度网盘下载链接:https://pan.baidu.com/s/1eSiLWo6密码: enfm,下载后需要解压码:edsryu4yu9hgfr34kj098@#^%(*@#FHk43ee6y9p8743。

4,下载 redis-trib.rb ,放到c:\redis目录下备用

github下载链接:https://github.com/antirez/redis/tree/3.2/src

这个是redis集群工作要用到的小工具,ruby语言开发的小工具。

2.2安装环境

1. 安装RubyInstaller

安装2.1章中第2节下载的RubyInstaller,勾选

Install Td/Tk Support

Add Ruby executables to your PATH

Associate .rb and .rbw files with this Ruby installation

redis在Windows下以后台服务一键搭建集群(单机--伪集群)

2.安装 GEM

番@#!墙:运行第2.1章3节中下载的工具

redis在Windows下以后台服务一键搭建集群(单机--伪集群)

显示已经连接成功了,这个时候,你可以直接安装redis与ruby之间通信的一个组件了,直接在命令行输入命令:

gem install redis

提示成功即可。

至此环境准备完闭

三、创建集群脚本

注意:由于中英文可能遇到兼容问题,所以运行脚本时,请把脚本文件放到英文目录下。

关于脚本,嫌麻烦的,通过本人csdn下载链接可直接下载,里面包含了所需的所有资料,下载链接为:http://download.csdn.net/download/mingojiang/10186166

创建一文本“install.bat”,可直接运行此脚本,完成一键安装redis群集。打开脚本编辑,第二行是指定安装的根目录,可自行编辑:

rem 你要安装的盘符,可自行配置

set InstallPath=D:

 

set sourcePath=%~dp0

cd /d %InstallPath%

 

mkdir redis-cluster

cd redis-cluster

mkdir 31001 31002 31003 32001 32002 32003 33001 33002 33003

 

copy %sourcePath%redis-server.exe 31001

copy %sourcePath%redis-server.exe 31002

copy %sourcePath%redis-server.exe 31003

copy %sourcePath%redis-server.exe 32001

copy %sourcePath%redis-server.exe 32002

copy %sourcePath%redis-server.exe 32003

copy %sourcePath%redis-server.exe 33001

copy %sourcePath%redis-server.exe 33002

copy %sourcePath%redis-server.exe 33003

 

copy %sourcePath%EventLog.dll 31001

copy %sourcePath%EventLog.dll 31002

copy %sourcePath%EventLog.dll 31003

copy %sourcePath%EventLog.dll 32001

copy %sourcePath%EventLog.dll 32002

copy %sourcePath%EventLog.dll 32003

copy %sourcePath%EventLog.dll 33001

copy %sourcePath%EventLog.dll 33002

copy %sourcePath%EventLog.dll 33003

 

copy %sourcePath%redis.windows-service.conf 31001

copy %sourcePath%redis.windows-service.conf 31002

copy %sourcePath%redis.windows-service.conf 31003

copy %sourcePath%redis.windows-service.conf 32001

copy %sourcePath%redis.windows-service.conf 32002

copy %sourcePath%redis.windows-service.conf 32003

copy %sourcePath%redis.windows-service.conf 33001

copy %sourcePath%redis.windows-service.conf 33002

copy %sourcePath%redis.windows-service.conf 33003

 

copy %sourcePath%redis-trib.rb

copy %sourcePath%redis.windows-service.conf

copy %sourcePath%redis-cli.exe

copy %sourcePath%startAllRedisService.bat

copy %sourcePath%stopAllRedisService.bat

copy %sourcePath%uninstall.bat

 

rename 31001\redis.windows-service.conf redis.conf

rename 31002\redis.windows-service.conf redis.conf

rename 31003\redis.windows-service.conf redis.conf

rename 32001\redis.windows-service.conf redis.conf

rename 32002\redis.windows-service.conf redis.conf

rename 32003\redis.windows-service.conf redis.conf

rename 33001\redis.windows-service.conf redis.conf

rename 33002\redis.windows-service.conf redis.conf

rename 33003\redis.windows-service.conf redis.conf

 

rem 关闭回显

@echo off

 

rem 定义变量延迟环境

setlocal enabledelayedexpansion

rem 读取所有内容

for /f "eol=* tokens=*" %%i in (31001\redis.conf) do (

rem 设置变量a为每行内容

set a=%%i

rem 如果该行有123,则将其改为456

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

rem 把修改后的全部行存入$

set "a=!a:port 6379=port 31001!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\31001\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

rem 用$的内容替换原来a.txt内容

move $ 31001\redis.conf

 

 

for /f "eol=* tokens=*" %%i in (31002\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 31002!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\31002\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 31002\redis.conf

 

for /f "eol=* tokens=*" %%i in (31003\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 31003!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\31003\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 31003\redis.conf

 

for /f "eol=* tokens=*" %%i in (32001\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 32001!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\32001\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 32001\redis.conf

 

for /f "eol=* tokens=*" %%i in (32002\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 32002!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\32002\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 32002\redis.conf

 

for /f "eol=* tokens=*" %%i in (32003\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 32003!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\32003\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 32003\redis.conf

 

for /f "eol=* tokens=*" %%i in (33001\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 33001!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\33001\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 33001\redis.conf

 

for /f "eol=* tokens=*" %%i in (33002\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 33002!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\33002\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 33002\redis.conf

 

for /f "eol=* tokens=*" %%i in (33003\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 33003!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\33003\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 33003\redis.conf

 

@echo on

 

echo modify redis.conf file finished....

echo begin install redis windows services.....

31001\redis-server.exe --service-install %InstallPath%\redis-cluster\31001\redis.conf --service-name 31001redisM

31002\redis-server.exe --service-install %InstallPath%\redis-cluster\31002\redis.conf --service-name 31002redisM

31003\redis-server.exe --service-install %InstallPath%\redis-cluster\31003\redis.conf --service-name 31003redisM

32001\redis-server.exe --service-install %InstallPath%\redis-cluster\32001\redis.conf --service-name 32001redisM

32002\redis-server.exe --service-install %InstallPath%\redis-cluster\32002\redis.conf --service-name 32002redisM

32003\redis-server.exe --service-install %InstallPath%\redis-cluster\32003\redis.conf --service-name 32003redisM

33001\redis-server.exe --service-install %InstallPath%\redis-cluster\33001\redis.conf --service-name 33001redisM

33002\redis-server.exe --service-install %InstallPath%\redis-cluster\33002\redis.conf --service-name 33002redisM

33003\redis-server.exe --service-install %InstallPath%\redis-cluster\33003\redis.conf --service-name 33003redisM

 

echo install redis windows services finished.....

 

echo begin start all the redis as windows service......

31001\redis-server.exe --service-start --service-name 31001redisM

31002\redis-server.exe --service-start --service-name 31002redisM

31003\redis-server.exe --service-start --service-name 31003redisM

32001\redis-server.exe --service-start --service-name 32001redisM

32002\redis-server.exe --service-start --service-name 32002redisM

32003\redis-server.exe --service-start --service-name 32003redisM

33001\redis-server.exe --service-start --service-name 33001redisM

33002\redis-server.exe --service-start --service-name 33002redisM

33003\redis-server.exe --service-start --service-name 33003redisM

echo start all the redis as windows service finished....

 

echo begin create redis cluster......

redis-trib.rb create --replicas 2 127.0.0.1:31001 127.0.0.1:32001 127.0.0.1:33001 127.0.0.1:32002 127.0.0.1:33002 127.0.0.1:31002 127.0.0.1:33003 127.0.0.1:31003 127.0.0.1:32003

echo  create redis cluster finished....

 

echo off

 

rem 延时20秒,自动关闭窗口

ping -n 20 127.0>nul

四、卸载集群脚本

对安装后的集群服务,如果想要卸载,流程是,先停止所有节点服务,然后再反注册即可。创建一个文本,命名“uninstall.bat”,然后编辑脚本:

 

echo begin stop all the redis as windows service......

 

%~dp031001\redis-server.exe --service-stop --service-name 31001redisM

%~dp031002\redis-server.exe --service-stop --service-name 31002redisM

%~dp031003\redis-server.exe --service-stop --service-name 31003redisM

%~dp032001\redis-server.exe --service-stop --service-name 32001redisM

%~dp032002\redis-server.exe --service-stop --service-name 32002redisM

%~dp032003\redis-server.exe --service-stop --service-name 32003redisM

%~dp033001\redis-server.exe --service-stop --service-name 33001redisM

%~dp033002\redis-server.exe --service-stop --service-name 33002redisM

%~dp033003\redis-server.exe --service-stop --service-name 33003redisM

 

echo stop all the redis as windows service finished....

 

echo "unistall redis service....."

%~dp031001\redis-server.exe --service-uninstall --service-name 31001redisM

%~dp031002\redis-server.exe --service-uninstall --service-name 31002redisM

%~dp031003\redis-server.exe --service-uninstall --service-name 31003redisM

%~dp032001\redis-server.exe --service-uninstall --service-name 32001redisM

%~dp032002\redis-server.exe --service-uninstall --service-name 32002redisM

%~dp032003\redis-server.exe --service-uninstall --service-name 32003redisM

%~dp033001\redis-server.exe --service-uninstall --service-name 33001redisM

%~dp033002\redis-server.exe --service-uninstall --service-name 33002redisM

%~dp033003\redis-server.exe --service-uninstall --service-name 33003redisM

 

echo "uninstall redis service finished"

 

echo off

rem 延时20秒,自动关闭窗口

ping -n 5 127.0>nul

五、停止集群节点脚本

有时想要停止所有节点服务,在服务管理器中,一个一个手动停止也是一种办法,但是麻烦,一个一个操作,费事,可通过脚本一键停止,创建一个文本,命名“stopAllRedisService.bat”,编辑脚本:

echo begin stop all the redis as windows service......

 

%~dp031001\redis-server.exe --service-stop --service-name 31001redisM

%~dp031002\redis-server.exe --service-stop --service-name 31002redisM

%~dp031003\redis-server.exe --service-stop --service-name 31003redisM

%~dp032001\redis-server.exe --service-stop --service-name 32001redisM

%~dp032002\redis-server.exe --service-stop --service-name 32002redisM

%~dp032003\redis-server.exe --service-stop --service-name 32003redisM

%~dp033001\redis-server.exe --service-stop --service-name 33001redisM

%~dp033002\redis-server.exe --service-stop --service-name 33002redisM

%~dp033003\redis-server.exe --service-stop --service-name 33003redisM

 

echo stop all the redis as windows service finished....

 

echo off

 

rem 延时20秒,自动关闭窗口

ping -n 5 127.0>nul

六、启动集群脚本脚本

有时想要启动所有节点服务,在服务管理器中,一个一个手动启动也是一种办法,但是麻烦,一个一个操作,费事,可通过脚本一键启动,创建一个文本,命名“startAllRedisService.bat”,编辑脚本:

echo begin start all the redis as windows service......

 

%~dp031001\redis-server.exe --service-start --service-name 31001redisM

%~dp031002\redis-server.exe --service-start --service-name 31002redisM

%~dp031003\redis-server.exe --service-start --service-name 31003redisM

%~dp032001\redis-server.exe --service-start --service-name 32001redisM

%~dp032002\redis-server.exe --service-start --service-name 32002redisM

%~dp032003\redis-server.exe --service-start --service-name 32003redisM

%~dp033001\redis-server.exe --service-start --service-name 33001redisM

%~dp033002\redis-server.exe --service-start --service-name 33002redisM

%~dp033003\redis-server.exe --service-start --service-name 33003redisM

 

echo start all the redis as windows service finished....

 

echo off

 

rem 延时20秒,自动关闭窗口

ping -n 5 127.0>nul

七、设置集群密码

Redis是通过简单的密码来保证安全的,如果想开放给外网环境,那需要给配置密码,密码最长为256个字符,给集群配置密码,有几种方法,一种通过修改redis-trib.rb脚本的代码可以实现,这种方法,一般不适合初学者使用,对于初学者建议采用先搭建无密码的集群,再给集群通过命令加密的方式。或者直接用脚本,一键安装,实现逻辑,也是先无密码搭建,然后通过脚本把服务停止,脚本方式增加密码,然后再启动服务的方式,在此介绍这两种方法。

方法一

先按以上方法,创建集群,然后通过redis-cli.exe工具遂一连接各个redis节点:然后执行以下四个命令。注意,密码必须一样,才能保证集群相互交互成功。

config set masterauth abc  

config set requirepass abc  

auth abc

config rewrite  

方法二

直接通过脚本,一键安装,创建文本文件,重命名为“install-auth.bat”,也就是说以上第三章节的脚本,以以下脚本替换。

rem 你要安装的盘符,可自行配置

set InstallPath=D:

 

rem 给你的集群加上密码,可自行配置

set auth_password=#disiswnin((ndw0rksinm0stP0SIXsyst#mslik#Linux@*BSD@0ux(nd0SX(r#th#tw00p#r(tingsyst#

 

set sourcePath=%~dp0

cd /d %InstallPath%

 

mkdir redis-cluster

cd redis-cluster

mkdir 31001 31002 31003 32001 32002 32003 33001 33002 33003

 

copy %sourcePath%redis-server.exe 31001

copy %sourcePath%redis-server.exe 31002

copy %sourcePath%redis-server.exe 31003

copy %sourcePath%redis-server.exe 32001

copy %sourcePath%redis-server.exe 32002

copy %sourcePath%redis-server.exe 32003

copy %sourcePath%redis-server.exe 33001

copy %sourcePath%redis-server.exe 33002

copy %sourcePath%redis-server.exe 33003

 

copy %sourcePath%EventLog.dll 31001

copy %sourcePath%EventLog.dll 31002

copy %sourcePath%EventLog.dll 31003

copy %sourcePath%EventLog.dll 32001

copy %sourcePath%EventLog.dll 32002

copy %sourcePath%EventLog.dll 32003

copy %sourcePath%EventLog.dll 33001

copy %sourcePath%EventLog.dll 33002

copy %sourcePath%EventLog.dll 33003

 

copy %sourcePath%redis.windows-service.conf 31001

copy %sourcePath%redis.windows-service.conf 31002

copy %sourcePath%redis.windows-service.conf 31003

copy %sourcePath%redis.windows-service.conf 32001

copy %sourcePath%redis.windows-service.conf 32002

copy %sourcePath%redis.windows-service.conf 32003

copy %sourcePath%redis.windows-service.conf 33001

copy %sourcePath%redis.windows-service.conf 33002

copy %sourcePath%redis.windows-service.conf 33003

 

copy %sourcePath%redis-trib.rb

copy %sourcePath%redis.windows-service.conf

copy %sourcePath%redis-cli.exe

copy %sourcePath%startAllRedisService.bat

copy %sourcePath%stopAllRedisService.bat

copy %sourcePath%uninstall.bat

 

rename 31001\redis.windows-service.conf redis.conf

rename 31002\redis.windows-service.conf redis.conf

rename 31003\redis.windows-service.conf redis.conf

rename 32001\redis.windows-service.conf redis.conf

rename 32002\redis.windows-service.conf redis.conf

rename 32003\redis.windows-service.conf redis.conf

rename 33001\redis.windows-service.conf redis.conf

rename 33002\redis.windows-service.conf redis.conf

rename 33003\redis.windows-service.conf redis.conf

 

 

rem 关闭回显

@echo off

 

rem 定义变量延迟环境

setlocal enabledelayedexpansion

rem 读取所有内容

for /f "eol=* tokens=*" %%i in (31001\redis.conf) do (

rem 设置变量a为每行内容

set a=%%i

rem 如果该行有123,则将其改为456

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

rem 把修改后的全部行存入$

set "a=!a:port 6379=port 31001!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\31001\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

rem 用$的内容替换原来a.txt内容

move $ 31001\redis.conf

 

 

for /f "eol=* tokens=*" %%i in (31002\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 31002!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\31002\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 31002\redis.conf

 

for /f "eol=* tokens=*" %%i in (31003\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 31003!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\31003\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 31003\redis.conf

 

for /f "eol=* tokens=*" %%i in (32001\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 32001!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\32001\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 32001\redis.conf

 

for /f "eol=* tokens=*" %%i in (32002\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 32002!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\32002\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 32002\redis.conf

 

for /f "eol=* tokens=*" %%i in (32003\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 32003!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\32003\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 32003\redis.conf

 

for /f "eol=* tokens=*" %%i in (33001\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 33001!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\33001\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 33001\redis.conf

 

for /f "eol=* tokens=*" %%i in (33002\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 33002!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\33002\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 33002\redis.conf

 

for /f "eol=* tokens=*" %%i in (33003\redis.conf) do (

set a=%%i

set "a=!a:# cluster-enabled yes=cluster-enabled yes!"

set "a=!a:port 6379=port 33003!"

set "a=!a:dir ./=dir %InstallPath%\redis-cluster\33003\!"

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

echo !a!>>$)

move $ 33003\redis.conf

 

@echo on

 

echo modify redis.conf file finished....

echo begin install redis windows services.....

31001\redis-server.exe --service-install %InstallPath%\redis-cluster\31001\redis.conf --service-name 31001redisM

31002\redis-server.exe --service-install %InstallPath%\redis-cluster\31002\redis.conf --service-name 31002redisM

31003\redis-server.exe --service-install %InstallPath%\redis-cluster\31003\redis.conf --service-name 31003redisM

32001\redis-server.exe --service-install %InstallPath%\redis-cluster\32001\redis.conf --service-name 32001redisM

32002\redis-server.exe --service-install %InstallPath%\redis-cluster\32002\redis.conf --service-name 32002redisM

32003\redis-server.exe --service-install %InstallPath%\redis-cluster\32003\redis.conf --service-name 32003redisM

33001\redis-server.exe --service-install %InstallPath%\redis-cluster\33001\redis.conf --service-name 33001redisM

33002\redis-server.exe --service-install %InstallPath%\redis-cluster\33002\redis.conf --service-name 33002redisM

33003\redis-server.exe --service-install %InstallPath%\redis-cluster\33003\redis.conf --service-name 33003redisM

 

echo install redis windows services finished.....

 

echo begin start all the redis as windows service......

31001\redis-server.exe --service-start --service-name 31001redisM

31002\redis-server.exe --service-start --service-name 31002redisM

31003\redis-server.exe --service-start --service-name 31003redisM

32001\redis-server.exe --service-start --service-name 32001redisM

32002\redis-server.exe --service-start --service-name 32002redisM

32003\redis-server.exe --service-start --service-name 32003redisM

33001\redis-server.exe --service-start --service-name 33001redisM

33002\redis-server.exe --service-start --service-name 33002redisM

33003\redis-server.exe --service-start --service-name 33003redisM

echo start all the redis as windows service finished....

 

echo begin create redis cluster......

redis-trib.rb create --replicas 2 127.0.0.1:31001 127.0.0.1:32001 127.0.0.1:33001 127.0.0.1:32002 127.0.0.1:33002 127.0.0.1:31002 127.0.0.1:33003 127.0.0.1:31003 127.0.0.1:32003

echo  create redis cluster finished....

 

 

echo begin stop all the redis as windows service......

31001\redis-server.exe --service-stop --service-name 31001redisM

31002\redis-server.exe --service-stop --service-name 31002redisM

31003\redis-server.exe --service-stop --service-name 31003redisM

32001\redis-server.exe --service-stop --service-name 32001redisM

32002\redis-server.exe --service-stop --service-name 32002redisM

32003\redis-server.exe --service-stop --service-name 32003redisM

33001\redis-server.exe --service-stop --service-name 33001redisM

33002\redis-server.exe --service-stop --service-name 33002redisM

33003\redis-server.exe --service-stop --service-name 33003redisM

echo stop all the redis as windows service finished....

 

echo begin add password to the windows service redis......

 

@echo off

 

rem 读取所有内容

for /f "eol=* tokens=*" %%i in (31001\redis.conf) do (

rem 设置变量a为每行内容

set a=%%i

rem 如果该行有123,则将其改为456

set "a=!a:# requirepass foobared=requirepass %auth_password%!"

set "a=!a:# masterauth <master-password>=masterauth %auth_password%!"

echo !a!>>$)

rem 用$的内容替换原来a.txt内容

move $ 31001\redis.conf

 

 

for /f "eol=* tokens=*" %%i in (31002\redis.conf) do (

set a=%%i

set "a=!a:# cluster-config-file nodes-6379.conf=cluster-config-file nodes.conf!"

set "a=!a:# requirepass foobared=requirepass %auth_password%!"

set "a=!a:# masterauth <master-password>=masterauth %auth_password%!"

echo !a!>>$)

move $ 31002\redis.conf

 

for /f "eol=* tokens=*" %%i in (31003\redis.conf) do (

set a=%%i

set "a=!a:# requirepass foobared=requirepass %auth_password%!"

set "a=!a:# masterauth <master-password>=masterauth %auth_password%!"

echo !a!>>$)

move $ 31003\redis.conf

 

for /f "eol=* tokens=*" %%i in (32001\redis.conf) do (

set a=%%i

set "a=!a:# requirepass foobared=requirepass %auth_password%!"

set "a=!a:# masterauth <master-password>=masterauth %auth_password%!"

echo !a!>>$)

move $ 32001\redis.conf

 

for /f "eol=* tokens=*" %%i in (32002\redis.conf) do (

set a=%%i

set "a=!a:# requirepass foobared=requirepass %auth_password%!"

set "a=!a:# masterauth <master-password>=masterauth %auth_password%!"

echo !a!>>$)

move $ 32002\redis.conf

 

for /f "eol=* tokens=*" %%i in (32003\redis.conf) do (

set a=%%i

set "a=!a:# requirepass foobared=requirepass %auth_password%!"

set "a=!a:# masterauth <master-password>=masterauth %auth_password%!"

echo !a!>>$)

move $ 32003\redis.conf

 

for /f "eol=* tokens=*" %%i in (33001\redis.conf) do (

set a=%%i

set "a=!a:# requirepass foobared=requirepass %auth_password%!"

set "a=!a:# masterauth <master-password>=masterauth %auth_password%!"

echo !a!>>$)

move $ 33001\redis.conf

 

for /f "eol=* tokens=*" %%i in (33002\redis.conf) do (

set a=%%i

set "a=!a:# requirepass foobared=requirepass %auth_password%!"

set "a=!a:# masterauth <master-password>=masterauth %auth_password%!"

echo !a!>>$)

move $ 33002\redis.conf

 

for /f "eol=* tokens=*" %%i in (33003\redis.conf) do (

set a=%%i

set "a=!a:# requirepass foobared=requirepass %auth_password%!"

set "a=!a:# masterauth <master-password>=masterauth %auth_password%!"

echo !a!>>$)

move $ 33003\redis.conf

 

@echo on

 

echo end add password to the windows service redis......

 

 

echo begin start all the redis as windows service......

 

31001\redis-server.exe --service-start --service-name 31001redisM

31002\redis-server.exe --service-start --service-name 31002redisM

31003\redis-server.exe --service-start --service-name 31003redisM

32001\redis-server.exe --service-start --service-name 32001redisM

32002\redis-server.exe --service-start --service-name 32002redisM

32003\redis-server.exe --service-start --service-name 32003redisM

33001\redis-server.exe --service-start --service-name 33001redisM

33002\redis-server.exe --service-start --service-name 33002redisM

33003\redis-server.exe --service-start --service-name 33003redisM

 

echo start all the redis as windows service finished....

 

 

echo off

 

rem 延时30秒,自动关闭窗口

ping -n 30 127.0>nul

八、集群的测试、运维

集群搭建后,需要测试,调试,运维等,涉及到很多的命令与方法,请参加另一博文:http://blog.csdn.net/mingojiang/article/details/78902370