您好,登錄后才能下訂單哦!
這篇文章主要介紹基于SpringBoot集成測試遠程連接Redis服務的教程詳解,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
前期準備
Linux虛擬機或者租用的云服務器:sudo安裝redis,或者docker加載redis鏡像。后者需要使用docker啟用redis容器。
配置好redis.conf文件。注意:一定要注釋 # bind 127.0.0.1
其他詳細配置可參考我另一篇文章,不過能想到集成測試redis,配置文件應該已經配置好了。
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
開放6379端口
初始化SpringBoot項目使用Spring Initializr 初始化一個SpringBoot項目,可以勾選NoSQL下這兩項,或者后面自行添加pom依賴。(當然也可以初始化一個maven項目來測試)
添加jedis依賴
<!--導入jedis依賴--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency>
編寫簡易的單元測試程序
可使用ifconfig
或者 ip addr
查詢服務器的ip地址
/** * host: 虛擬機或遠程服務器的ip地址 * port: redis.conf中配置的服務端口號 */ @Test void TestPing() { Jedis jedis = new Jedis("192.168.137.132",6379); System.out.println("TestPing >>>>>>>>>>>>> "+jedis.ping()); }
測試結果:
測試五大基本數據類型
測試過程中,有些數據防止重復插入,部分測試用例使用了del
命令。
五大基本數據類型:字符串,列表,哈希,集合,有序集合
每次單元測試都需進行遠程連接,故做以下處理:
@SpringBootTest class JedisDemoApplicationTests { // 此處配置連接 Jedis jedis = new Jedis("192.168.137.132",6379); @Test void contextLoads() { } // 此處進行單元測試 }
Key鍵
測試用例:
@Test void KeyTest() { System.out.println(jedis.keys("*")); System.out.println(jedis.keys("*").getClass()); }
測試結果:
[list01, customer, set01, username]
class java.util.HashSet
String字符串
測試用例:
@Test void StringTest() { System.out.println("測試set >>> "+jedis.set("name", "kris")); System.out.println("測試get >>> "+jedis.get("name")); System.out.println(jedis.get("name").getClass()); System.out.println("\n測試append >>> "+jedis.append("name", " ^v^")); System.out.println("測試strlen >>> "+jedis.strlen("name")); System.out.println("測試get >>> "+jedis.get("name")); System.out.println("\n測試mset >>> "+jedis.mset( "username", "kris", "age", "18", "sex","男")); System.out.println("測試mget >>> "+jedis.mget("username", "age", "sex")); System.out.println("\n測試incr >>> "+jedis.incr("age")); System.out.println("測試incrBy >>> "+jedis.incrBy("age",3)); System.out.println("測試decr >>> "+jedis.decr("age")); System.out.println("測試decrBy >>> "+jedis.decrBy("age", 3)); }
測試結果:
測試set >>> OK
測試get >>> kris
class java.lang.String
測試append >>> 8
測試strlen >>> 8
測試get >>> kris ^v^
測試mset >>> OK
測試mget >>> [kris, 18, 男]
測試incr >>> 19
測試incrBy >>> 22
測試decr >>> 21
測試decrBy >>> 18
List列表
測試用例:
@Test void ListTest() { System.out.println("測試rpush >>> "+jedis.rpush("list01", "I", "love", "redis", "!")); System.out.println("測試lpush >>> "+jedis.lpush("list01", "hello")); System.out.println("測試lrange >>> "+jedis.lrange("list01", 0, -1)); System.out.println(jedis.lrange("list01", 0, -1).getClass()); System.out.println("\n測試lpop >>> "+jedis.lpop("list01")); System.out.println("測試rpop >>> "+jedis.rpop("list01")); System.out.println("測試lrange >>> "+jedis.lrange("list01", 0, -1)); System.out.println("\n測試lset >>> "+jedis.lset("list01", 0, "We")); System.out.println("測試lrange >>> "+jedis.lrange("list01", 0, -1)); System.out.println("\n測試 linsert >>> "+jedis.linsert("list01", ListPosition.AFTER, "redis", "!")); System.out.println("測試lrange >>> "+jedis.lrange("list01", 0, -1)); jedis.del("list01"); }
測試結果:
測試rpush >>> 4
測試lpush >>> 5
測試lrange >>> [hello, I, love, redis, !]
class java.util.ArrayList
測試lpop >>> hello
測試rpop >>> !
測試lrange >>> [I, love, redis]
測試lset >>> OK
測試lrange >>> [We, love, redis]
測試 linsert >>> 4
測試lrange >>> [We, love, redis, !]
Hash哈希
測試用例:
@Test void HashTest() { Map<String, String> customer = new HashMap<>(); customer.put("username", "kris"); customer.put("password", "123456"); customer.put("email", "kris@163.com"); System.out.println("測試hset >>> "+jedis.hset("customer", customer)); System.out.println("測試hget >>> "+jedis.hget("customer", "email")); System.out.println("測試getAll >>> "+jedis.hgetAll("customer")); System.out.println(jedis.hgetAll("customer").getClass()); }
測試結果:
測試hset >>> 0
測試hget >>> kris@163.com
測試getAll >>> {name=kris, email=kris@163.com, password=123456, username=kris}
class java.util.HashMapSet集合
測試用例:
@Test void SetTest() { System.out.println("測試sadd >>> "+jedis.sadd("names", "louis", "kris", "Jerry", "Jackson")); System.out.println("測試smembers >>> "+jedis.smembers("names")); System.out.println("測試scard >>> "+jedis.scard("names")); System.out.println(jedis.smembers("names").getClass()); System.out.println("\n測試srandmember >>> "+jedis.srandmember("names", 3)); System.out.println("測試srandmember >>> "+jedis.srandmember("names", 3)); System.out.println("\n測試spop >>> "+jedis.spop("names", 2)); System.out.println("測試smembers >>> "+jedis.smembers("names")); System.out.println("\n測試sadd >>> "+jedis.sadd("names0", "Jack", "louis", "kris")); System.out.println("測試sunion >>> "+jedis.sunion("names", "names0")); System.out.println("測試sdiff >>> "+jedis.sdiff("names", "names0")); System.out.println("測試sinter >>> "+jedis.sinter("names", "names0")); System.out.println("測試smembers >>> "+jedis.smembers("names")); System.out.println("測試smembers >>> "+jedis.smembers("names0")); jedis.del("names", "names0"); }
測試結果:
測試sadd >>> 4
測試smembers >>> [kris, Jerry, Jackson, louis]
測試scard >>> 4
class redis.clients.jedis.BinaryJedis$SetFromList測試srandmember >>> [kris, Jerry, Jackson]
測試srandmember >>> [Jerry, louis, Jackson]測試spop >>> [Jerry, kris]
測試smembers >>> [Jackson, louis]測試sadd >>> 3
測試sunion >>> [Jack, kris, louis, Jackson]
測試sdiff >>> [Jackson]
測試sinter >>> [louis]
測試smembers >>> [Jackson, louis]
測試smembers >>> [kris, Jack, louis]ZSet有序集合
測試用例:
@Test void SortSetTest() { Map<String, Double> score = new HashMap<>(); score.put("鉆石", 1000.0); score.put("王者", 2000.0); // 無序 score.put("星耀", 1500.0); score.put("榮耀", 2500.0); System.out.println("測試zadd >>> "+jedis.zadd("score", score)); System.out.println("測試zrangeByScore >>> "+jedis.zrangeByScore("score", 1500,2500)); System.out.println("測試zrange >>> "+jedis.zrange("score", 0,-1)); System.out.println("測試zrevrange >>> "+jedis.zrevrange("score", 0,-1)); System.out.println(jedis.smembers("names").getClass()); jedis.del("score"); }
測試結果:
測試zadd >>> 4
測試zrangeByScore >>> [星耀, 王者, 榮耀]
測試zrange >>> [鉆石, 星耀, 王者, 榮耀]
測試zrevrange >>> [榮耀, 王者, 星耀, 鉆石]
class redis.clients.jedis.BinaryJedis$SetFromList
以上是“基于SpringBoot集成測試遠程連接Redis服務的教程詳解”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。