How to Implement MySQL Read/Write Splitting with Xiaomi’s Gaea Middleware
This guide explains how to use Xiaomi's open‑source Gaea middleware to achieve automatic MySQL read/write separation, covering Go environment setup, Gaea compilation, configuration files, Docker deployment, testing procedures, and integration with SpringBoot.
Gaea Overview
Gaea is a MySQL‑protocol database middleware developed by Xiaomi's e‑commerce R&D team, widely used in Xiaomi Mall for order, community, and activity services. It supports sharding, SQL routing, and read/write splitting, compatible with Mycat and Kingshard routing rules.
MySQL Master‑Slave Replication
Gaea requires a MySQL master‑slave replication environment. Refer to MySQL master‑slave replication tutorials for setup.
Installing Go Environment
Download Go from https://golang.org/dl/ and extract to /mydata.
tar -zxvf go1.13.5.linux-amd64.tar.gz -C /mydata/Add /mydata/go/bin to the PATH variable.
# Edit profile
vim /etc/profile
# Append
export GOROOT=mydata/go
export PATH=$PATH:$GOROOT/bin
# Apply changes
source /etc/profileVerify installation with go version.
go version go1.13.5 linux/amd64Installing Gaea
Download the Gaea source zip from https://github.com/XiaoMi/Gaea and unzip to /mydata/gaea/. unzip Gaea-master.zip Enter /mydata/gaea/ and build with make build. make build After compilation, the executable gaea appears in /mydata/gaea/bin.
Configuration
Since etcd is not set up, change the local config file /mydata/gaea/etc/gaea.ini to use file mode: config_type=file Create a namespace JSON file (e.g., /mydata/gaea/etc/file/namespace/mall_namespace_1.json) to define master/slave connections and other settings.
{
"name": "mall_namespace_1",
"online": true,
"read_only": false,
"allowed_dbs": {"mall": true},
"slices": [{
"name": "slice-0",
"user_name": "root",
"password": "root",
"master": "192.168.6.132:3307",
"slaves": ["192.168.6.132:3308"],
"capacity": 12,
"max_capacity": 24,
"idle_timeout": 60
}],
"users": [{
"user_name": "macro",
"password": "123456",
"namespace": "mall_namespace_1",
"rw_flag": 2,
"rw_split": 1
}],
"default_slice": "slice-0"
}Running Gaea in Docker
Build a Docker image using the following Dockerfile:
# Base image
FROM golang:latest
# Add source archive
ADD Gaea-master.tar.gz /go/Gaea-master
# Move source
RUN bash -c 'mv /go/Gaea-master/Gaea-master /go/gaea'
WORKDIR /go/gaea
# Compile
RUN bash -c 'make build'
EXPOSE 13306
ENTRYPOINT ["/go/gaea/bin/gaea"]
MAINTAINER macrozhengConvert the source zip to a .tar.gz archive, then build: docker build -t gaea:1.0.2 . Copy the configuration directory to the host path /mydata/gaea-docker/etc/ and run the container:
cp -r /mydata/gaea/etc/ /mydata/gaea-docker/etc/
docker run -p 13306:13306 --name gaea \
-v /mydata/gaea-docker/etc:/go/gaea/etc \
-d gaea:1.0.2Testing Read/Write Splitting
Disable slave replication, insert a row via Gaea, and verify that the row appears only in the master database, confirming write routing, while reads return no row, confirming read routing to the slave.
Integration with SpringBoot
In a SpringBoot application, configure the datasource to point to the Gaea proxy address; no additional read/write logic is required.
References
Official Gaea repository: https://github.com/XiaoMi/Gaea
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
