Java连接Redis全过程讲解

网友投稿 238 2022-09-26

Java连接Redis全过程讲解

目录java连接Redis引入jar包编写测试类Jedis常用方法API一、首先把 jedis-2.1.0.jar(jedis基础包)二、创建 jedis对象三、键操作四、字符串操作五、整数和浮点数操作六、列表(List)操作七、集合(Set)操作八、哈希(Hash)操作九、有序集合(Zsort)操作十、排序操作

Java连接Redis

Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对redis各类API进行封装调用.

引入jar包

我创建的是maven项目,所以只用在pom文件中加入

redis.clients

jedis

3.0.0

如果不是maven项目http://,你要确定引入相关依赖

编写测试类

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 jedihttp://s = 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.getcQgtjauJBhResource();

}catch(Exception e){

e.printStackTrace();

return null;

}

}

}

注意:如果出现

报错:Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException:

检查端口是否开放

解决方法:

1.关闭防火墙:service iptables stop2.开放端口:

(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

下面再运行就可以了

Jedis常用方法API

前段时间给大家介绍了如何在linux环境下部署和操作redis,今天将为大家介绍如何在我们的Java代码中操作redis。接下来 按部就班:

一、首先把 jedis-2.1.0.jar(jedis基础包)

导入到 java项目里

二、创建 jedis对象

三、键操作

四、字符串操作

五、整数和浮点数操作

六、列表(List)操作

七、集合(Set)操作

八、哈希(Hash)操作

九、有序集合(Zsort)操作

十、排序操作

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:2021热度不减的在线教育,正在努力成为线下教育的有益补充
下一篇:如何引领万亿休闲零食市场新消费浪潮?三只松鼠提供了一种思路
相关文章

 发表评论

暂时没有评论,来抢沙发吧~