Java使用Redis

 2023-09-16 阅读 23 评论 0

摘要:Redis的安装略过 Java打开、首先导入jedis.jar包 1.连接redis服务 Java怎么编译?写个简单的程序: package com.fpc.Test;import redis.clients.jedis.Jedis;public class ManipulateRedis {public static void main( String[] args ) {//连接10.0.20.251的Redis服务Je

Redis的安装略过

Java打开、首先导入jedis.jar包

1.连接redis服务

Java怎么编译?写个简单的程序:

package com.fpc.Test;import redis.clients.jedis.Jedis;public class ManipulateRedis {public static void main( String[] args ) {//连接10.0.20.251的Redis服务Jedis jedis = new Jedis("10.0.20.251");//查看服务是否运行System.out.println("服务正在运行 : " + jedis.ping());}
}

注意:说下2个主要的坑点:

redis怎么用?1.Redis要配置下redis.conf文件

要配置什么呢?配置其监听的ip地址

2.第二个要注意的就是redis-server的启动要加参数

如果不加--protected-mode no,Java运行报错误:

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.at redis.clients.jedis.Protocol.processError(Protocol.java:127)at redis.clients.jedis.Protocol.process(Protocol.java:161)at redis.clients.jedis.Protocol.read(Protocol.java:215)at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:196)at com.fpc.Test.ManipulateRedis.main(ManipulateRedis.java:11)

正常运行的结果:

 

Redis&Java 操作元素

package com.fpc.Test;import redis.clients.jedis.Jedis;public class ManipulateRedis {public static void main( String[] args ) {//连接10.0.20.251的Redis服务Jedis jedis = new Jedis("10.0.20.251");//查看服务是否运行System.out.println("服务正在运行 : " + jedis.ping());//设置redis字符串数据jedis.set("fpc", "fangpengcheng");//获取存储的数据并输出String value  = jedis.get("fpc");System.out.println("Redis 存储的字符串为  :"  + value);}
}

运行结果:

 

 Redis&Java 操作列表

package com.fpc.Test;import java.util.List;import redis.clients.jedis.Jedis;public class ManipulateRedis {public static void main( String[] args ) {//连接10.0.20.251的Redis服务Jedis jedis = new Jedis("10.0.20.251");//查看服务是否运行System.out.println("服务正在运行 : " + jedis.ping());//将数据存储到列表中,每次都是从左边push进入listjedis.lpush("list", "fangpengcheng1");jedis.lpush("list", "fangpengcheng2");jedis.lpush("list", "fangpengcheng3");//获取存储的数据并输出List<String> list = jedis.lrange("list", 0, 2);System.out.println(list.toString());list = jedis.lrange("list", 0, 3);System.out.println(list.toString());list = jedis.lrange("list", 0, 4);System.out.println(list.toString());list = jedis.lrange("list", 0, 5);System.out.println(list.toString());list = jedis.lrange("list", 0, 6);System.out.println(list.toString());list = jedis.lrange("list", 0, 7);System.out.println(list.toString());list = jedis.lrange("list", 0, 8);System.out.println(list.toString());}
}

运行的结果是:

可见如果jedis.lrange中给出的范围是超过Redis中实际list的长度的话是会循环继续取元素的。

 Redis&Java也可以操作Redis中的Set,Map等,再这里就不赘述了,查下文档就可以。下面打算用Redis&Java实现一个消息队列。

转载于:https://www.cnblogs.com/fangpengchengbupter/p/9260069.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/68598.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息