首先要引入依赖:
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
-
- <dependency>
- <groupId>redis.clientsgroupId>
- <artifactId>jedisartifactId>
- dependency>
在linux中开启redis服务器:
redis-server /xxx/xxx/redis.conf
若提示连接不上的解决方案:
1.检查redis-conf文件
1.1 daemonize 是否为 yes
1.2 bind 127.0.0.1 是否已注释掉
1.3 protected-mode 是否为 no
2.linux防火墙设置
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --zone=public --add-port=6379/tcp --permanent 开放6379端口
测试:
- @Test
- void testCon(){
- Jedis jedis = new Jedis("192.168.80.128",6379);
- jedis.set("s", "success");
- String ping = jedis.ping();
- System.out.println(ping+","+jedis.get("s"));
- }
-
- //结果:
- PONG,success