Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对redis各类API进行封装调用.
我创建的是maven项目,所以只用在pom文件中加入
<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.0</version> </dependency>
如果不是maven项目,你要确定引入相关依赖
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; } } }
注意:如果出现
报错:Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException:
检查端口是否开放
解决方法:
1.关闭防火墙:service iptables stop
2.开放端口:
(1.修改配置文件:vi /etc/sysconfig/iptabls 追加:-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379-j ACCEPT
(2.包存改变:service iptables save
(3.重启服务:service iptables restart
查看redis的配置文件
报错: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.
报错信息很长,但是主要是说redis开启了protected mode,这也是Redis3.2加入的新特性,开启保护模式的redis只允许本机登录,同样设置在配置文件redis.conf中
这里原来是yes代表开启了保护模式,后面可以填密码也可以填no代表关闭,我们这里选择关闭保护模式,wq保存退出后再重启redis-server
下面再运行就可以了
前段时间给大家介绍了如何在Linux环境下部署和操作redis,今天将为大家介绍如何在我们的Java代码中操作redis。接下来 按部就班:
导入到 java项目里
以上是Java如何连接Redis的详细内容。更多信息请关注PHP中文网其他相关文章!