jedis+Maven整合
jedis+Maven整合
什么是jedis ? jedis是Redis官方推荐的java连接开发工具!使用java操作Redis中间件!
导入依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
编码测试
- 连接数据库
- 操作命令
- 断开连接
public class TestPing {
public static void main(String[] args) {
//new 一个jedis对象
Jedis jedis = new Jedis("8.140.30.101",6379);
System.out.println(jedis.ping());
jedis.set("name","zhangsan");
String name = jedis.get("name");
System.out.println(name);
}
}
注意事项
连接本机服务
Jedis jedis = new Jedis("127.0.0.1",6379);
连接阿里云服务器
-
开启阿里云服务器端口6379
-
redis配置文件修改:maemonize yes,注释bind 127.0.0.1,protected-mode no
-
终端输入:
[root@zhang bin]# systemctl start firewalld [root@zhang bin]# firewall-cmd --query-port=6379/tcp #查看端口是否开放如果是no则需输入下一条命令 no [root@zhang bin]# firewall-cmd --zone=public --add-port=6379/tcp --permanent #设置redis 6379端口开放 success [root@zhang bin]# firewall-cmd --query-port=6379/tcp #成功开放 yes [root@zhang bin]# redis-server ./myconfig/redis.conf #启动服务
-
Jedis jedis = new Jedis("服务器ip",6379);
写到最后:远程连接建议设置密码