redis related notes (II. cluster configuration and use)

1. Configuration: 8337,8338,8339,8340} {Add in the original folder redis-sentinel folder, and the original copy of the configuration 8333

To add a cluster configuration on the 8333 configuration file

            # ============================================ custom configuration start = ======================================= 
            #Redis default is not run as daemon, can modify the configuration item, use yes enable daemon 
            daemonize yes 
            # when Redis when running in daemon mode, the default Redis will write /var/run/redis.pid pid file can be specified by the PidFile 
            the PidFile / var / rUN /redis_8340.pid 
            # Redis specified listening port, the default port is 6379, the author explains why the choice of 6379 as the default port in his blog post, since 6379 MERZ corresponding number on the phone keypad, and MERZ from Italian showgirl Alessia Merz name 
            Port 8340 
            # host address binding 
            the bind 192.168.37.134 
            # logging mode, the default is standard output, log output to specify the log file 
            logfile "./redis-8340.log" 
            # specify the local database storage directory 
            dir"/ usr / local / redis- sentinel / 8340" 
            password # when the master service is password-protected, slave master service connection of 
            masterauth ADMIN .1231 
            # Set Redis connection password, if the password in the connection Redis configure the connection, the client the need to provide by AUTH <password> command password, the default closed 
            requirepass ADMIN .1231 
            # specify whether logging after every update operation, Redis default asynchronous data is written to disk, if not open, might when the power failure led to a period of 
            data within the time lost. 
            # Redis itself as synchronous data file is based on the above criteria to save synchronized, so some data will exist only in the memory for some time. The default is no (AOF holding open> long of) 
            appendOnly yes 
            ## cluster switch, the default is not open cluster model. 
            yes-Enabled Cluster 
            # cluster configuration file name, each node has a relevant configuration file for the cluster, persistent information stored cluster. This document does not require manual configuration, the configuration> files have Redis generated and updated every Redis cluster node requires a separate configuration file, make sure the system instance running in the configuration file name does not conflict
            File nodes_8340.conf-config-Cluster 
            # interconnection node timeout threshold. The number of milliseconds timeout cluster nodes 
            Cluster-the Node-timeout 10100
            # ============================================ end custom configuration = =======================================

2. Start All

            [root@localhost redis-sentinel]# redis-server 8337/redis-8337.conf 
            [root@localhost redis-sentinel]# redis-server 8338/redis-8338.conf 
            [root@localhost redis-sentinel]# redis-server 8339/redis-8339.conf 
            [root@localhost redis-sentinel]# redis-server 8340/redis-8340.conf 

View Results

            [root@localhost redis-sentinel]# ps -ef | grep redis
            root      44355      1  0 10:10 ?        00:00:00 redis-server 192.168.37.134:8337 [cluster]
            root      44368      1  0 10:11 ?        00:00:00 redis-server 192.168.37.134:8338 [cluster]
            root      44372      1  0 10:11 ?        00:00:00 redis-server 192.168.37.134:8339 [cluster]
            root      44376      1  0 10:11 ?        00:00:00 redis-server 192.168.37.134:8340 [cluster]

And groove connection and assign 3. (16484)

(Can be achieved (plus a cluster from a server using the official tools redis redis-trib.rb:. Redis Cluster requires at least 3 master nodes, At least 6 nodes are required.This is not possible with 4 nodes and 1 replicas per node))

1. Connect clustered

[root@localhost redis-sentinel]# redis-cli -c -h 192.168.37.134 -p 8337 -a admin.1231

# There is only one

192.168.37.134:8337> cluster nodes
1cc4d5687c9f75cfe6cda8a0f22120b9ef652ca5 :8337 myself,master - 0 0 0 connected

And linking # 8338 port

192.168.37.134:8337> cluster meet 192.168.37.134 8338
OK

Similarly sequentially connected 8339,8340
At this time the cluster redis offline state can not be used, CLUSTERDOWN Hash slot not served, unassigned slots

2. assigned slot

Write a shell script, which reads as follows, content refers to the slot assignment port node 8337 (0-4999), and other nodes in order to allocate a total of 16,484 allocated to the four nodes

                #! /bin/bash
                for((i=0; i<5000; i++))
                do
                  redis-cli -c -h 192.168.37.134 -p 8337 -a admin.1231 cluster addslots $i
                  echo $i" 次添加完毕"
                done

3. join queries can be seen cluster_slot16384 has been all ok

                192.168.37.134:8337> cluster info
                cluster_state:ok
                cluster_slots_assigned:16384
                cluster_slots_ok:16384
                cluster_slots_pfail:0
                cluster_slots_fail:0
                cluster_known_nodes:4
                cluster_size:4
                cluster_current_epoch:3
                cluster_my_epoch:1
                cluster_stats_messages_sent:9053
                cluster_stats_messages_received:9053
                # Can be seen that this value is the set value of the node 8340 into slot 15495 
                192.168 . 37.134 : 8337 > SET ab &
                 -> slot to the Redirected [ 15495 ] located block AT 192.168 . 37.134 : 8340 
                the OK

redis use

    public  static  void main (String [] args) throws IOException {
         // establish a connection 
        the Socket Socket = new new the Socket ( "192.168.37.134", 6379 ); 
        the OutputStream the outputStream = Socket.getOutputStream (); 
        the InputStream inputStream = Socket.getInputStream (); 
        
        / ** 
        * RESP protocol: https://redis.io/topics/protocol 
        * single-line response to: the first byte of the reply is "+", such as the OK on success + 
        * error message: the first byte of the reply It is "-", as represented Unknown Command error -ERR 'XXX' 
        * shaping figures: the first byte of the reply is ":" 
        * multiline string: the first byte of the reply is "$"
        * Array: the first byte reply is "*"
        * / 
        
        
        / *      Follow resp protocol to send messages: 
            * 3 array contains three elements, namely, SET, EAT, the I want to EAT 
            $ 3 is a string, the string length is 3 and 
            the contents of the string SET 
            $ 3 is a string and the length of the string. 3 
            AAA contents of the string 
            $ 6 is a string and the string length of 13 is 
            BBBBBB content of the string 
        * * / 
        OutputStream.write ( "*. 3" .getBytes ()); 
        OutputStream.write ( "\ R & lt \ n-" .getBytes ()); 
        
        OutputStream.write ( "$. 3" .getBytes ()); 
        OutputStream.write ( "\ R & lt \ n-" .getBytes ()); 
        
        OutputStream.write ( "the SET" .getBytes ());
        outputStream.write("\r\n".getBytes());
        
        outputStream.write("$3".getBytes());
        outputStream.write("\r\n".getBytes());
        
        outputStream.write("aaa".getBytes());
        outputStream.write("\r\n".getBytes());
        
        outputStream.write("$6".getBytes());
        outputStream.write("\r\n".getBytes());
        
        outputStream.write("bbbbbb".getBytes());
        outputStream.write("\r\n".getBytes());
        
        outputStream.the flush (); 

        receiver message//
        [] = Resultbytenew new  byte [2048 ]; 
        InputStream.read (Result); 
        System.out.println ( "response is received:" + new new String (Result)); 
    }

Results ===> receiving the response: + OK

 

Guess you like

Origin www.cnblogs.com/lantuanqing/p/11577169.html