- Notifications
You must be signed in to change notification settings - Fork1
mariadb-developers/read-write-split-java-app
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This example shows how to useMariaDB MaxScale as a database proxy to load balance writes toprimary servers and reads to replicas.
Clone this repository:
git clone https://github.com/mariadb-developers/read-write-split-java-app.git
The Docker image used in this demo comes fromthis GitHub repository. The image is available on DockerHub, so you can simply run the containers using Docker Compose:
# cd read-write-split-java-appdocker compose up -d
Alternatively, you can deploy the MariaDB database cluster on Docker Swarm to distribute the servers on multiple machines:
# (optional alternative, run only on a Docker Swarm)docker stack deploy -c docker-compose.yaml mariadb
Access the MaxScale web GUI athttp://localhost:8989. Log in using the following credentials:
- Username:
admin
- Password:
mariadb
In theDashboard, click onmdb_monitor to access the monitor configuration and enable:
- automatic failover, to automatically promote a a replica as a new primary when the primary fails, and
- auto rejoin, to make a failed server automatically rejoin the cluster when it recovers
In theDashboard, clickquery_router_service and enable:
- transaction reply, to automatically retry in-flight transactions that might have failed during a failover
Use the MaxScale GUI to create the table. In the main menu, go toWorkspace, click onRun Queries, and connect to the MariaDB database cluster using the following credentials:
- Listener name:
query_router_listener
- Username:
user
- Password:
Password123!
Run the following SQL statement:
CREATE OR REPLACETABLEdemo.person( idINTPRIMARY KEY AUTO_INCREMENT, nameVARCHAR(255)NOT NULL, credit_card_numberVARCHAR(20), write_server_idINTNOT NULL DEFAULT (@@server_id), read_server_idINTAS (@@server_id) VIRTUAL);
Build and run the Java web application using Maven:
# cd read-write-split-java-appmvn
Alternatively you can build it withmvn package -P production
and run it withjava -jar target/webapp.jar
.
Access the application in your browser athttp://localhost:8080. Insert and update data and refresh the table to see how writes are performed on one server ID (the one corresponding to the primary node), but reads are load-balanced on other servers (replicas).
Stop the primary node:
docker stop server-1
Note: Remember to configure MaxScale for automatic failover and rejoin!
MaxScale should promote a replica as the new primary and the web application should remain fully functional.
If you start the stopped container, it should rejoin the cluster as a replica.
To start it:
docker start server-1
To shutdown the database cluster run:
docker compose down
Add-v
to the above command if you want to remove the related Docker volumes as well (you'll lose all the configuration and data).
Please feel free to submit PR's, issues or requests to this projectdirectly.
If you have any other questions, comments, or looking for more informationon MariaDB please check out:
Or reach out to us directly via:
About
Connecting a Spring Boot Java application to MaxScale with read-write splitting
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.