标签:dep print rop 主从 ppi 分享图片 mic jedis 字符串
整合代码已经过测试
1、pom
1 <!-- redisson --> 2 <dependency> 3 <groupId>org.redisson</groupId> 4 <artifactId>redisson</artifactId> 5 <version>3.5.7</version> 6 </dependency>
2、properties
# redis spring.redis.host= spring.redis.port= spring.redis.password= spring.redis.jedis.pool.max-active=500 spring.redis.jedis.pool.max-idle=1000 spring.redis.jedis.pool.max-wait=6000ms spring.redis.jedis.pool.min-idle=4
3、添加redisson配置类、这里是单机模式
1 package com.example.common.config; 2 3 import org.redisson.Redisson; 4 import org.redisson.api.RedissonClient; 5 import org.redisson.config.Config; 6 import org.springframework.beans.factory.annotation.Value; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 10 /** 11 * redisson 配置类 12 * Created on 2018/6/19 13 */ 14 @Configuration 15 public class RedissonConfig { 16 17 @Value("${spring.redis.host}") 18 private String host; 19 20 @Value("${spring.redis.port}") 21 private String port; 22 23 @Value("${spring.redis.password}") 24 private String password; 25 26 @Bean 27 public RedissonClient getRedisson(){ 28 29 Config config = new Config(); 30 config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password); 31 //添加主从配置 32 // config.useMasterSlaveServers().setMasterAddress("").setPassword("").addSlaveAddress(new String[]{"",""}); 33 34 return Redisson.create(config); 35 } 36 37 }
4、加入redisson 操作类(redissonService)
1 package com.example.common.base; 2 3 import org.redisson.api.*; 4 import org.redisson.config.Config; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import java.io.IOException; 9 10 /** 11 * redisson操作类 12 */ 13 @Service("redissonService") 14 public class RedissonService { 15 16 @Autowired 17 private RedissonClient redissonClient; 18 19 public void getRedissonClient() throws IOException { 20 Config config = redissonClient.getConfig(); 21 System.out.println(config.toJSON().toString()); 22 } 23 24 /**` 25 * 获取字符串对象 26 * 27 * @param objectName 28 * @return 29 */ 30 public <T> RBucket<T> getRBucket(String objectName) { 31 RBucket<T> bucket = redissonClient.getBucket(objectName); 32 return bucket; 33 } 34 35 /** 36 * 获取Map对象 37 * 38 * @param objectName 39 * @return 40 */ 41 public <K, V> RMap<K, V> getRMap(String objectName) { 42 RMap<K, V> map = redissonClient.getMap(objectName); 43 return map; 44 } 45 46 /** 47 * 获取有序集合 48 * 49 * @param objectName 50 * @return 51 */ 52 public <V> RSortedSet<V> getRSortedSet(String objectName) { 53 RSortedSet<V> sortedSet = redissonClient.getSortedSet(objectName); 54 return sortedSet; 55 } 56 57 /** 58 * 获取集合 59 * 60 * @param objectName 61 * @return 62 */ 63 public <V> RSet<V> getRSet(String objectName) { 64 RSet<V> rSet = redissonClient.getSet(objectName); 65 return rSet; 66 } 67 68 /** 69 * 获取列表 70 * 71 * @param objectName 72 * @return 73 */ 74 public <V> RList<V> getRList(String objectName) { 75 RList<V> rList = redissonClient.getList(objectName); 76 return rList; 77 } 78 79 /** 80 * 获取队列 81 * 82 * @param objectName 83 * @return 84 */ 85 public <V> RQueue<V> getRQueue(String objectName) { 86 RQueue<V> rQueue = redissonClient.getQueue(objectName); 87 return rQueue; 88 } 89 90 /** 91 * 获取双端队列 92 * 93 * @param objectName 94 * @return 95 */ 96 public <V> RDeque<V> getRDeque(String objectName) { 97 RDeque<V> rDeque = redissonClient.getDeque(objectName); 98 return rDeque; 99 } 100 101 102 /** 103 * 获取锁 104 * 105 * @param objectName 106 * @return 107 */ 108 public RLock getRLock(String objectName) { 109 RLock rLock = redissonClient.getLock(objectName); 110 return rLock; 111 } 112 113 /** 114 * 获取读取锁 115 * 116 * @param objectName 117 * @return 118 */ 119 public RReadWriteLock getRWLock(String objectName) { 120 RReadWriteLock rwlock = redissonClient.getReadWriteLock(objectName); 121 return rwlock; 122 } 123 124 /** 125 * 获取原子数 126 * 127 * @param objectName 128 * @return 129 */ 130 public RAtomicLong getRAtomicLong(String objectName) { 131 RAtomicLong rAtomicLong = redissonClient.getAtomicLong(objectName); 132 return rAtomicLong; 133 } 134 135 /** 136 * 获取记数锁 137 * 138 * @param objectName 139 * @return 140 */ 141 public RCountDownLatch getRCountDownLatch(String objectName) { 142 RCountDownLatch rCountDownLatch = redissonClient.getCountDownLatch(objectName); 143 return rCountDownLatch; 144 } 145 146 /** 147 * 获取消息的Topic 148 * 149 * @param objectName 150 * @return 151 */ 152 public <M> RTopic<M> getRTopic(String objectName) { 153 RTopic<M> rTopic = redissonClient.getTopic(objectName); 154 return rTopic; 155 } 156 }
5.、测试代码
package com.example.test; import com.example.common.base.RedissonService; import org.redisson.api.RLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.concurrent.TimeUnit; @Controller @RequestMapping("test") public class TestService { private static final Logger log = LoggerFactory.getLogger(TestService.class); @Autowired private RedissonService redissonService; @RequestMapping(value = "/test") @ResponseBody public void test(String recordId) { RLock lock = redissonService.getRLock(recordId); try { boolean bs = lock.tryLock(5, 6, TimeUnit.SECONDS); if (bs) { // 业务代码 log.info("进入业务代码: " + recordId); lock.unlock(); } else { Thread.sleep(300); } } catch (Exception e) { log.error("", e); lock.unlock(); } } }
标签:dep print rop 主从 ppi 分享图片 mic jedis 字符串
原文地址:https://www.cnblogs.com/milicool/p/9201271.html