简单搭建redis 主从服务器集群

**配置机器1**
1 在192.168.157.128上进⼊Desktop⽬录,创建conf⽬录
2 在conf⽬录下创建⽂件7000.conf,编辑内容如下
	port 7000
	bind 192.168.157.128
	daemonize yes
	pidfile 7000.pid
	cluster-enabled yes
	cluster-config-file 7000_node.conf
	cluster-node-timeout 15000
	appendonly yes
3 在conf⽬录下创建⽂件7001.conf,编辑内容如下
	port 7001
	bind 192.168.157.128
	daemonize yes
	pidfile 7001.pid
	cluster-enabled yes
	cluster-config-file 7001_node.conf
	cluster-node-timeout 15000
	appendonly yes
4 在conf⽬录下创建⽂件7002.conf,编辑内容如下
	port 7002
	bind 192.168.157.128
	daemonize yes
	pidfile 7002.pid
	cluster-enabled yes
	cluster-config-file 7002_node.conf
	cluster-node-timeout 15000
	appendonly yes
总结:这三个文件的配置区别只有port、pidfile、cluster-config-file三项
使用配置文件启动redis服务
	redis-server 7000.conf
	redis-server 7001.conf
	redis-server 7002.conf

配置机器2
1 在192.168.157.127上进⼊Desktop⽬录,创建conf⽬录
2 在conf⽬录下创建⽂件7003.conf,编辑内容如下
port 7003
bind 192.168.157.128
daemonize yes
pidfile 7003.pid
cluster-enabled yes
cluster-config-file 7003_node.conf
cluster-node-timeout 15000
appendonly yes
3 在conf⽬录下创建⽂件7004.conf,编辑内容如下
port 7004
bind 192.168.157.128
daemonize yes
pidfile 7004.pid
cluster-enabled yes
cluster-config-file 7004_node.conf
cluster-node-timeout 15000
appendonly yes
4 在conf⽬录下创建⽂件7005.conf,编辑内容如下
port 7005
bind 192.168.157.128
daemonize yes
pidfile 7005.pid
cluster-enabled yes
cluster-config-file 7005_node.conf
cluster-node-timeout 15000
appendonly yes
总结:这三个文件的配置区别只有port、pidfile、cluster-config-file三项
使用配置文件启动redis服务
redis-server 7003.conf
redis-server 7004.conf
redis-server 7005.conf

创建集群

1 redis的安装包中包含了redis-trib.rb,⽤于创建集群  //ruby
2 接下来的操作在192.168.157.128机器上进⾏
3 将命令复制,这样可以在任何⽬录下调⽤此命令

sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/
若提示 目录不存在,自行按提示安装 ruby,及其所需其他组件

4 安装ruby环境,因为redis-trib.rb是⽤ruby开发的

sudo apt-get install ruby

** 运⾏如下命令创建集群**
redis-trib.rb create --replicas 1 192.168.157.128:7000 192.168.157.128:7001 192.168.157.128:7002 192.168.157.127:7003 192.168.157.127:7004 192.168.157.127:7005

go语言redis-cluster开源客户端

安装:
go get github.com/gitstliu/go-redis-cluster
示例代码

安装:
go get github.com/gitstliu/go-redis-cluster
示例代码
func (this*ClusterController)Get(){
	cluster, _ := redis.NewCluster(
		&redis.Options{
			StartNodes: []string{"192.168.157.128:7000", "192.168.157.128:7001", "192.168.157.128:7002","192.168.157.127:7003","192.168.157.127:7004","192.168.157.127:7005"},
			ConnTimeout: 50 * time.Millisecond,
			ReadTimeout: 50 * time.Millisecond,
			WriteTimeout: 50 * time.Millisecond,
			KeepAlive: 16,
			AliveTime: 60 * time.Second,
		})
	cluster.Do("set","name","itheima")

	name,_ := redis.String(cluster.Do("get","name"))
	beego.Info(name)
	this.Ctx.WriteString("集群创建成功")
}

你可能感兴趣的:(golang,redis)