redis集群

来源:互联网 发布:2020软件 编辑:程序博客网 时间:2024/06/05 00:41

ubuntu下安裝

1. 下载Redis的安装包(x.x.x表示版本)

wget http://download.redis.io/releases/redis-x.x.x.tar.gz

2.在目录下,解压按照包,生成新的目录redis-x.x.x

tar xvfz redis-x.x.x.tar.gz

3. 进入解压之后的目录,进行编译

cd redis-2.8.9sudo make//如果没有明显的错误,则表示编译成功

4.安装

sudo make install

5.测试是否安装成功

sudo make test

6.启动redis服务

进入redis的目录,再进入src目录。

./redis-server 

这时候是用了默认的配置
如果要用自己的配置可以在后面加入配置文件路径

./redis-server ../redis.conf 

查看日志

 src/redis-server ../redis.conf  > ../logs/log.out 2>&1 &查看redis日志(或者配置文件中配置)tail logs/log.out

7.连接redis服务

./redis-cli

如果用密码,则–》auth password

密码的设置在conf文件中–》requirepass foobared 改为自己的密码即可。

8.java测试连接

使用jredis.jar

    @Test    public void link(){        //连接服务器的 Redis 服务        Jedis jedis = new Jedis("192.168.247.133");        jedis.auth("redis");        System.out.println("Connection to server sucessfully");        //查看服务是否运行        System.out.println("Server is running: "+jedis.ping());    }

9.密码设置

redis.conf 中requirepass foobared 改为自己的密码
jedis.auth(“redis”); 表示redis的密码如果不设置,默认会报错

redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions:  1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.  2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server.   3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.按照要求修改配置文件即可,但是启动的时候记得加入配置文件路径。

安装方式二

在 Ubuntu 系统安装 Redi 可以使用以下命令:

$sudo apt-get update$sudo apt-get install redis-server

这里写图片描述

集群配置

1.Redis官方集群方案 Redis Cluster

3.0版本后才开始支持集群案

2.Redis Sharding(分片)集群

master-slave-sentinel方案

redis的方案采用master-slave-sentinel方案。于是分配三个主机。

角色分配为

master: 192.168.1.100:6379

slave: 192.168.1.101:6380,192.168.1.102:6381

sentinel: 192.168.1.100:26379, 192.168.1.101:26380 , 192.168.1.102:2681

1 主 2 从 3 哨兵

3.1 防火墙

三台主机开通tcp策略

-A INPUT -m state —state NEW -m tcp -p tcp —dport16661 -j ACCEPT

-A INPUT -m state —state NEW -m tcp -p tcp —dport16662 -j ACCEPT

3.2 创建工作目录

在每台主机上创建目录mkdir –p /opt/redis-xx

3.3 配置文件

u 3台主机各需要一份redis.conf文件u 3台主机也需要一份sentinel.conf配置文件将redis-3.2.4/下的redis.conf 拷贝到各个主机工作目录(/opt/redis- xx /)cp /opt/redis-3.2.4/redis.conf /opt/redis-xx/ xxxxxxxxxx将redis-3.2.4/下的sentinel.conf 拷贝到各个主机工作目录(/opt/redis-mss/)cp /opt/redis-3.2.4/sentinel.conf /opt/redis-xx/master的redis.conf部分配置如下

配置master

bind 192.16.1.100     #bind 当前主机的ipprotected-mode no  # 取消掉保护模式timeout 60port 16661                                  # redis服务端口,daemonize yeslogfile /opt/redis-mss/redis.log              # 日志目录,dbfilename dump.rdb   # db文件的名字dir /opt/redis-mss      # db文件所在的目录,最终db文件在/opt/redis-mss/dump.rdbslave的redis.conf部分配置如下

配置slave

protected-mode no  # 取消掉保护模式timeout 60port 16661                                  # redis服务端口,daemonize yeslogfile /opt/redis-mss/redis.log           # 日志目录,dbfilename dump.rdb      # db文件的名字dir /opt/redis-mss         # db文件所在的目录,最终db文件在/opt/redis-mss/dump.rdbslaveof 192.168.1.100 6379    # 填写master的ip和portsentinel的sentinel.conf的部分配置如下

添加下面两行

daemonize yeslogfile /opt/redis-mss/sentinel.log

修改

protected-mode no  # 取消掉保护模式port 26379dir /opt/redis-mss# sentinel monitor <master-name><ip><redis-port><quorum># quorum 表示要多少台sentinel 主观判断master断开时,执行auto-failoversentinel monitor mymaster 192.168.1.100 6379 2 # sentinel down-after-milliseconds <master-name><milliseconds># 多少毫秒内会认为master挂了,默认30s,sentinel down-after-milliseconds mymaster 5000

需要注意的是,img这个2表示3台sentinel中有2台主观认为master宕机时,就执行自动主从切换(auto-failover)。这个值应该尽可能靠近sentinel总数的50%。

3.5 启动

第一次启动集群先要启动master/opt/redis-3.2.4/src/redis-server /opt/redis-xx/redis.conf然后启动slav(117, 118)/opt/redis-3.2.4/src/redis-server /opt/redis-xx/redis.conf最后启动sentinel/opt/redis-3.2.4/src/redis-server /opt/redis-mss/sentinel.conf --sentinel各个节点的日志都会放在相应的/opt/redis-xx目录下
0 0