Przeglądaj źródła

Redisson config

yegang 2 lat temu
rodzic
commit
1397019493

+ 30 - 0
src/main/java/com/sw/config/RedissonConfig.java

@@ -0,0 +1,30 @@
+package com.sw.config;
+
+import org.redisson.Redisson;
+import org.redisson.api.RedissonClient;
+import org.redisson.client.codec.StringCodec;
+import org.redisson.config.Config;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.ClassPathResource;
+
+import java.io.IOException;
+
+/**
+ * redisson 配置
+ *
+ * @author yegang
+ * @create 2022-02-23 13:14
+ **/
+@Configuration
+public class RedissonConfig {
+    @Bean
+    public RedissonClient configRedisson() throws IOException {
+        Config config = Config.fromYAML(new ClassPathResource("redisson.yml").getInputStream());
+        config.setCodec(new StringCodec());
+//        config.setLockWatchdogTimeout(12000);
+        RedissonClient redissonClient = Redisson.create(config);
+        return redissonClient;
+
+    }
+}

+ 45 - 0
src/main/resources/redisson.yml

@@ -0,0 +1,45 @@
+# 单节点配置
+singleServerConfig:
+  # 连接空闲超时,单位:毫秒
+  idleConnectionTimeout: 10000
+  # 连接超时,单位:毫秒
+  connectTimeout: 10000
+  # 命令等待超时,单位:毫秒
+  timeout: 3000
+  # 命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误。
+  # 如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时。
+  retryAttempts: 3
+  # 命令重试发送时间间隔,单位:毫秒
+  retryInterval: 1500
+  #  # 重新连接时间间隔,单位:毫秒
+  #  reconnectionTimeout: 3000
+  #  # 执行失败最大次数
+  #  failedAttempts: 3
+  # 密码
+  password: sunwin
+  # 单个连接最大订阅数量
+  subscriptionsPerConnection: 5
+  # 客户端名称
+  clientName: null
+  #  # 节点地址
+  address: "redis://192.168.20.83:6379"
+  # 发布和订阅连接的最小空闲连接数
+  subscriptionConnectionMinimumIdleSize: 1
+  # 发布和订阅连接池大小
+  subscriptionConnectionPoolSize: 50
+  # 最小空闲连接数
+  connectionMinimumIdleSize: 500
+  # 连接池大小
+  connectionPoolSize: 1000
+  # 数据库编号
+  database: 0
+  # DNS监测时间间隔,单位:毫秒
+  dnsMonitoringInterval: 5000
+# 线程池数量,默认值: 当前处理核数量 * 2
+threads: 16
+# Netty线程池数量,默认值: 当前处理核数量 * 2
+nettyThreads: 32
+# 编码,不使用默认编码,因为set进去之后是乱码
+#codec: !<org.redisson.codec.MarshallingCodec> {}
+# 传输模式
+transportMode : "NIO"

+ 57 - 24
src/test/java/com/sw/RedissonTest.java

