配置Redis Sentinel,并使用Jedis连接


目标:
演示的3台redis主从服务器的连接信息,使用sentinel管理主从节点,并使用jedis连接sentinel
配置:

* 配置3个redis服务,其中7000做主服务器,7001、7002做从服务器
* 配置3个sentinel,端口分别为26379、26380、26381
* 使用jedis连接26379这个sentinel

注意

* 所有编辑的文件和输出日志均在/usr/local/bin目录下,生产中最好将日志文件移出
* 所有服务器均在本地测试,生产中要使用不同的服务器

演示:
编辑一个redis-7000.conf的配置文件,如下

 port 7000
 daemonize yes
 pidfile /var/run/redis-7000.pid
 logfile "7000.log"
 dir "/usr/local/bin/"

使用sed命令生成redis-7001.conf、redis-7002.conf

 sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf
 sed "s/7000/7002/g" redis-7000.conf > redis-7002.conf

在7001、 7002配置文件中添加slaveof 配置,表明将其作为7000服务的从服务

 echo "slaveof 127.0.0.1 7000" >> redis-7001.conf
 echo "slaveof 127.0.0.1 7000" >> redis-7002.conf

启动7000的redis服务

 redis-server redis-7000.conf

这时还没有启动其他从服务器,可以看一下它的信息
命令“redis-cli -p 7000 info replication”

[root@localhost bin]# redis-server redis-7000.conf
 [root@localhost bin]# ps -ef | grep redis
 root 6377 1 0 22:53 ? 00:00:00 redis-server *:7000
 root 6382 6231 0 22:58 pts/0 00:00:00 grep redis
 [root@localhost bin]# redis-cli -p 7000 info replication
 # Replication
 role:master
 connected_slaves:0
 master_replid:c27ac443e0029013cd57842e9c09f4e0aa712a32
 master_replid2:0000000000000000000000000000000000000000
 master_repl_offset:0
 second_repl_offset:-1
 repl_backlog_active:0
 repl_backlog_size:1048576
 repl_backlog_first_byte_offset:0
 repl_backlog_histlen:0
 [root@localhost bin]#

可以看出,这台服务并没有挂从节点。
启动7001、 7002服务

 redis-server redis-7001.conf
 redis-server redis-7002.conf

这时再看7000主服务的info

 [root@localhost bin]# redis-server redis-7001.conf
 [root@localhost bin]# redis-server redis-7002.conf
 [root@localhost bin]# redis-cli -p 7000 info replication
 # Replication
 role:master
 connected_slaves:2
 slave0:ip=127.0.0.1,port=7001,state=online,offset=28,lag=1
 slave1:ip=127.0.0.1,port=7002,state=online,offset=28,lag=1
 master_replid:d011fcb6df8d5637c60936855f56a52cefeabba3
 master_replid2:0000000000000000000000000000000000000000
 master_repl_offset:28
 second_repl_offset:-1
 repl_backlog_active:1
 repl_backlog_size:1048576
 repl_backlog_first_byte_offset:1
 repl_backlog_histlen:28
 [root@localhost bin]

这时显示主服务器已经连接了2台从服务器。

配置sentinel
使用模板文件配置sentinel

 cat sentinel.conf | grep -v "#" | grep -v "^$" > sentinel-26379.conf
编辑sentinel-26379.conf
 port 26379
 daemonize yes
 dir "/usr/local/bin"
 logfile "26379.log"
 sentinel myid 02384c65d87f61fb4c2382b4a0922504545af708
 # 定义别名,指定主服务器,注意,没有配置从服务器,启动后会自动发现
 sentinel monitor mymaster 127.0.0.1 7000 2
 sentinel config-epoch mymaster 0
 sentinel leader-epoch mymaster 0

配置另外2台服务

 sed "s/26379/26380/g" sentinel-26379.conf > sentinel-26380.conf
 sed "s/26379/26381/g" sentinel-26379.conf > sentinel-26381.conf

启动这3台sentinel服务

 [root@localhost bin]# redis-sentinel sentinel-26379.conf
 [root@localhost bin]# redis-sentinel sentinel-26380.conf
 [root@localhost bin]# redis-sentinel sentinel-26381.conf

查看其中一台sentinel服务器的信息

 [root@localhost bin]# redis-cli -p 26379
 127.0.0.1:26379> info
 # Server
 redis_version:4.0.6
 redis_git_sha1:00000000
 redis_git_dirty:0
 redis_build_id:144111a618779b67
 redis_mode:sentinel
 os:Linux 2.6.32-042stab123.3 i686
 arch_bits:32
 multiplexing_api:epoll
 atomicvar_api:sync-builtin
 gcc_version:4.4.7
 process_id:6480
 run_id:d5a27912fa6eef1b551acbee71e3d1a20242f514
 tcp_port:26379
 uptime_in_seconds:84
 uptime_in_days:0
 hz:19
 lru_clock:4918901
 executable:/usr/local/bin/redis-sentinel
 config_file:/usr/local/bin/sentinel-26379.conf
 # Clients
 connected_clients:2
 client_longest_output_list:0
 client_biggest_input_buf:0
 blocked_clients:0
 // ....省略一部分内容...
 # Sentinel
 sentinel_masters:1
 sentinel_tilt:0
 sentinel_running_scripts:0
 sentinel_scripts_queue_length:0
 sentinel_simulate_failure_flags:0
 master0:name=mymaster,status=ok,address=127.0.0.1:7000,slaves=2,sentinels=2

 

 

使用Java的JedisSentinelPool连接
/**
     * 使用sentinel测试jedis连接
     */
    @Test
    public void testJedisSentinelPool(){
        Set<String> sentinels = new HashSet<String>();
        sentinels.add("23.83.247.149:26379");
        sentinels.add("23.83.247.149:26380");
        sentinels.add("23.83.247.149:26381");
        JedisSentinelPool sentinelPool = new JedisSentinelPool("mymaster", sentinels);
        Jedis jedis = null;
        try {
            jedis = sentinelPool.getResource();
            int index = new Random().nextInt(10000);
            String key = "k-" + index;
            String value = "v-" + index;
            jedis.set(key, value);
            logger.info("{} value is {}", key, jedis.get(key));
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }

 


分享到:

发表评论

昵称

抢个沙发呗~~~