Spring boot 整合 Jedis
Posted yx88
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot 整合 Jedis相关的知识,希望对你有一定的参考价值。
1 概述
1. Jedis是Redis的Java实现的客户端,其API提供了比较全面的Redis命令的支持。
2.Jedis中的方法调用是比较底层的暴露的Redis的API,也即Jedis中的Java方法基本和Redis的API保持着一致,了解Redis的API,也就能熟练的使用Jedis。
3.Jedis使用阻塞的I/O,且其方法调用都是同步的,程序流需要等到sockets处理完I/O才能执行,不支持异步。Jedis客户端实例不是线程安全的,所以需要通过连接池来使用Jedis。
4.Jedis仅支持基本的数据类型如:String、Hash、List、Set、Sorted Set。
2 pom
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
3 properties
spring.jedis.enabled = true spring.jedis.cluster = false spring.jedis.host = x.x.x.x spring.jedis.port = x spring.jedis.password = xxxxx spring.jedis.pool-config.max-total = 300
4 Jedis配置类JedisProperties
package com.yx.redis.jedis; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @Auther: yx * @Date: 2019-04-30 10:35 * @Description: RedisProperties */ @ConfigurationProperties(prefix = "spring.jedis") public class JedisProperties { boolean cluster = false; String hosts; String host = "localhost"; int port = 6379; int timeout = 600000; String password; PoolConfig poolConfig = new PoolConfig(); Cluster clusterConfig = new Cluster(); public static class Cluster { int connectionTimeout = 5000; int soTimeout = 5000; int maxAttempts = 5000; public int getConnectionTimeout() { return connectionTimeout; } public void setConnectionTimeout(int connectionTimeout) { this.connectionTimeout = connectionTimeout; } public int getSoTimeout() { return soTimeout; } public void setSoTimeout(int soTimeout) { this.soTimeout = soTimeout; } public int getMaxAttempts() { return maxAttempts; } public void setMaxAttempts(int maxAttempts) { this.maxAttempts = maxAttempts; } } public static class PoolConfig { int maxTotal = GenericObjectPoolConfig.DEFAULT_MAX_TOTAL; int maxIdle = GenericObjectPoolConfig.DEFAULT_MAX_IDLE; int minIdle = GenericObjectPoolConfig.DEFAULT_MIN_IDLE; int numTestsPerEvictionRun = GenericObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN; long timeBetweenEvictionRunsMillis = GenericObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; long minEvictableIdleTimeMillis = GenericObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; long softMinEvictableIdleTimeMillis = GenericObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS; long maxWaitMillis = GenericObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS; boolean testWhileIdle = GenericObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE; boolean blockWhenExhausted = GenericObjectPoolConfig.DEFAULT_BLOCK_WHEN_EXHAUSTED; boolean testOnCreate = GenericObjectPoolConfig.DEFAULT_TEST_ON_CREATE; boolean testOnBorrow = GenericObjectPoolConfig.DEFAULT_TEST_ON_BORROW; boolean testOnReturn = GenericObjectPoolConfig.DEFAULT_TEST_ON_RETURN; public int getMaxTotal() { return maxTotal; } public void setMaxTotal(int maxTotal) { this.maxTotal = maxTotal; } public int getMaxIdle() { return maxIdle; } public void setMaxIdle(int maxIdle) { this.maxIdle = maxIdle; } public int getMinIdle() { return minIdle; } public void setMinIdle(int minIdle) { this.minIdle = minIdle; } public int getNumTestsPerEvictionRun() { return numTestsPerEvictionRun; } public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) { this.numTestsPerEvictionRun = numTestsPerEvictionRun; } public long getTimeBetweenEvictionRunsMillis() { return timeBetweenEvictionRunsMillis; } public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; } public long getMinEvictableIdleTimeMillis() { return minEvictableIdleTimeMillis; } public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) { this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; } public long getSoftMinEvictableIdleTimeMillis() { return softMinEvictableIdleTimeMillis; } public void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis) { this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis; } public long getMaxWaitMillis() { return maxWaitMillis; } public void setMaxWaitMillis(long maxWaitMillis) { this.maxWaitMillis = maxWaitMillis; } public boolean isTestWhileIdle() { return testWhileIdle; } public void setTestWhileIdle(boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; } public boolean isBlockWhenExhausted() { return blockWhenExhausted; } public void setBlockWhenExhausted(boolean blockWhenExhausted) { this.blockWhenExhausted = blockWhenExhausted; } public boolean isTestOnCreate() { return testOnCreate; } public void setTestOnCreate(boolean testOnCreate) { this.testOnCreate = testOnCreate; } public boolean isTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } public boolean isTestOnReturn() { return testOnReturn; } public void setTestOnReturn(boolean testOnReturn) { this.testOnReturn = testOnReturn; } } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } public PoolConfig getPoolConfig() { return poolConfig; } public void setPoolConfig(PoolConfig poolConfig) { this.poolConfig = poolConfig; } public boolean isCluster() { return cluster; } public void setCluster(boolean cluster) { this.cluster = cluster; } public String getHosts() { return hosts; } public void setHosts(String hosts) { this.hosts = hosts; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Cluster getClusterConfig() { return clusterConfig; } public void setClusterConfig(Cluster clusterConfig) { this.clusterConfig = clusterConfig; } }
5 Jedis配置类JedisTemplate
package com.yx.redis.jedis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCommands; import java.util.List; import java.util.Map; import java.util.Set; /** * @Auther: yx * @Date: 2019-04-30 10:35 * @Description: JedisTemplate */ public class JedisTemplate { private JedisAction jedisAction; public JedisCommands jedisCommands; public JedisTemplate(final JedisAction jedisAction, final JedisClient jedisClient) { this.jedisAction = jedisAction; this.jedisCommands = jedisClient.getJedisCommands(); } public void set(String key, String value) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.set(key, value); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.set(key, value); } }); } public void setex(String key, String value, int time) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.setex(key, time, value); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.setex(key, time, value); } }); } public void zadd(String key, double score, String member) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.zadd(key, score, member); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.zadd(key, score, member); } }); } public void zscore(String key, String member) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.zscore(key, member); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.zscore(key, member); } }); } public void zrem(String key, String member) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.zrem(key, member); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.zrem(key, member); } }); } public String get(String key) { return jedisAction.execute(new JedisAction.ActionResult<String>() { @Override public String action(Jedis jedis) { return jedis.get(key); } @Override public String action(JedisCluster jedisCluster) { return jedisCluster.get(key); } }); } public List<String> mget(String[] key) { return jedisAction.execute(new JedisAction.ActionResult<List<String>>() { @Override public List<String> action(Jedis jedis) { return jedis.mget(key); } @Override public List<String> action(JedisCluster jedisCluster) { return jedisCluster.mget(key); } }); } public List<String> hmget(String key, String[] field) { return jedisAction.execute(new JedisAction.ActionResult<List<String>>() { @Override public List<String> action(Jedis jedis) { return jedis.hmget(key, field); } @Override public List<String> action(JedisCluster jedisCluster) { return jedisCluster.hmget(key, field); } }); } public void hset(String key, String field, String value) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.hset(key, field, value); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.hset(key, field, value); } }); } public Long hincrBy(String key, String field, int step) { return jedisAction.execute(new JedisAction.ActionResult<Long>() { @Override public Long action(Jedis jedis) { return jedis.hincrBy(key, field, step); } @Override public Long action(JedisCluster jedisCluster) { return jedisCluster.hincrBy(key, field, step); } }); } public String hget(String key, String field) { return jedisAction.execute(new JedisAction.ActionResult<String>() { @Override public String action(Jedis jedis) { return jedis.hget(key, field); } @Override public String action(JedisCluster jedisCluster) { return jedisCluster.hget(key, field); } }); } public Long hdel(String key, String field) { return jedisAction.execute(new JedisAction.ActionResult<Long>() { @Override public Long action(Jedis jedis) { return jedis.hdel(key, field); } @Override public Long action(JedisCluster jedisCluster) { return jedisCluster.hdel(key, field); } }); } public void expire(String key, int seconds) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.expire(key, seconds); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.expire(key, seconds); } }); } public boolean exists(String... keys) { return jedisAction.execute(new JedisAction.ActionResult<Boolean>() { @Override public Boolean action(Jedis jedis) { return jedis.exists(keys) > 0; } @Override public Boolean action(JedisCluster jedisCluster) { return jedisCluster.exists(keys) > 0; } }); } public Map<String, String> hgetAll(String key) { return jedisAction.execute(new JedisAction.ActionResult<Map<String, String>>() { @Override public Map<String, String> action(Jedis jedis) { return jedis.hgetAll(key); } @Override public Map<String, String> action(JedisCluster jedisCluster) { return jedisCluster.hgetAll(key); } }); } public Boolean setnxex(String key, int seconds, String value) { return "OK".equals(jedisAction.execute(new JedisAction.ActionResult<String>() { @Override public String action(Jedis jedis) { return jedis.set(key, value, "NX", "EX", seconds); } @Override public String action(JedisCluster jedisCluster) { return jedisCluster.set(key, value, "NX", "EX", seconds); } })); } public Long setnx(String key, String value) { return jedisAction.execute(new JedisAction.ActionResult<Long>() { @Override public Long action(Jedis jedis) { return jedis.setnx(key, value); } @Override public Long action(JedisCluster jedisCluster) { return jedisCluster.setnx(key, value); } }); } public Long incr(String key) { return jedisAction.execute(new JedisAction.ActionResult<Long>() { @Override public Long action(Jedis jedis) { return jedis.incr(key); } @Override public Long action(JedisCluster jedisCluster) { return jedisCluster.incr(key); } }); } public Long incrBy(String key, int step) { return jedisAction.execute(new JedisAction.ActionResult<Long>() { @Override public Long action(Jedis jedis) { return jedis.incrBy(key, step); } @Override public Long action(JedisCluster jedisCluster) { return jedisCluster.incrBy(key, step); } }); } public void sadd(String key, String value) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.sadd(key, value); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.sadd(key, value); } }); } public Set<String> smembers(String key) { return jedisAction.execute(new JedisAction.ActionResult<Set<String>>() { @Override public Set<String> action(Jedis jedis) { return jedis.smembers(key); } @Override public Set<String> action(JedisCluster jedisCluster) { return jedisCluster.smembers(key); } }); } public void srem(String key, String... value) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.srem(key, value); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.srem(key, value); } }); } public void lpush(String key, String... value) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.lpush(key, value); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.lpush(key, value); } }); } public void rpush(String key, String... value) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.rpush(key, value); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.rpush(key, value); } }); } public List<String> lrange(String key, int start, int end) { return jedisAction.execute(new JedisAction.ActionResult<List<String>>() { @Override public List<String> action(Jedis jedis) { return jedis.lrange(key, start, end); } @Override public List<String> action(JedisCluster jedisCluster) { return jedisCluster.lrange(key, start, end); } }); } public void lrem(String key, int count, String value) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.lrem(key, count, value); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.lrem(key, count, value); } }); } public String setnxex(String key, String value, int timeout) { return jedisAction.execute(new JedisAction.ActionResult<String>() { @Override public String action(Jedis jedis) { return jedis.set(key, value, "NX", "EX", timeout); } @Override public String action(JedisCluster jedisCluster) { return jedisCluster.set(key, value, "NX", "EX", timeout); } }); } public void del(String... key) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.del(key); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.del(key); } }); } public String hmset(String key, Map<String, String> map) { return jedisAction.execute(new JedisAction.ActionResult<String>() { @Override public String action(Jedis jedis) { return jedis.hmset(key, map); } @Override public String action(JedisCluster jedisCluster) { return jedisCluster.hmset(key, map); } }); } public Long setnxByte(byte[] key, byte[] value) { return jedisAction.execute(new JedisAction.ActionResult<Long>() { @Override public Long action(Jedis jedis) { return jedis.setnx(key, value); } @Override public Long action(JedisCluster jedisCluster) { return jedisCluster.setnx(key, value); } }); } public void setByte(byte[] key, byte[] value) { jedisAction.execute(new JedisAction.ActionNoResult() { @Override public void action(Jedis jedis) { jedis.set(key, value); } @Override public void action(JedisCluster jedisCluster) { jedisCluster.set(key, value); } }); } public byte[] getByte(byte[] key) { return jedisAction.execute(new JedisAction.ActionResult<byte[]>() { @Override public byte[] action(Jedis jedis) { return jedis.get(key); } @Override public byte[] action(JedisCluster jedisCluster) { return jedisCluster.get(key); } }); } /** * 位图 * * @param key * @param offset * @param value * @return 修改之前的状态 */ public Boolean setBit(String key, Long offset, boolean value) { return jedisAction.execute(new JedisAction.ActionResult<Boolean>() { @Override public Boolean action(Jedis jedis) { return jedis.setbit(key, offset, value); } @Override public Boolean action(JedisCluster jedisCluster) { return jedisCluster.setbit(key, offset, value); } }); } public Boolean getBit(String key, Long offset) { return jedisAction.execute(new JedisAction.ActionResult<Boolean>() { @Override public Boolean action(Jedis jedis) { return jedis.getbit(key, offset); } @Override public Boolean action(JedisCluster jedisCluster) { return jedisCluster.getbit(key, offset); } }); } public Set<String> zrevrange(String key, long start, long end) { return jedisAction.execute(new JedisAction.ActionResult<Set<String>>() { @Override public Set<String> action(Jedis jedis) { return jedis.zrevrange(key, start, end); } @Override public Set<String> action(JedisCluster jedisCluster) { return jedisCluster.zrevrange(key, start, end); } }); } public Object eval(String script, List<String> keys, List<String> args) { return jedisAction.execute(new JedisAction.ActionResult<Object>() { @Override public Object action(Jedis jedis) { return jedis.eval(script, keys, args); } @Override public Object action(JedisCluster jedisCluster) { return jedisCluster.eval(script, keys, args); } }); } }
6 Jedis配置类JedisCondition
package com.yx.redis.jedis;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* @Auther: yx
* @Date: 2019-04-30 10:35
* @Description: KafkaCondition
*/
public class JedisCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return propertyIsTrue(context, "spring.jedis.enabled");
}
private static boolean propertyIsTrue(ConditionContext context, String key) {
return context.getEnvironment().getProperty(key, Boolean.TYPE, Boolean.FALSE);
}
}
7 Jedis配置类JedisClient
package com.yx.redis.jedis; import lombok.Getter; import lombok.Setter; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCommands; import redis.clients.jedis.JedisPool; /** * @Auther: yx * @Date: 2019-04-30 14:16 * @Description: JedisClient */ public class JedisClient { @Setter private boolean cluster = false; @Setter private JedisPool jedisPool; @Setter @Getter private JedisCluster jedisCluster; public JedisCommands getJedisCommands() { if (cluster) { return jedisCluster; } else { return getJedis(); } } public Jedis getJedis() { return jedisPool.getResource(); } }
8 Jedis配置类JedisAction
package com.yx.redis.jedis; import lombok.Setter; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.ShardedJedis; /** * @Auther: yx * @Date: 2019-04-30 14:30 * @Description: JedisAction */ public class JedisAction { @Setter private boolean isCluster = false; @Setter private JedisClient jedisClient; interface ActionResult<T>{ T action(Jedis jedis); T action(JedisCluster jedisCluster); } interface ActionNoResult{ void action(Jedis jedis); void action(JedisCluster jedisCluster); } public void execute(ActionNoResult actionNoResult){ if(isCluster){ JedisCluster jedisCluster = jedisClient.getJedisCluster(); actionNoResult.action(jedisCluster); }else{ Jedis jedis = null; try { jedis = jedisClient.getJedis(); actionNoResult.action(jedis); }finally { if(jedis != null){ jedis.close(); } } } } public <T> T execute(ActionResult<T> actionResult){ if(isCluster){ ShardedJedis shardedJedis = null; try { JedisCluster jedisCluster = jedisClient.getJedisCluster(); return actionResult.action(jedisCluster); }finally { if(shardedJedis != null){ shardedJedis.close(); } } }else{ Jedis jedis = null; try { jedis = jedisClient.getJedis(); return actionResult.action(jedis); }finally { if(jedis != null){ jedis.close(); } } } } }
以上是关于Spring boot 整合 Jedis的主要内容,如果未能解决你的问题,请参考以下文章