redis sentinel搭建以及在jedis中使用

一、redis主从搭建

1、搭建redis master

1>redis安装

mkdir -p /usr/local/webserver/redis  //安装目录
cd /usr/local/webserver/redis
wget http://download.redis.io/redis-stable.tar.gz //最新稳定版
tar xzf redis-stable.tar.gz
cd redis-stable
make     //报错的话 install gcc
make install //redis可执行文件redis-cli,redis-server等会被复制到/usr/local/bin,命令行直接输入即可执行

2>主配置文件redis_master.conf做如下修改:

daemonize   yes   //使redis能以守护进程模式运行
pidfile   /var/run/redis_6379.pid   //设置redis PID 文件位置
port  6379
dir   /var/redis/6379   //设置持久化文件存放位置

3>启动master

[[email protected] redis-stable]# redis-server redis_master.conf
[[email protected]-sentinel redis-stable]# ps -ef|grep redis
root      5504     1  0 12:41 ?        00:00:00 redis-server 127.0.0.1:6379
root      5508  1361  0 12:41 pts/0    00:00:00 grep redis

4>使用客户端连接测试

[[email protected] redis-stable]# redis-cli -p 6379
127.0.0.1:6379> set ww 1
OK
127.0.0.1:6379> get ww
"1"

2、搭建2个redis slave

1>从配置文件redis_slave_6380.conf,redis_slave_6381.conf做如下修改:

daemonize   yes   //使redis能以守护进程模式运行
pidfile   /var/run/redis_6380.pid   //设置redis PID 文件位置
port  6380
dir   /var/redis/6380   //设置持久化文件存放位置
slaveof 127.0.0.1 6379  //设置属于哪个master
daemonize   yes   //使redis能以守护进程模式运行
pidfile   /var/run/redis_6381.pid   //设置redis PID 文件位置
port  6381
dir   /var/redis/6381   //设置持久化文件存放位置
slaveof 127.0.0.1 6379  //设置属于哪个master

2>启动slave并测试数据是否同步

[[email protected] redis-stable]# redis-server redis_slave_6380.conf
[[email protected]-sentinel redis-stable]# ps -ef|grep redis
root      5504     1  0 12:41 ?        00:00:02 redis-server 127.0.0.1:6379
root      5555     1  0 14:47 ?        00:00:00 redis-server 127.0.0.1:6380
root      5561  1361  0 14:48 pts/0    00:00:00 grep redis
[[email protected]-sentinel redis-stable]# redis-cli -p 6380
127.0.0.1:6380> get ww
"1"     //数据已经同步过来
[[email protected] redis-stable]# redis-server redis_slave_6381.conf
[[email protected]-sentinel redis-stable]# ps -ef|grep redis
root      5504     1  0 12:41 ?        00:00:02 redis-server 127.0.0.1:6379
root      5555     1  0 14:47 ?        00:00:00 redis-server 127.0.0.1:6380
root      5557     1  0 14:48 ?        00:00:00 redis-server 127.0.0.1:6381
root      5561  1361  0 14:48 pts/0    00:00:00 grep redis
[[email protected]-sentinel redis-stable]# redis-cli -p 6381
127.0.0.1:6380> get ww
"1"     //数据已经同步过来

二、Redis容灾部署(哨兵Sentinel)

1、三个哨兵文件配置

////sentinel_26379.conf
port 26379
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel myid 1c805b9134364d69986f9042c22e95debf1932c9

//sentinel_26380.conf
port 26380
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel myid ac1ef015411583d4b9f3d81cee830060b2f29862

//sentinel_26381.conf
port 26381
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel myid 0aca3a57038e2907c8a07be2b3c0d15171e44da5

2、启动sentinel,并查看sentinel所监控的master和slave

[[email protected] redis-stable]# redis-sentinel  sentinel_26379.conf
[[email protected]-sentinel redis-stable]# redis-sentinel  sentinel_26380.conf
[[email protected]-sentinel redis-stable]# redis-sentinel  sentinel_26381.conf

