[Payment System] springboot integrates redission distributed lock

        When doing payment-related functions, the concept of locks will be used extensively. In order to facilitate future expansion, distributed locks are used instead of memory locks.

        The functions of locks used in the payment system are basically: operating balances, receiving payment callbacks when processing orders, settlement, etc.

        The lock used in this article uses the relatively popular redission lock operation.

  •  Introduce redission
            
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

         <dependency>
             <groupId>org.redisson</groupId>
             <artifactId>redisson-spring-boot-starter</artifactId>
             <version>3.16.8</version>
         </dependency>
  • Configure application.yml (specific configuration depends on actual situation)
spring:
  redis:
    # Redis服务器地址
    host: 127.0.0.1
    # Redis服务器连接端口
    port: 6379
    # 连接超时时间
    timeout: 10s
    lettuce:
      pool:
        # 连接池最大连接数
        max-active: 200
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms
        # 连接池中的最大空闲连接
        max-idle: 10
        # 连接池中的最小空闲连接
        min-idle: 0
  • Usage
        RLock lock = redissonClient.getLock(RedisKey.BALANCE + oldUser.getAccount());
		try {
            // 上锁
            // 参数1:最大等待时间 , 参数2:最大上锁时间(超出自动解锁) ,参数3:前两个参数的时间单位
			if (lock.tryLock(3, 30, TimeUnit.SECONDS)) {
				// do something
			}
		} catch (InterruptedException e) {
			throw new ServiceException("处理失败");
		} finally {
			if (lock.isLocked()) {
                //解锁
				lock.unlock();
			}
		}

The above is how to use distributed lock redission. The next article will explain the use of redission in the payment system.

Guess you like

Origin blog.csdn.net/qq_39078783/article/details/131128337