redis连接池

master
王堂东 2023-10-29 15:03:38 +08:00
commit b1a4f06a19
9 changed files with 311 additions and 0 deletions

38
.gitignore vendored 100644
View File

@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
.idea/.gitignore vendored 100644
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

14
.idea/misc.xml 100644
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

55
pom.xml 100644
View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wtd</groupId>
<artifactId>RedisPool</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>RedisPool</name>
<description>RedisPool</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,17 @@
package com.wtd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @program: RedisPool
* @description:
* @author: Mr.Wang
* @create: 2023-10-29 14:50
**/
@SpringBootApplication
public class RedisPoolApplication {
public static void main(String[] args) {
SpringApplication.run(RedisPoolApplication.class);
}
}

View File

@ -0,0 +1,31 @@
package com.wtd.pool;
import redis.clients.jedis.Jedis;
/**
* @program: RedisPool
* @description:
* @author: Mr.Wang
* @create: 2023-10-29 10:09
**/
public interface IRedisPool {
/**
*
* @param maxTotal
* @param maxWaitMillis
*/
public void init(int maxTotal,long maxWaitMillis);
/**
*
* @return
* @throws Exception
*/
public Jedis getResource() throws Exception;
/**
*
* @param jedis
*/
public void release(Jedis jedis);
}

View File

@ -0,0 +1,85 @@
package com.wtd.pool;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @program: RedisPool
* @description:
* @author: Mr.Wang
* @create: 2023-10-29 11:11
**/
@Component
public class MyRedisPool implements IRedisPool{
private int maxTotal;
private long maxWaitMillis;
//定义一个空闲的连接集合
private LinkedBlockingDeque<Jedis> freeObjects=null;
//定义一个活动的连接集合
private LinkedBlockingDeque<Jedis> activeObjects=null;
//总的redis连接数量
private final AtomicInteger count=new AtomicInteger();
@Override
public void init(int maxTotal, long maxWaitMillis) {
this.maxTotal=maxTotal;
this.maxWaitMillis=maxWaitMillis;
freeObjects=new LinkedBlockingDeque<>(maxTotal);
activeObjects=new LinkedBlockingDeque<>(maxTotal);
}
@Override
public Jedis getResource() throws Exception {
//记录开始时间 用于判断超时时间
long startTime=System.currentTimeMillis();
//从空闲连接集合中获取连接,如果获取到了一个空闲的连接 将该链接放入到活动链接集合中 返回这个连接
Jedis redis= null;
while (redis==null) {
redis = freeObjects.poll();
if(redis!=null){
activeObjects.offer(redis);
return redis;
}
//如果无法从空闲连接集合中拿到连接,判断连接池是否已满,如果没有满,创建一个新的连接,并放入到活动连接集合中,返回这个连接
if(count.get()<maxTotal){
if(count.incrementAndGet()<=maxTotal){
redis=new Jedis("127.0.0.1",6379);
System.out.println("redis连接池创建了一个连接");
activeObjects.offer(redis);
return redis;
}else{
count.decrementAndGet();
}
}
//如果线程池满了,等待其他线程释放连接到空闲连接集合中,如果说一定时间之内,可以得到一个空闲的连接,将连接放入到活动连接结合中,返回连接
try {
redis=freeObjects.poll(maxWaitMillis-(System.currentTimeMillis()-startTime), TimeUnit.MILLISECONDS);
if(redis!=null){
activeObjects.offer(redis);
return redis;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
//如果等待时间超过了我们最长等待超时时间,抛出等待超时异常
if(maxWaitMillis<(System.currentTimeMillis()-startTime)){
throw new Exception("timeout,超过最长等待连接时间");
}else{
continue;
}
}
return redis;
}
@Override
public void release(Jedis jedis) {
if(activeObjects.remove(jedis)){
freeObjects.offer(jedis);
}
}
}

View File

@ -0,0 +1,56 @@
import com.wtd.pool.IRedisPool;
import com.wtd.pool.MyRedisPool;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;
import javax.annotation.Resource;
import java.util.concurrent.CountDownLatch;
/**
* @program: RedisPool
* @description:
* @author: Mr.Wang
* @create: 2023-10-29 10:46
**/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyRedisPool.class)
public class RedisPoolTest {
@Resource
private IRedisPool pool;
private final static int THREAD_NUM=20;
private final CountDownLatch cdl=new CountDownLatch(THREAD_NUM);//栅栏
@Test
public void test() throws Exception{
//连接池的初始化
pool.init(10,2000);
//模拟并发的场景对redis进行一些操作 目的是分析连接池中是否只创建了指定的连接数
for (int i = 0; i < THREAD_NUM; i++) {
new Thread(() ->{
try {
cdl.await();//等待一个指令 所有等待的线程同时进行
} catch (Exception e) {
e.printStackTrace();
}
Jedis jedis=null;
try {
jedis=pool.getResource();
} catch (Exception e) {
e.printStackTrace();
}finally {
pool.release(jedis);
}
}).start();
cdl.countDown();
}
Thread.sleep(2000);
}
}