Jedis Client is a java client recommended by the Redis official website. The library file implements encapsulation and calling of various redis APIs.
I created a maven project, so I only need to add
<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.0</version> </dependency>
to the pom file. If it is not a maven project, you have to make sure to introduce the relevant dependencies
package cn.jiangdoc; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * * @author jiangdoc * */ public class JedisUtil { public static void main(String[] args) { //ip地址,端口号 Jedis jedis = cli_single("192.168.1.103", 6379); jedis.set("key", "first Java connect!"); String value = jedis.get("key"); System.out.println(value); } /** * 单个连接 * * @param host * @param port * @return */ public static Jedis cli_single(String host, int port) { try { return new Jedis(host, port); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 连接池 * * @param host * @param port * @return */ public static Jedis cli_pool(String host, int port) { JedisPoolConfig config = new JedisPoolConfig(); // 最大连接数 config.setMaxTotal(10); // 最大连接空闲数 config.setMaxIdle(2); JedisPool jedisPool = new JedisPool(config, host, port); try{ return jedisPool.getResource(); }catch(Exception e){ e.printStackTrace(); return null; } } }
Note: If
error occurs: Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException:
Check whether the port is open
Solution:
1. Turn off the firewall: service iptables stop
2. Open port:
(1. Modify the configuration file: vi /etc/sysconfig/iptabls append: -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379-j ACCEPT
(2. Package save changes: service iptables save
(3. Restart the service: service iptables restart
View redis configuration file
Error report: 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.
The error message is very long, but it mainly means that redis has turned on protected mode, which is also a new feature added in Redis 3.2. Redis that turns on protected mode only allows local login, which is also set in the configuration file. In redis.conf
it turns out that yes means turning on the protected mode. You can fill in the password or no to turn off the protected mode. Here we choose to turn off the protected mode. After wq saves and exits, restart redis-server
Just run it below
Some time ago I introduced to you how to deploy and operate redis in the Linux environment. Today I will introduce to you how to operate redis in our Java code. Next step by step:
into the java project
The above is the detailed content of How to connect Java to Redis. For more information, please follow other related articles on the PHP Chinese website!