今天小编给大家分享一下Java如何连接Redis的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。Java连接RedisJedi
今天小编给大家分享一下Java如何连接Redis的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对redis各类api进行封装调用.
<!-- 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;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);}public static Jedis cli_single(String host, int port) {try {return new Jedis(host, port);} catch (Exception e) {e.printStackTrace();return null;}}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:
检查端口是否开放
解决方法:
关闭防火墙:service iptables stop
开放端口:
(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”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网精选频道。
--结束END--
本文标题: Java如何连接Redis
本文链接: https://lsjlt.com/news/341504.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0