什么是Jedis
jedis是redis官方推荐的java连接开发工具!使用Java操作redis的中间件!如果你要使用Java操作redis,那么一定要对jedis十分熟悉
使用Jedis
前提
- 必须要放开redis允许启动主机访问。
注释/usr/local/bin/config/redis.conf的 bind 127.0.0.1 这个句话
- 取消保护模式
redis处于保护模式,只能本地链接,我们需要修改配置文件redis.conf,将protected-mode yes改成no
3.防火墙开放6379端口
查看端口开放信息,如果没有看见6379,就需要设置
[root@centos7 config]# firewall-cmd --list-ports
#开启6379端口
[root@centos7 config]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
#重启防火墙
[root@centos7 config]# firewall-cmd --reload
success
查看端口开放信息
[root@centos7 config]# firewall-cmd --list-ports
6379/tcp
测试连接
- 连接数据库
- 操作命令
- 断开连接
第一步:创建maven简单项目,添加依赖pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>redis-01-jedis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--导入jedis依赖包-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<!--3.2.0-->
<version>3.6.3</version>
</dependency>
<!--fastjson依赖包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
</project>
第二步:测试代码
package org.example;
import redis.clients.jedis.Jedis;
public class TestPing {
public static void main(String[] args) {
//1.new Jedis对象
Jedis jedis = new Jedis("192.168.222.129", 6379);
//jedis 所有redis命令都在jedis同名的方法中
System.out.println(jedis.ping());
}
}
测试结果图
jedis常用的API
String、List、Set、Hash、Zset
所有的api命令,就是redis命令