k8s_redis_sentinel_java_app_1
Redis-Sentinel Java App on Kubernetes ①
Overview
Java (Spring) app for Redis-Sentinel
We write and test a Java app for Redis-Sentinel running as Kubernetes Pods.
- Java 21
- Spring Boot 3.4.3
- Redis 8.8.2 🔗 Use the Redis-Sentinel we built here.
- Develop the Java (Spring) app in IntelliJ on a PC (laptop)
- Server: Linux (Rocky 9.6), Redis-Sentinel already running
Java Spring Project
Create a Java Spring project
Create a Java Spring project and write the application.
URL to create a Java Spring project with Spring Initializr: start.spring.io
- Project: Gradle-Groovy
- Language: Java
- Spring Boot: 4.1.0 ➡📍Change it to 3.4.3 in IntelliJ build.gradle.
- Project Metadata
- Group: com.redisgate
- Artifact: KubeRedis -> project name; the zip file uses this name
- Package name: com.redisgate.KubeRedis -> Package Name
- Packaging: Jar
- Configuration: Properties
- Java: 21 -> choose Java version
- In Dependencies, click [ADD … CTRL + B] and add the three items below.
- Spring Web
- Lombok
- Spring Data Redis(Access+Driver)
- Finally click [GENERATE CTRL + ⏎ ] and download the zip.
🖥️ Files / classes
- build.gradle file
- Dockerfile
- application.properties file
- KubeRedisApplication.java
- RedisSentinelConfig.java
- RedisOperationService.java
- RedisFailoverTestService.java
💾 build.gradle file
- Change 'org.springframework.boot' version '4.1.0' -> '3.4.3'
- Change 'webmvc' -> 'web' in two places
- Add these two libraries
implementation 'org.springframework.retry:spring-retry' // add Spring Retry
implementation 'org.springframework:spring-aspects' // AOP support - Click the elephant (Gradle) icon at the top right -> click the first menu icon 'Reload All Gradle Projects'
📄 build.gradle
💾 Dockerfile
📄 Dockerfile
💾 application.properties file
List sentinel-0 through sentinel-2 in 'spring.data.redis.sentinel.nodes' as shown below.
📄 application.properties
💾 KubeRedisApplication.java
📄 KubeRedisApplication.java
💾 RedisSentinelConfig.java
Key settings are marked with ✅ or 📍
📄 RedisSentinelConfig.java
✅ How do commandTimeout() and timeoutOptions() differ?
- 1. Spring setting: commandTimeout(Duration.ofSeconds(1))
- Where: `LettuceClientConfigurationBuilder` (Spring Data Redis upper layer)
- Role: the default synchronous command timeout Spring Data Redis uses when sending commands through the Lettuce client.
- Behavior: with `RedisTemplate` or `StringRedisTemplate`, if Redis does not respond within 1s, it throws `QueryTimeoutException` or `RedisCommandTimeoutException`.
- Notes: this is the standard default. If no TimeoutOptions are defined, Lettuce uses this value internally as the default timeout and cancels the command.
- 2. Lettuce setting: timeoutOptions(TimeoutOptions.enabled(Duration.ofSeconds(1)))
- Where: `ClientOptions` (native Lettuce driver option)
- Role: fine-grained control of each command's lifecycle and timeout strategy inside the Lettuce engine.
- Behavior: in Lettuce's async/reactive pipeline, after a command is sent, if there is no response for 1s (or the network is delayed), a custom timeout scheduler cancels the command.
- Notes:
- Finer failure control: combined with `disconnectedBehavior(REJECT_COMMANDS)`, commands waiting in the queue while disconnected or reconnecting are not stuck forever; they are cancelled after 1s.
- Dynamic timeout: `TimeoutOptions` can use not only a single `Duration` but also a `TimeoutSource` that varies by command type (READ/WRITE) or situation. (e.g. `GET` 500ms, `FLUSHALL` 5s)
💾 RedisOperationService.java
📄 RedisOperationService.java
💾 RedisFailoverTestService.java
📄 RedisFailoverTestService.java
App Docker image build and YAML
Build the App Docker image
- 1) Compile (make)
IntelliJ [KubeRedis]> ./gradlew clean bootJar - 2) Copy 'KubeRedis-0.0.1-SNAPSHOT.jar' and 'Dockerfile' to the server (~/kube/app/sentinel/).
app\IntelliJ\workspace\KubeRedis\build\libs\KubeRedis-0.0.1-SNAPSHOT.jar
app\IntelliJ\workspace\KubeRedis\Dockerfile - 3) Build the Docker image (on the server)
[~/kube/app/sentinel/] $ docker build -t spring-redis-sentinel-app:1.0 . - 4) Import the image into k3s (containerd)
[~/kube/app/sentinel/] $ docker save spring-redis-sentinel-app:1.0 | sudo k3s ctr images import -
💾 app-deployment.yaml
Write this under "~/kube/app/sentinel" on the server
📄 app-deployment.yaml
➡ The Java app and Pod YAML are ready. Next: failover test.
| << K8s Redis-Sentinel Test ③ | K8s Redis-Sentinel App ② >> |
|---|
Email
답글이 올라오면 이메일로 알려드리겠습니다.
