Redis sentinel architecture build test example

In the master-slave architecture, if the master node is down, it needs to be handled manually by the operation and maintenance personnel to rewrite the configuration of the master node.

The sentinel architecture can solve this problem. When the master node goes down, the sentinel will automatically elect a node as the master node.

The sentinel sentinel is a redis service for books and does not provide read and write services. It is mainly used to monitor redis instance nodes.

Under the sentinel architecture, the client side finds the redis master node from the sentinel for the first time, and subsequently supports access to the redis master node. It will not access the redis master node through the sentinel agent every time. When the redis master node sends changes, the sentinel will be the first time Perceived and notify the client of the new redis master node, the redis client subscribes to the node change message published by sentinel.

Architecture-Build the master-slave architecture on the right

1. Follow the steps to build a master-slave architecture, and copy a config file to redis.conf_2

cd /home/allen/packages/redis-5.0.3/conf
cp redis.conf_1 redis.conf_2

2. Modify the content of redis.config_2

port 6381                                  #修改端口号,代表本机另外一台机器
pidfile /var/run/redis_6381.pid            #把pid写入到该文件中
logfile "6381.log"                         #log的地址和文件
dir /home/allen/packages/redis-5.0.3/6381  #知道数据存放的目录
replicaof 127.0.0.1 6379  #本机从6379那台机器上获取数据 5.0之后改成slaveof
replica-read-only yes     #配置从节点只读

3. Create a directory for data storage

mkdir /home/allen/packages/redis-5.0.3/6381

4. Start this new node

src/redis-server conf/redis.conf_2 &

As follows, we have completed the following architecture:

[allen@localhost conf]$ ps -ef| grep redis
allen     25604  17775  0 05:11 pts/3    00:00:09 src/redis-server *:6379
allen     25636  17807  0 05:12 pts/4    00:00:08 src/redis-server 127.0.0.1:6380
allen     25665  17807  0 05:13 pts/4    00:00:08 src/redis-server 127.0.0.1:6381
allen     26384  17807  0 06:24 pts/4    00:00:00 grep --color=auto redis
[allen@localhost conf]$ 

 

Architecture-Build the sentry architecture on the left

1. Copy a sentinel.conf file

[allen@localhost redis-5.0.3]$ cp sentinel.conf sentinel-26379.conf
[allen@localhost redis-5.0.3]$ vi sentinel-26379.conf

2. Modify the file sentinel-26379.conf

port 26379
daemonize yes
pidfile /var/run/redis-sentinel-26379.pid
logfile "26379.log"
dir /home/allen/packages/redis-5.0.3/26379
sentinel monitor mymaster 192.168.0.109 6379 2 
#sentinel monitor mymaster 192.168.0.109 6379 2 其中2 代表配置3个哨兵。

3. Create a directory

mkdir /home/allen/packages/redis-5.0.3/26379

4. Create new files sentinel-26380.conf and sentinel-26381.conf with the same configuration

5. Start View Sentinel

[allen@localhost redis-5.0.3]$ src/redis-sentinel sentinel-26379.conf 
[allen@localhost redis-5.0.3]$ src/redis-sentinel sentinel-26380.conf 
[allen@localhost redis-5.0.3]$ src/redis-sentinel sentinel-26381.conf 
[allen@localhost redis-5.0.3]$ ps -ef| grep sentinel
allen     33493      1  0 03:41 ?        00:00:00 src/redis-sentinel *:26379 [sentinel]
allen     33498      1  1 03:41 ?        00:00:00 src/redis-sentinel *:26380 [sentinel]
allen     33503      1  1 03:41 ?        00:00:00 src/redis-sentinel *:26381 [sentinel]

You can see the configuration architecture information by viewing less sentinel-26379.conf. The following information was written by redis after we started.

sentinel known-replica mymaster 127.0.0.1 6380
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-sentinel mymaster 192.168.0.109 26381 ba6ebe75530c0129b5ab7ef0f580cb7a52667a7d
sentinel known-sentinel mymaster 192.168.0.109 26380 948ea521e8308e2b285795d940f5f8dd0506b412
sentinel current-epoch 0

