文章目录
- 一、Jedis
- 二、Lettuce
- 三、RedisTemplate(重点)
- 单机
- 3.1 springboot 整合swagger
- 3.2 序列化+中文问题
- 集群
- 3.3 applications配置
- 3.4 问题
一、Jedis
package com.example.redis;import redis.clients.jedis.Jedis;import javax.print.DocFlavor;
import java.util.*;/*** @ClassName Mydemo* @Description TODO* @Author lukcy* @Date 2024/6/23 10:31* @Version 1.0*/
public class Mydemo {public static void main(String[] args) {Jedis jedis = new Jedis("127.0.0.1", 6379);//有密码的话
// jedis.auth("qqqqqq");//看是否连接上System.out.println(jedis.ping());Set<String> keys = jedis.keys("*");System.out.println(keys);//Stringjedis.set("k1","v1");System.out.println(jedis.get("k1"));System.out.println(jedis.ttl("k1"));System.out.println(jedis.expire("k1",20L));//listjedis.lpush("list","11","22");List<String> list = jedis.lrange("list", 0, -1);for (String s : list) {System.out.println(s);}//hashjedis.hset("hash","k2","2");System.out.println(jedis.hget("hash","k2"));Map<String,String> map=new HashMap<String,String>();map.put("k3","3");map.put("k4","4");jedis.hmset("hash1",map);//setjedis.sadd("k6","6");jedis.sadd("k6","7");jedis.sadd("k6","7");Set<String> set1 = jedis.smembers("k6");for (Iterator iterator =set1.iterator(); iterator.hasNext();){String string=(String)iterator.next();System.out.println(string);}jedis.srem("orders","jd002");System.out.println(jedis.smembers("orders").size());jedis.zadd("k9",20d,"9");}
}
二、Lettuce
package com.example.redis;import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;import java.util.List;/*** @ClassName Mydemo2* @Description TODO* @Author lukcy* @Date 2024/6/23 11:07* @Version 1.0*/
public class Mydemo2 {public static void main(String[] args) {
// RedisURI.builder().redis("127.0.0.1").withPort(6379).withAuthentication("default","11111").build();RedisURI build = RedisURI.builder().redis("127.0.0.1").withPort(6379).build();//获得客户端RedisClient redisClient = RedisClient.create(build);StatefulRedisConnection<String, String> connect = redisClient.connect();//通过conn 获得commandRedisCommands commands = connect.sync();//业务逻辑List keys = commands.keys("*");for (Object key : keys) {System.out.println(key);}//释放connect.close();redisClient.shutdown();}
}
三、RedisTemplate(重点)
单机
springboot可以整合的,lettuce其实就被整合到了这个包里。
pom
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
service.java
package com.example.redis.controller;import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;/*** @ClassName Service1* @Description TODO* @Author lukcy* @Date 2024/6/23 16:19* @Version 1.0*/
@Service
@Slf4j
public class Service1 {public static final String ORDER_KEY = "ord:";@Resourceprivate RedisTemplate redisTemplate;public void addorder(){int keyId = ThreadLocalRandom.current().nextInt(1000)+1;String serialNo = UUID.randomUUID().toString();String key= ORDER_KEY+keyId;String value ="京东订单"+serialNo;redisTemplate.opsForValue().set(key,value);log.info("***key:{}",key);log.info("***value:{}",value);
}public String getOrdeByID(Integer key_id){return (String) redisTemplate.opsForValue().get(ORDER_KEY+key_id);}
}
controller.java
package com.example.redis.controller;import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
@Slf4j
@Tag(name = "订单接口")
public class HelloController {@Autowiredpublic Service1 service1;@GetMapping("order/add")@Operation(summary = "新增订单", description = "新增订单")public void addOrd() {service1.addorder();}@GetMapping("order/{id}")@Operation(summary = "查询订单", description = "查询订单")public String getorder( @PathVariable Integer id){return service1.getOrdeByID(id);}}
3.1 springboot 整合swagger
博客链接
springboot3 整合swagger2和3时都会报错,可以看上边博客。
之后我们进入swagger
可以在这里直接进行http请求。
3.2 序列化+中文问题
在上边代码 我们使用的是RedisTemplate 它使用默认的序列化器 所以我们会看到存储到redis中显示
:
并不是我们存储的ord+序号
如果我们想要是我们存储的 可以使用StringRedisTemplate
package com.example.redis.controller;import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;/*** @ClassName Service1* @Description TODO* @Author lukcy* @Date 2024/6/23 16:19* @Version 1.0*/
@Service
@Slf4j
public class Service1 {public static final String ORDER_KEY = "ord:";@Resourceprivate StringRedisTemplate stringRedisTemplate;public void addorder(){int keyId = ThreadLocalRandom.current().nextInt(1000)+1;String serialNo = UUID.randomUUID().toString();String key= ORDER_KEY+keyId;String value ="京东订单"+serialNo;stringRedisTemplate.opsForValue().set(key,value);log.info("***key:{}",key);log.info("***value:{}",value);
}public String getOrdeByID(Integer key_id){return stringRedisTemplate.opsForValue().get(ORDER_KEY+key_id);}
}
这样子存储的就是
如果在redis中 要显示中文 可以在后边加上–raw 如果还不是中文 可以先输入
之后在进入客户端
就是中文了。
集群
3.3 applications配置
3.4 问题
如果一个主机宕机,虽然再服务器redis上已经从机上机了,故障迁移成功,但是java服务器
并没有检测到最新的消息。
解决方法:开启自动刷新