Mall project (version 2.0) day08

One, the use of redisson framework

https://github.com/redisson/redisson/wiki/1.-Overview

1.1 Introduction

Redisson is a Java In-Memory Data Grid implemented on the basis of Redis. It not only provides a series of distributed Java objects, but also provides many distributed services. These include (BitSet, Set, Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service , Scheduler service) Redisson provides the easiest and most convenient way to use Redis. The purpose of Redisson is to promote the separation of concerns from users to Redis, so that users can focus more on processing business logic.

Insert picture description here

2. Integration


引入pom
<!-- https://mvnrepository.com/artifact/org.redisson/redisson -->
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.10.5</version>
</dependency>

Configuration

spring.redis.host=192.168.159.130
spring.redis.port=6379



@Configuration
public class GmallRedissonConfig {
    
    

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Bean
    public RedissonClient redissonClient(){
    
    
        Config config = new Config();
        config.useSingleServer().setAddress("redis://"+host+":"+port);
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
}

3. Redisson lock

Reentrant Lock (Reentrant Lock)
The Redisson distributed reentrant lock RLock Java object based on Redis implements the java.util.concurrent.locks.Lock interface. It also provides asynchronous (Async), reflective (Reactive) and RxJava2 standard interfaces.

In addition, Redisson also provides the leaseTime parameter through the locking method to specify the lock time. After this time, the lock will be automatically unlocked.

RLock lock = redisson.getLock("anyLock");
// 最常见的使用方法
lock.lock();
// 加锁以后10秒钟自动解锁
// 无需调用unlock方法手动解锁
lock.lock(10, TimeUnit.SECONDS);

// 尝试加锁,最多等待100秒,上锁以后10秒自动解锁
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
    
    
   try {
    
    
     ...
   } finally {
    
    
       lock.unlock();
   }
}

Two, redisson distributed stress test

2.1 Installation

Linux
linux directly yum -y install httpd-tools, then ab -V test

Windows

1 Check if port 80 is occupied, netstat -ano | findstr "80"
2 Download address https://www.apachehaus.com/cgi-bin/download.plx
Insert picture description here
3 After decompression, find httpd.conf in the installation directory, Modify to your own installation directory

Insert picture description here
4 Start the service

Insert picture description here
5 Command example (200 concurrent requests, a total of 1000 requests)
D:\apache24\bin>ab -c 200 -n 1000 http:nginx load balancing/stress method
6 Test results
Study the effect of redisson's distributed lock under concurrency

Guess you like

Origin blog.csdn.net/qq_42082278/article/details/113872004