#1
Struts has long been a solid framework for Java web apps. When you want to boost performance—like for caching sessions, throttling, or simple pub/sub—you can’t go wrong with Redis. This guide walks through setting up Redis in a Struts application using Docker for local development and Kubernetes for production, with clean separation between environments and minimal fuss.

1. Add Redis & Kubernetes Dependencies

Add these to your pom.xml to get Redis connectivity and optional Kubernetes integration:
<dependencies>
  <!-- Struts framework -->
  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>${struts.version}</version>
  </dependency>

  <!-- Jedis Redis client -->
  <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.4.3</version>
  </dependency>

  <!-- Kubernetes client (optional) -->
  <dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>kubernetes-client</artifactId>
    <version>${fabric8.version}</version>
  </dependency>
</dependencies>
You’ll have Struts for web logic, Jedis for Redis, and Kubernetes support for production configurations.

2. Run Redis Locally via Docker

For development, keep it simple with Redis in Docker:
version: '3'
services:
  redis:
    image: redis:7.0-alpine
    ports:
      - "6379:6379"
Run it:
docker-compose up -d
Redis is now available at localhost:6379, ready for caching and quick data storage.

3. Create Dev Configuration: redis-dev.properties

Place this in your src/main/resources:
redis.host=localhost>redis.port=6379
redis.timeout=2000
In your Struts helper or action class, load these settings:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/redis-dev.properties"));

JedisPoolConfig poolConfig = new JedisPoolConfig();
JedisPool pool = new JedisPool(
    poolConfig,
    props.getProperty("redis.host"),
    Integer.parseInt(props.getProperty("redis.port")),
    Integer.parseInt(props.getProperty("redis.timeout"))
);

// Later in code:
try (Jedis jedis = pool.getResource()) {
    jedis.set("myKey", "Hello, Redis!");
}
That gives you fast local development with working caching or key-value access.

4. Prepare Kubernetes for Production

In production, keep sensitive config out of code by using Kubernetes ConfigMaps and Secrets:
kubectl create configmap redis-config \
  --from-literal=REDIS_HOST=redis-service \
  --from-literal=REDIS_PORT=6379 \
  --from-literal=REDIS_TIMEOUT=2000

kubectl create secret generic redis-secret \
  --from-literal=REDIS_PASSWORD=prodpass

5. Create Production Config: redis-prod.properties

Add this to your JAR under src/main/resources:
redis.host=${REDIS_HOST}
redis.port=${REDIS_PORT}
redis.timeout=${REDIS_TIMEOUT}
redis.password=${REDIS_PASSWORD}
At runtime, environment variables supplied by Kubernetes are automatically used.

6. Add Kubernetes Deployment Snippet

Ensure your pod spec injects the values:
env:
  - name: REDIS_HOST
    valueFrom:
      configMapKeyRef:
        name: redis-config
        key: REDIS_HOST
  - name: REDIS_PORT
    valueFrom:
      configMapKeyRef:
        name: redis-config
        key: REDIS_PORT
  - name: REDIS_TIMEOUT
    valueFrom:
      configMapKeyRef:
        name: redis-config
        key: REDIS_TIMEOUT
  - name: REDIS_PASSWORD
    valueFrom:
      secretKeyRef:
        name: redis-secret
        key: REDIS_PASSWORD
This ensures your Struts app reads secure parameters at launch.

7. Code Example in Production

Your Struts-based helper might look like:
JedisPoolConfig poolConfig = new JedisPoolConfig();
JedisPool pool = new JedisPool(
    poolConfig,
    System.getenv("REDIS_HOST"),
    Integer.parseInt(System.getenv("REDIS_PORT")),
    Integer.parseInt(System.getenv("REDIS_TIMEOUT")),
    System.getenv("REDIS_PASSWORD")
);

// Executing a command
try (Jedis jedis = pool.getResource()) {
    String val = jedis.get("someKey");
}
You can publish or cache freely, with the same codebase in both environments.

Why This Setup Makes Sense

  • Quick dev loop: Docker + local config = fast iteration.
  • Secure production: No credentials hardcoded; Kubernetes manages them securely.
  • Single codebase: Same logic works in development and production.
  • Easy separation: Distinct dev/prod configuration files minimize mistakes.
With Docker, Kubernetes, and Redis, you can give your Struts application a responsive, cache-ready data layer that behaves consistently across environments. Whether you're serving user sessions or caching API responses, this setup is simple to build, test locally, and scale securely in production.
#ads

image quote pre code