springboot&Redis
作者:互联网
.opsForValue().setIfAbsent(lockKey, 1); 加锁 .expire(key, timeout, unit); 超时时间 .delete(lockKey) 解锁 boolean absent = redisTemplate.opsForValue().setIfAbsent("mykey","myvluesss",5, TimeUnit.SECONDS); * 获取指定位置长度的缓存列表 * List<Object> queues = getRedisTemplate(key).opsForList().range(key, startIdx, length); 向列表添加元素 .opsForList().rightPush(key, value); .opsForList().size(key); .opsForList().leftPop(key); .opsForList().range(key, 0, -1); .opsForZSet().range(key, 0, -1); .opsForZSet().remove(key, String.valueOf(id)); .opsForZSet().add(key, String.valueOf(id), currentTimeMillis); .opsForSet().pop(key); .opsForValue().increment(key, delta); 返回值 计数 .hasKey(key) .opsForHash().get(key1, key2) .opsForValue().set(key, val) .opsForValue().set(key, val, timeout, unit) .opsForList().range(key, 0, length) 插入队列 List<Object> queues = .opsForList().range(key, 0, Integer.MAX_VALUE); 获取队列长度 .opsForList().size(key); 从队列中删除 .opsForList().remove(key,0,PAHQueue); 从队列中取出 .opsForList().rightPop(key); 返回 PAHQueue.getUniqueKey() 插入队列 .opsForList().leftPush(key, PAHQueue); 从队列中取出 .opsForSet().remove(key, value) .opsForList().rightPush(key, value); .opsForList().rightPushAll(key, valueArray) .opsForSet().remove(key, List<String>) .opsForHash().delete(hashKey, valueKey) .opsForSet().isMember(key, value);是否存在 .opsForSet().members(key); 获取所有values .opsForSet().add(key, cacheValues) Object[] cacheValues = new String[values.size()]; cacheValues = values.toArray(cacheValues); .opsForSet().add(key, value)
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration @EnableAutoConfiguration public class RedisConfig extends CachingConfigurerSupport { // @Value("${redis.database}") private int database = 0; // @Value("${redis.password.base.name}") // private String baseName = ; // @Autowired // @Qualifier(value = "redisBasePasswordProvider") // private BasePasswordProvider basePasswordProvider; // @Value("${redis.sentinel.nodes}") private String redisNodes = "192.168.89.133:26379,192.168.89.133:26380,192.168.89.133:26381"; // @Value("${redis.sentinel.master}") private String master = "resque";//master0:name=resque,status=ok,address=192.168.89.133:6381,slaves=2,sentinels=3 // @Value("${spring.redis.pool.maxTotal}") private int maxTotal=10; // @Value("${spring.redis.pool.maxIdle}") private int maxIdle=10; // @Value("${spring.redis.pool.maxWaitMillis}") private int maxWaitMillis=60000; // @Value("${spring.redis.pool.numTestsPerEvictionRun}") private int numTestsPerEvictionRun=1; // @Value("${spring.redis.pool.minIdle}") private int minIdle = 10; /** * redis哨兵配置 * @return */ @Bean(name = "redisSentinelConfiguration") public RedisSentinelConfiguration redisSentinelConfiguration(){ RedisSentinelConfiguration configuration = new RedisSentinelConfiguration(); String[] host = redisNodes.split(","); for(String redisHost : host){ String[] item = redisHost.split(":"); String ip = item[0]; String port = item[1]; configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port))); } configuration.setMaster(master); return configuration; } @Bean(name = "jedisPoolConfig") @ConfigurationProperties(prefix="spring.redis.pool") public JedisPoolConfig getRedisConfig(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(maxTotal); config.setMaxIdle(maxIdle); config.setMaxWaitMillis(maxWaitMillis); config.setNumTestsPerEvictionRun(numTestsPerEvictionRun); config.setMinIdle(minIdle); config.setTestOnBorrow(false);//向资源池借用连接时是否做连接有效性检测(ping) config.setTestWhileIdle(true); System.out.println("jedisPoolConfigdatas"+config.toString()); return config; } /** * 连接redis的工厂类 * * @return */ @Bean(name = "jedisConnectionFactory") public JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(redisSentinelConfiguration()); factory.setPassword("123"); factory.setDatabase(0); factory.setPoolConfig(getRedisConfig()); return factory; } /** * 配置RedisTemplate * 设置添加序列化器 * key 使用string序列化器 * value 使用Json序列化器 * 还有一种简答的设置方式,改变defaultSerializer对象的实现。 * * @return */ @Bean(name = "redisTemplate") public RedisTemplate<String, Object> redisTemplate() { //StringRedisTemplate的构造方法中默认设置了stringSerializer RedisTemplate<String, Object> template = new RedisTemplate<>(); //设置开启事务 template.setEnableTransactionSupport(true); //set key serializer StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setValueSerializer(jdkSerializationRedisSerializer); template.setConnectionFactory(jedisConnectionFactory()); template.afterPropertiesSet(); return template; } } <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.1.0.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
启动三个redis主从
./src/redis-server redis.conf
连接
./src/redis-cli -p 6379
启动sentinel
./src/redis-sentinel sentinel.conf
连接sentinel
./src/redis-cli -p 26379
每个sentinel节点其实就是一个redis实例,与主从节点不同的是sentinel节点作用是用于监控redis数据节点的
sentinel只是在其外部额外添加的一组用于监控作用的redis实例
sentinel架构的主要作用是解决主从模式下主节点的故障转移工作的
:wq 保存
标签:opsForList,springboot,Redis,redis,springframework,key,org,import 来源: https://www.cnblogs.com/xingminghui111/p/14352828.html