redis系列(1)之安装和集群部署

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenjianandiyi/article/details/60575492

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.9
sudo 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方案。于是分配三个主机。

扫描二维码关注公众号,回复: 3187036 查看本文章

角色分配为

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 当前主机的ip
protected-mode no  # 取消掉保护模式
timeout 60
port 16661                                  # redis服务端口,
daemonize yes
logfile /opt/redis-mss/redis.log              # 日志目录,
dbfilename dump.rdb   # db文件的名字
dir /opt/redis-mss      # db文件所在的目录,最终db文件在/opt/redis-mss/dump.rdb
slave的redis.conf部分配置如下

配置slave

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

添加下面两行

daemonize yes
logfile /opt/redis-mss/sentinel.log

修改

protected-mode no  # 取消掉保护模式
port 26379
dir /opt/redis-mss
# sentinel monitor <master-name><ip><redis-port><quorum>
# quorum 表示要多少台sentinel 主观判断master断开时,执行auto-failover
sentinel 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目录下

我的官网
我的博客

我的官网http://guan2ye.com
我的CSDN地址http://blog.csdn.net/chenjianandiyi
我的简书地址http://www.jianshu.com/u/9b5d1921ce34
我的githubhttps://github.com/javanan
我的码云地址https://gitee.com/jamen/
阿里云优惠券https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=vf2b5zld&utm_source=vf2b5zld

阿里云教程系列网站http://aliyun.guan2ye.com

1.png

我的开源项目spring boot 搭建的一个企业级快速开发脚手架

1.jpg

猜你喜欢

转载自blog.csdn.net/chenjianandiyi/article/details/60575492