On the client side, you can view the following 9 major information through the info command:

The role of the current server can be determined according to the role in #Replication, where the role of 6379 is master

127.0.0.1:6379> info
# Server
# Clients
# Memory
# Persistence
# Stats
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=5728302,lag=0
slave1:ip=127.0.0.1,port=6380,state=online,offset=5728161,lag=0
master_replid:fcc58e692eba4932010f9d184a6a0df71bb958c2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5728302
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4679727
repl_backlog_histlen:1048576
# CPU
# Cluster
# Keyspace

Below you can see that the role of 6381 is slave:

[allen@localhost redis-5.0.3]$ src/redis-cli -p 6381
127.0.0.1:6381> info Replication
# Replication
role:slave
master_host:192.168.0.109
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:5960518
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:fcc58e692eba4932010f9d184a6a0df71bb958c2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5960518
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4911943
repl_backlog_histlen:1048576
127.0.0.1:6381> 

Jedis link sentinel architecture:

public class RedisSentinel {
    public static void main(String[] args) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(20);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMinIdle(5);

        //sentinel monitor mymaster 192.168.0.109 6379 2 , 这里的必须和这里的mymaster名字一样
        String masterName = "mymaster";

        Set<String> sentinels = new HashSet<String>();
        sentinels.add(new HostAndPort("192.168.0.109", 26379).toString());
        sentinels.add(new HostAndPort("192.168.0.109", 26380).toString());
        sentinels.add(new HostAndPort("192.168.0.109", 26381).toString());
        //JedisSentinelPool并不是说与sentinel建立的连接池,而是通过sentinel发现redis主节点并与其建立连接

        JedisSentinelPool jedisPool = new JedisSentinelPool(masterName, sentinels, jedisPoolConfig, 3000, null);

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            System.out.println(jedis.set("sentinelTset1", "hello world"));
            System.out.println(jedis.get("sentinelTset1"));
        } finally {
            jedis.close();
        }
    }
}

 Results of the:

OK
hello world

Check the execution effect in redis:

127.0.0.1:6381> get sentinelTset1
"hello world"
127.0.0.1:6381> 

Link sentinel architecture through spring boot api:

1. Import the jar package

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.7.RELEASE</version>
        </dependency>

2. Add yml configuration sentinel

server:
  port: 8080

spring:
  redis:
    database: 0
    timeout: 3000
    sentinel: #哨兵模式
      master: mymaster #主服务器所在集群名称
      nodes: 192.168.0.109:26379,192.168.0.109:26380,192.168.0.109:26381
    lettuce:
      pool:
        max‐idle: 50
        min‐idle: 10
        max‐active: 100
        max‐wait: 1000

3. java code

@SpringBootApplication
public class ApplicationStarter {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationStarter.class,args);
    }
}
@RestController
public class IndexController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/SentinelTest")
   public void testSentinel() throws InterruptedException {
       int i = 1;
       while(true){
           stringRedisTemplate.opsForValue().set("SentinelTest2",i+++"");
           System.out.println("successed set SentinelTest2 to "+i);
           Thread.sleep(1000);
       }
   }
}

4. Start Application and visit through the page

5. You can see that the value of SentinelTest2 has been changing

127.0.0.1:6379> get SentinelTest2
"22"
127.0.0.1:6379> get SentinelTest2
"24"
127.0.0.1:6379> get SentinelTest2
"28"
127.0.0.1:6379> get SentinelTest2
"34"
127.0.0.1:6379> get SentinelTest2
"37"
127.0.0.1:6379> 

6. Manually kill the process

[allen@localhost redis-5.0.3]$ kill 35186

7. You can see that the program will report an error and return to normal after a while.

8. 6380 is selected as the master node to continue to provide services.

Guess you like

Origin blog.csdn.net/pengweismile/article/details/112589069