[[email protected]-sentinel redis-stable]# ps -ef|grep redis
root      5504     1  0 12:41 ?        00:00:03 redis-server 127.0.0.1:6379
root      5555     1  0 14:47 ?        00:00:00 redis-server 127.0.0.1:6380
root      5606     1  0 15:23 ?        00:00:00 redis-server 127.0.0.1:6381
root      5770  1361  0 15:54 pts/0    00:00:00 redis-sentinel *:26379 [sentinel]
root      5773  5429  0 15:54 pts/1    00:00:00 redis-sentinel *:26380 [sentinel]
root      5776  5710  0 15:55 pts/2    00:00:00 redis-sentinel *:26381 [sentinel]
root      5781  5743  0 15:57 pts/3    00:00:00 grep redis

[[email protected]-sentinel redis-stable]# redis-cli -p 26379
127.0.0.1:26379> sentinel masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "127.0.0.1"
    5) "port"
    6) "6379"
    7) "runid"
    8) "83491622b984fa6bbb4cd7761c77b23491569b1b"
    9) "flags"
   10) "master"
   ...

127.0.0.1:26379> SENTINEL slaves mymaster
1)  1) "name"
    2) "127.0.0.1:6380"
    3) "ip"
    4) "127.0.0.1"
    5) "port"
    6) "6380"
    7) "runid"
    8) "e532cc9e510bf93a6c3dd5d978be8364c91c616f"
    9) "flags"
   10) "slave"
   ...
2)  1) "name"
    2) "127.0.0.1:6381"
    3) "ip"
    4) "127.0.0.1"
    5) "port"
    6) "6381"
    7) "runid"
    8) "f7cee9c9f6e5a346d38f2a09dfc9eb4846118ae5"
    9) "flags"
   10) "slave"
   ...

127.0.0.1:26379> SENTINEL get-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6379"

[[email protected]-sentinel redis-stable]# kill -9 5504
[[email protected]-sentinel redis-stable]# ps -ef|grep redis
root      5555     1  0 14:47 ?        00:00:00 redis-server 127.0.0.1:6380
root      5606     1  0 15:23 ?        00:00:00 redis-server 127.0.0.1:6381
root      5770  1361  0 15:54 pts/0    00:00:00 redis-sentinel *:26379 [sentinel]
root      5773  5429  0 15:54 pts/1    00:00:00 redis-sentinel *:26380 [sentinel]
root      5776  5710  0 15:55 pts/2    00:00:00 redis-sentinel *:26381 [sentinel]
root      5789  5743  0 15:59 pts/3    00:00:00 grep redis
//主库出现故障时自动将从库转换为主库
[[email protected]-sentinel redis-stable]# redis-cli -p 26379
127.0.0.1:26379> SENTINEL get-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6380"

三、jedis中使用哨兵,具体代码参考haixuanweb

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans">
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
         <property name="maxTotal" value="1000"/>
         <property name="maxIdle" value="10"/>
         <property name="minIdle" value="1"/>
         <property name="maxWaitMillis" value="30000"/>
         <property name="testOnBorrow" value="true"/>
         <property name="testOnReturn" value="true"/>
         <property name="testWhileIdle" value="true"/>
    </bean>

    <bean id="cacheService" class="sentinel.CacheServiceImpl" destroy-method="destroy">
        <property name="jedisSentinlePool">
            <bean class="redis.clients.jedis.JedisSentinelPool">
                 <constructor-arg index="0" value="mymaster" />
                 <constructor-arg index="1">
                     <set>
                         <value>192.168.13.128:26379</value>
                         <value>192.168.13.128:26380</value>
                         <value>192.168.13.128:26381</value>
                     </set>
                 </constructor-arg>
                 <constructor-arg index="2" ref="jedisPoolConfig" />
            </bean>
        </property>
    </bean>
</beans>
public class CacheServiceImpl implements CacheService {

    private static Logger logger = LoggerFactory.getLogger(CacheServiceImpl.class);

    private JedisSentinelPool jedisSentinlePool;

    public void set(String key, String value) {
        JedisSentinelPool pool = getJedisSentinelPool();
        Jedis jedis = pool.getResource();
        try {
            jedis.set(key, value);
        } catch (Exception e) {
            pool.returnBrokenResource(jedis);
        }finally {
            if(pool!=null){
                pool.returnResource(jedis);
            }
        }
    }
}
public interface CacheService {
    public void set(String key, String value);
}
@Controller
public class GoodsController extends BaseNoLoginController {
    @Autowired
    private CacheService cacheService;
    ...
}
时间: 2024-08-17 20:01:58

redis sentinel搭建以及在jedis中使用的相关文章

