0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

spring-boot 4のredisでコネクションプーリングを有効化

0
Posted at

ソースコードと説明

dockerで適当なredisを起動する。

docker run --name some-redis -d redis -p 6379:6379
build.gradle
plugins {
    id 'java'
    id 'org.springframework.boot' version '4.1.1'
    id 'io.spring.dependency-management' version '1.1.7'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    implementation 'org.apache.commons:commons-pool2'
    testImplementation 'org.springframework.boot:spring-boot-starter-data-redis-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    useJUnitPlatform()
}
src/main/resources/application.properties
spring.data.redis.host=127.0.0.1
spring.data.redis.port=6379

#spring.data.redis.lettuce.pool.enabled=true
spring.data.redis.lettuce.pool.max-active=10
spring.data.redis.lettuce.pool.max-idle=10
spring.data.redis.lettuce.pool.min-idle=10

依存性にorg.apache.commons:commons-pool2を追加すると自動的にコネクションプーリングが有効になる。spring.data.redis.lettuce.pool.enabledの明示的なtrueは設定してもしなくても自動的に有効になる。設定値が分かりやすいようにコネクションが10個起動するようにしておく。

spring-bootを起動した状態でredis-cli で確認する。

 docker exec -it some-redis bash
root@023b96c54364:/data# redis-cli
127.0.0.1:6379> info clients
# Clients
connected_clients:11
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:0
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
pubsub_clients:0
watching_clients:0
clients_in_timeout_table:0
active_clients:1
total_watched_keys:0
total_blocking_keys:0
total_blocking_keys_on_nokey:0

connected_clients:11のとおり10 + 1コネクションが張られているのが分かる。

その他メモ

java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig

クラスパスにorg.apache.commons:commons-pool2が無いのにspring.data.redis.lettuce.pool.enabled=trueにすると実行時にエラーになる。

Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) ~[na:na]

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?