@@ -24,37 +24,38 @@ public class RedissonTest {
     @Autowired
     private RedissonClient redissonClient;
     private CountDownLatch count = new CountDownLatch(2);
+
     @Test
-    public void  lock(){
-        RLock lock =   redissonClient.getLock("mask-" + 1);
-        new Thread(()->{
+    public void lock() {
+        RLock lock = redissonClient.getLock("mask-" + 1);
+        new Thread(() -> {
             String threadName = Thread.currentThread().getName();
-            log.info("线程:{} 正在尝试获取锁。。。",threadName);
+            log.info("线程:{} 正在尝试获取锁。。。", threadName);
             lock.lock(4000, TimeUnit.MILLISECONDS);
             try {
                 Thread.sleep(3000);
-                log.info("{}:业务执行完成",threadName);
+                log.info("{}:业务执行完成", threadName);
                 count.countDown();
             } catch (InterruptedException e) {
                 e.printStackTrace();
-            }finally {
+            } finally {
                 lock.unlock();
-                log.info("线程:{},释放了锁",threadName);
+                log.info("线程:{},释放了锁", threadName);
             }
         }).start();
-        new Thread(()->{
+        new Thread(() -> {
             String threadName = Thread.currentThread().getName();
-            log.info("线程:{} 正在尝试获取锁。。。",threadName);
+            log.info("线程:{} 正在尝试获取锁。。。", threadName);
             lock.lock(4000, TimeUnit.MILLISECONDS);
             count.countDown();
             try {
                 Thread.sleep(3000);
-                log.info("{}:业务执行完成",threadName);
+                log.info("{}:业务执行完成", threadName);
             } catch (InterruptedException e) {
                 e.printStackTrace();
-            }finally {
+            } finally {
                 lock.unlock();
-                log.info("线程:{},释放了锁",threadName);
+                log.info("线程:{},释放了锁", threadName);
             }
         }).start();
         try {
@@ -64,37 +65,38 @@ public class RedissonTest {
         }
         log.info("子线程都已执行完毕,main函数可以结束了!");
     }
+
     @Test
-    public void  trylock(){
-        RLock lock =   redissonClient.getLock("mask-" + 1);
-        new Thread(()->{
+    public void trylock() {
+        RLock lock = redissonClient.getLock("mask-" + 1);
+        new Thread(() -> {
             String threadName = Thread.currentThread().getName();
-            log.info("线程:{} 正在尝试获取锁。。。",threadName);
+            log.info("线程:{} 正在尝试获取锁。。。", threadName);
             try {
-                lock.tryLock(4000,TimeUnit.MILLISECONDS);
+                lock.tryLock(4000, TimeUnit.MILLISECONDS);
                 Thread.sleep(3000);
-                log.info("{}:业务执行完成",threadName);
+                log.info("{}:业务执行完成", threadName);
                 count.countDown();
             } catch (InterruptedException e) {
                 e.printStackTrace();
             } finally {
                 lock.unlock();
-                log.info("线程:{},释放了锁",threadName);
+                log.info("线程:{},释放了锁", threadName);
             }
         }).start();
-        new Thread(()->{
+        new Thread(() -> {
             String threadName = Thread.currentThread().getName();
-            log.info("线程:{} 正在尝试获取锁。。。",threadName);
+            log.info("线程:{} 正在尝试获取锁。。。", threadName);
             try {
-                lock.tryLock(4000,TimeUnit.MILLISECONDS);
+                lock.tryLock(4000, TimeUnit.MILLISECONDS);
                 Thread.sleep(3000);
-                log.info("{}:业务执行完成",threadName);
+                log.info("{}:业务执行完成", threadName);
                 count.countDown();
             } catch (InterruptedException e) {
                 e.printStackTrace();
             } finally {
                 lock.unlock();
-                log.info("线程:{},释放了锁",threadName);
+                log.info("线程:{},释放了锁", threadName);
             }
         }).start();
         try {
@@ -105,4 +107,35 @@ public class RedissonTest {
         log.info("子线程都已执行完毕,main函数可以结束了!");
     }
 
+    @Test
+    public void timeout() {
+        RLock lock = redissonClient.getLock("mask-" + 2);
+        new Thread(() -> {
+            String threadName = Thread.currentThread().getName();
+            log.info("线程:{} 正在尝试获取锁。。。", threadName);
+            boolean b = false;
+            try {
+                b = lock.tryLock(3, TimeUnit.SECONDS);
+                System.out.println(b);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            if (b) {
+                try {
+
+                    Thread.sleep(6000);
+                    log.info("{}:业务执行完成", threadName);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                } finally {
+                    lock.unlock();
+                    log.info("线程:{},释放了锁", threadName);
+                }
+            } else {
+                log.info("{}:没有获取到锁,锁已被占用", threadName);
+            }
+        }).start();
+
+    }
+
 }