Redis服务器搭建/配置/及Jedis客户端的使用方法

Redis服务器搭建 安装 在命令行执行下面的命令: $ wget http://download.redis.io/releases/redis-2.8.13.tar.gz $ tar xzf redis-2.8.13.tar.gz $ cd redis-2.8.13 $ make 编译完成后,会产生六个文件: redis-server:这个是redis的服务器 redis-cli:这个是redis的客户端 redis-check-aof:这个是检查AOF文件的工具 redis-check-d

Redis Sentinel(哨兵详解)

摘要:Redis Sentinel(哨兵):集群解决方案 官方文档(英文) http://redis.io/topics/sentinel .Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务: 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常. 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应

Redis(2)用jedis实现在java中使用redis

昨天已经在windows环境下安装使用了redis. 下面准备在java项目中测试使用redis. redis官网推荐使用jedis来访问redis.所以首先准备了jedis的jar包,以及需要依赖的jar包. commons-pool2-2.3 hamcrest-core-1.3 jedis-2.7.2.jar 因为redis也是属于一种数据库,也是对数据的访问,所以把他放置在dao层,与service分开 import redis.clients.jedis.Jedis; import re

2278棋牌源码控制输赢,Redis Sentinel h5房卡斗牛棋牌平台出租高可用服务架构搭建

前几天,看到一篇H5房卡斗牛平台,H5房卡斗牛平台租赁,想法说得很好,但是没有建设过程,本文记录了Redis哨兵高av可服务性服务体系构建过程.    根据作者的第四个计划,        1.ReDIS是一个完全开源的.免费的.符合BSD的.高性能的密钥值数据库.    ReDIS和其他关键值缓存产品具有以下三个特征:    Redis支持数据持久性,将内存中的数据保存到磁盘,并在重新启动时再次加载.Redis不仅支持简单的键值数据,还支持列表.集合.zset.散列和其他数据结构.Redis支

Redis Sentinel高可用架构

Redis目前高可用的架构非常多,比如keepalived+redis,redis cluster,twemproxy,codis,这些架构各有优劣,今天暂且不说这些架构,今天主要说说redis sentinel高可用架构. 它的主要功能有以下几点 不时地监控redis是否按照预期良好地运行; 如果发现某个redis节点运行出现状况,能够通知另外一个进程(例如它的客户端); 能够进行自动切换.当一个master节点不可用时,能够选举出master的多个slave(如果有超过一个slave的话)中

基于Redis Sentinel的Redis集群(主从Sharding)高可用方案(转)

本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在Redis2.4中,Redis2.8中Sentinel更加稳定),Redis集群是以分片(Sharding)加主从的方式搭建,满足可扩展性的要求: Redis Sentinel介绍 Redis Sentinel是Redis官方提供的集群管理工具,主要有三大功能: 监控,能持续监控Redis的主从实例是否正常

Redis Sentinel 高可用实现说明

背景:      前面介绍了Redis 复制.Sentinel的搭建和原理说明,通过这篇文章大致能了解Sentinel的原理和实现方法以及相关的搭建.这篇文章就针对Redis Sentinel的搭建做下详细的说明. 安装:      这里对源码编译进行一下说明,本文实例的操作系统是Ubuntu16.04,使用Redis的版本是3.2.0.安装步骤如下: 下载源码包:wget http://download.redis.io/releases/redis-3.2.0.tar.gz 安装依赖包:su

(转)基于Redis Sentinel的Redis集群(主从&amp;Sharding)高可用方案

转载自:http://warm-breeze.iteye.com/blog/2020413 本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在Redis2.4中,Redis2.8中Sentinel更加稳定),Redis集群是以分片(Sharding)加主从的方式搭建,满足可扩展性的要求: Redis Sentinel介绍 Redis Sentinel是Re

Redis Sentinel安装与部署,实现redis的高可用

前言 对于生产环境,高可用是避免不了要面对的问题,无论什么环境.服务,只要用于生产,就需要满足高可用:此文针对的是redis的高可用. 接下来会有系列文章,该系列是对spring-session实现分布式集群session的共享的完整阐述,同时也引伸出缓存的实现:而此篇是该系列的第一篇. github地址:https://github.com/youzhibing/redis 环境准备 redis版本:redis-3.0.0 linux:centos6.7 ip:192.168.11.202,