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);
寫到最后:遠程連接建議設置密碼