Struts is a reliable Java web framework, and integrating YugabyteDB gives you access to globally distributed, PostgreSQL-compatible SQL. This guide walks through setting up YugabyteDB in Struts using Docker for local development and Kubernetes for production—keeping configs neat and code reusable across environments.
1. Add Dependencies to pom.xml
First things first, update your
pom.xml
:
<dependencies>
<!-- Struts framework -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- PostgreSQL JDBC driver (works with YugabyteDB) -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<!-- Kubernetes client (optional) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
YugabyteDB is PostgreSQL-compatible, so the PostgreSQL JDBC driver works perfectly.
2. Run YugabyteDB Locally with Docker
Set up a lightweight, distributed YugabyteDB cluster:
version: '3'
services:
yb-tserver:
image: yugabytedb/yugabyte:latest
command: ["/home/yugabyte/bin/yb-tserver", "--fs_data_dirs=/data", "--start"]
volumes:
- yb-data:/data
ports:
- "5433:5433"
yb-master:
image: yugabytedb/yugabyte:latest
command: ["/home/yugabyte/bin/yb-master", "--fs_data_dirs=/data", "--start"]
ports:
- "7100:7100"
volumes:
yb-data:
Run:
docker-compose up -d
YugabyteDB listens on
localhost:5433
—ready for local development.
3. Create Dev Configuration: yugabyte-dev.properties
Add to
src/main/resources
:
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5433/yugabyte?user=yugabyte&sslmode=disable
db.user=yugabyte
db.password=
db.validationQuery=SELECT 1
Load it via a Struts helper:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/yugabyte-dev.properties"));
Class.forName(props.getProperty("db.driver"));
try (Connection conn = DriverManager.getConnection(
props.getProperty("db.url"),
props.getProperty("db.user"),
props.getProperty("db.password"));
Statement stmt = conn.createStatement()) {
stmt.executeQuery(props.getProperty("db.validationQuery"));
}
Now you’re connected to your local YugabyteDB instance.
4. Prepare Kubernetes Config for Production
Store production database settings securely:
kubectl create configmap yugabyte-config \
--from-literal=DB_URL=jdbc:postgresql://yugabyte-service:5433/proddb?sslmode=disable \
--from-literal=DB_DRIVER=org.postgresql.Driver \
--from-literal=DB_VALIDATION_QUERY=SELECT 1
kubectl create secret generic yugabyte-secret \
--from-literal=DB_USER=produser \
--from-literal=DB_PASSWORD=prodPass123
5. Create Production Configuration: yugabyte-prod.properties
Add this to your application resources:
db.driver=${DB_DRIVER}
db.url=${DB_URL}
db.user=${DB_USER}
db.password=${DB_PASSWORD}
db.validationQuery=${DB_VALIDATION_QUERY}
At runtime, your Struts application reads these values via environment variables.
6. Kubernetes Deployment Example
Ensure your pod spec includes these environment variables:
env:
- name: DB_DRIVER
valueFrom:
configMapKeyRef:
name: yugabyte-config
key: DB_DRIVER
- name: DB_URL
valueFrom:
configMapKeyRef:
name: yugabyte-config
key: DB_URL
- name: DB_VALIDATION_QUERY
valueFrom:
configMapKeyRef:
name: yugabyte-config
key: DB_VALIDATION_QUERY
- name: DB_USER
valueFrom:
secretKeyRef:
name: yugabyte-secret
key: DB_USER
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: yugabyte-secret
key: DB_PASSWORD
Package
yugabyte-prod.properties
and deploy your Struts application. It will automatically choose prod settings.
7. Example Struts Action to Verify Connection
Here's a Struts action to check DB connectivity:
public class DbHealthAction extends ActionSupport {
public String execute() {
String url = System.getenv("DB_URL");
String user = System.getenv("DB_USER");
String pass = System.getenv("DB_PASSWORD");
String validation = System.getenv("DB_VALIDATION_QUERY");
try (Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement()) {
stmt.executeQuery(validation);
addActionMessage("Connected to YugabyteDB successfully!");
return SUCCESS;
} catch (Exception e) {
addActionError("Connection failed: " + e.getMessage());
return ERROR;
}
}
}
This works seamlessly across both environments.
Why This Approach Works
- Local dev ease: Docker and dev configs give quick, reliable testing
- Production safety: Kubernetes Secrets hold credentials securely
- Consistent code: Same logic works in both environments with no branching
- Environment clarity: Separate config files reduce errors
You now have a Struts web application ready to work with YugabyteDB—fast for local development, and production-ready with Kubernetes! Happy building and scaling.
image quote pre code