重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍基于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依赖
redis.clients jedis 3.2.0
编写简易的单元测试程序
可使用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() { Mapcustomer = 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() { Mapscore = 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服务的教程详解”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!