Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Connecting a Spring Boot Java application to MaxScale with read-write splitting

NotificationsYou must be signed in to change notification settings

mariadb-developers/read-write-split-java-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License

This example shows how to useMariaDB MaxScale as a database proxy to load balance writes toprimary servers and reads to replicas.

MariaDB MaxScale database proxy

Set up the database cluster using Docker

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

Configure the database proxy (MariaDB MaxScale)

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

Create the table

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);

Run the web application

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).

Support and Contribution

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp