Redis test class

Standalone & cluster installation: https://blog.csdn.net/zxd1435513775/article/details/88901992
install version 5.0.4 OK, version 5.0.5 being given make.

redis.conf configuration:
Reference:
https://www.cnblogs.com/taiyonghai/p/5826134.html
https://www.cnblogs.com/taiyonghai/p/5826237.html
https://blog.csdn.net / tiantiandjava / article / details / 72831529 ( emphasis reference )

bind 127.0.0.1 (Notes)
Exception in the Thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed Connecting to Host 192.167.2.167:6379
Caused by: java.net.ConnectException: Connection refused The: Connect
where English Description bind is the interface, that is to say the network interface. The server may have a network interface (NIC popular to say, execute ifconfig view), or more. An analogy that has two network cards on the machine are 192.168.205.5 and 192.168.205.6, if bind 192.168.205.5, then only the Ethernet address accepting external requests, if not binding, the two NIC ports are accepting requests.
Execute ps -ef | grep redis-server, you can see the difference, # bind 127.0.0.1 display root 32012 1 0 14:00 00:00:00 ./redis-server * : 6379, bind 192.167.2.167 display root 2850? 0 14:08 1? 00:00:00 ./redis-server 192.167.2.167:6379
the bind 192.167.2.167, the client must be connected to the machine ./redis-cli -h 192.167.2.167, or else directly. / redis-cli connection

protected-mode no (changed to yes)
Exception in thread "main" 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.

daemonize no(改成yes)
pidfile /var/run/redis_6379.pid

redis uses a single-process multi-threaded mode. When redis.conf daemonize option set to yes, on behalf of open daemon mode. In this mode, redis runs in the background and process pid number is written to the file redis.conf pidfile option set, in which case redis will always run, unless you manually kill the process. But when daemonize option is set to no, the current interface will enter redis command-line interface, exit forced to exit or close the connection tool (putty, xshell, etc.) will result in redis process to exit.
Most applications are based on the service side development model running in the background.
When redis running in the background, Redis will default pid file on /var/run/redis_6379.pid, you can be configured to a different address. When running multiple redis service, you need to specify a different pid files and ports.

requirepass iMC123 (release note)
Reference https://www.cnblogs.com/suanshun/p/7699084.html
Description: Warning: since Redis is pretty fast an outside user can try up to 150k passwords per second against a good box. This means that you should use a very strong password otherwise it will be very easy to break redis query speed is very fast, internal and external users can try one second how much 150K passwords; so the password you want as long as possible (no need for DBA It must be remembered password);
client ./redis-cli -p 6379 -a iMC123 or ./redis-cli -p 6379, in implementation of iMC123 auth
Exception in the Thread "main" redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.

rely

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.0.1</version>
</dependency>

Code

package com.example.demo_mg.test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class TestRedis {
    public static void main( String[] args ) {
        //方式一
//        test1();

        //方式二:线程池
        test2();
    }

    private static void test1() {
        Jedis jedis = new Jedis("129.204.58.30",6379);
        jedis.auth("test123"); //密码
        System.out.println(jedis.ping()); //PONG
        jedis.set("hello","world");
        String value = jedis.get("hello");
        System.out.println(value);
        jedis.close();
    }

    private static void test2() {
        JedisPool pool = new JedisPool("129.204.58.30",6379);
        Jedis jedis = pool.getResource();
        String value = jedis.get("hello");
        System.out.println(value);
        jedis.close();
    }
}

Guess you like

Origin www.cnblogs.com/kibana/p/11111542.html