一、导入依赖二、编码测试四、开放端口6379四、重启redis-server
使用Java来操作Redis,Jedis是Redis官方推荐使用的Java连接redis的客户端。
二、编码测试redis.clients jedis 3.2.0 com.alibaba fastjson 1.2.70
连接数据库
修改redis的配置文件
四、开放端口6379vim /usr/local/bin/myconfig/redis.conf
firewall-cmd --zone=public --add-port=6379/tcp --permanet
重启防火墙服务
四、重启redis-serversystemctl restart firewalld.service
[root@AlibabaECS bin]# redis-server myconfig/redis.conf
操作命令
TestPing.java
public class TestPing {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.xx.xxx", 6379);
String response = jedis.ping();
System.out.println(response); // PONG
}
}
事务
public class TestTX {
public static void main(String[] args) {
Jedis jedis = new Jedis("39.99.xxx.xx", 6379);
JSonObject jsonObject = new JSONObject();
jsonObject.put("hello", "world");
jsonObject.put("name", "kuangshen");
// 开启事务
Transaction multi = jedis.multi();
String result = jsonObject.toJSONString();
// jedis.watch(result)
try {
multi.set("user1", result);
multi.set("user2", result);
// 执行事务
multi.exec();
}catch (Exception e){
// 放弃事务
multi.discard();
} finally {
// 关闭连接
System.out.println(jedis.get("user1"));
System.out.println(jedis.get("user2"));
jedis.close();
}
}
}



