目前常用的客户端有jedis,lettuce,redisson
共同点:都提供了基于Redis操作的Java API,只是封装程度,具体实现稍有不同
不同点:
是Redis的Java实现的客户端。支持基本的数据类型如:String、Hash、List、Set、Sorted Set。
特点:使用阻塞的I/O,方法调用同步,程序流需要等到socket处理完I/O才能执行,不支持异步操作。Jedis客户端实例不是线程安全的,需要通过连接池来使用Jedis。
优点: 分布式锁,分布式集合,可通过Redis支持延迟队列
用于线程安全同步,异步和响应使用,支持集群,Sentinel,管道和编码器。
基于Netty框架的事件驱动的通信层,其方法调用是异步的。Lettuce的API是线程安全的,所以可以操作单个Lettuce连接来完成各种操作。
org.springframework.boot
spring-boot-starter-data-redis
//在2.0版本之前默认引入是jedispool连接池,在2.0之后默认引入lettuce连接池
org.apache.commons
commons-pool2
##application.properties 添加redis连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456server.port=8945
#redis数据库默认使用db0
spring.redis.database=2
spring.redis.password=
spring.redis.port=6379
spring.redis.host=127.0.0.1
# 连接超时时间
spring.redis.timeout=5000
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=3
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=2
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=3
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1
#在关闭客户端连接之前等待任务处理完成的最长时间,在这之后,无论任务是否执行完成,都会被执行器关闭,默认100ms
spring.redis.lettuce.shutdown-timeout=100
#是否缓存空值
spring.cache.redis.cache-null-values=false
//配置类
package org.example.base.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author l
* @date Created in 2020/11/3 10:51
*/
@Configuration
@EnableCaching
public class RedisConfig {
@Value("${spring.redis.database}")
private int database;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private long timeout;
@Value("${spring.redis.lettuce.shutdown-timeout}")
private long shutDownTimeout;
@Value("${spring.redis.lettuce.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.lettuce.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.lettuce.pool.max-active}")
private int maxActive;
@Value("${spring.redis.lettuce.pool.max-wait}")
private long maxWait;
Jackson2JsonRedisSerializer
使用时引入redistemplate即可
org.springframework.boot
spring-boot-starter-data-redis
org.redisson
redisson
3.8.2
true
org.redisson
redisson-spring-boot-starter
LATEST
# Redisson 配置
singleServerConfig:
address: "redis://192.168.1.140:6379"
password: null
clientName: null
database: 15 #选择使用哪个数据库0~15
idleConnectionTimeout: 10000
pingTimeout: 1000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
reconnectionTimeout: 3000
failedAttempts: 3
subscriptionsPerConnection: 5
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
connectionMinimumIdleSize: 32
connectionPoolSize: 64
dnsMonitoringInterval: 5000
#dnsMonitoring: false
threads: 0
nettyThreads: 0
codec:
class: "org.redisson.codec.JsonJacksonCodec"
transportMode: "NIO"
spring:
redis:
redisson:
config: classpath:redisson-config.yaml
@RestController
@RequestMapping("/")
public class TeController {
@Autowired
private RedissonClient redissonClient;
static long i = 20;
static long sum = 300;
// ========================== String =======================
@GetMapping("/set/{key}")
public String s1(@PathVariable String key) {
// 设置字符串
RBucket keyObj = redissonClient.getBucket(key);
keyObj.set(key + "1-v1");
return key;
}
@GetMapping("/get/{key}")
public String g1(@PathVariable String key) {
// 设置字符串
RBucket keyObj = redissonClient.getBucket(key);
String s = keyObj.get();
return s;
}
// ========================== hash =======================-=
@GetMapping("/hset/{key}")
public String h1(@PathVariable String key) {
Ur ur = new Ur();
ur.setId(MathUtil.randomLong(1,20));
ur.setName(key);
// 存放 Hash
RMap ss = redissonClient.getMap("UR");
ss.put(ur.getId().toString(), ur);
return ur.toString();
}
@GetMapping("/hget/{id}")
public String h2(@PathVariable String id) {
// hash 查询
RMap ss = redissonClient.getMap("UR");
Ur ur = ss.get(id);
return ur.toString();
}
// 查询所有的 keys
@GetMapping("/all")
public String all(){
RKeys keys = redissonClient.getKeys();
Iterable keys1 = keys.getKeys();
keys1.forEach(System.out::println);
return keys.toString();
}
// ================== ==============读写锁测试 =============================
@GetMapping("/rw/set/{key}")
public void rw_set(){
// RedissonLock.
RBucket ls_count = redissonClient.getBucket("LS_COUNT");
ls_count.set("300",360000000l, TimeUnit.SECONDS);
}
// 减法运算
@GetMapping("/jf")
public void jf(){
String key = "S_COUNT";
// RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
// atomicLong.set(sum);
// long l = atomicLong.decrementAndGet();
// System.out.println(l);
RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
if (!atomicLong.isExists()) {
atomicLong.set(300l);
}
while (i == 0) {
if (atomicLong.get() > 0) {
long l = atomicLong.getAndDecrement();
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
i --;
System.out.println(Thread.currentThread().getName() + "->" + i + "->" + l);
}
}
}
@GetMapping("/rw/get")
public String rw_get(){
String key = "S_COUNT";
Runnable r = new Runnable() {
@Override
public void run() {
RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
if (!atomicLong.isExists()) {
atomicLong.set(300l);
}
if (atomicLong.get() > 0) {
long l = atomicLong.getAndDecrement();
i --;
System.out.println(Thread.currentThread().getName() + "->" + i + "->" + l);
}
}
};
while (i != 0) {
new Thread(r).start();
// new Thread(r).run();
// new Thread(r).run();
// new Thread(r).run();
// new Thread(r).run();
}
RBucket bucket = redissonClient.getBucket(key);
String s = bucket.get();
System.out.println("================线程已结束================================" + s);
return s;
}
}