开发了一个小系统,命名为MySQL Rest Data Service(简称:MRDS)。MRDS的主要功能是针对MySQL数据中的表,自动公开CRUD的Rest API服务。
我们只需要在数据库中创建好表后,通过MRDS就可以公开自动该表的CRUD Rest API了。包括以下Rest API,
HOST_IP:PORT/rest///{id}
HOST_IP:PORT/rest//
HOST_IP:PORT/rest//
HOST_IP:PORT/rest///bulk
HOST_IP:PORT/rest///{id}
HOST_IP:PORT/rest///{id}
接下来,让我们体验一下MRDS的基本功能,
docker run --name mysql -e MYSQL_ROOT_PASSWORD=oracle -p 3306:3306 -d mysql:8.0.31
docker run --name mrds --rm -p 8080:8080 --link mysql:mysql -e MYSQL_USERNAME=root -e MYSQL_PASSWORD=oracle -e MYSQL_URL=jdbc:mysql://mysql:3306/mysql atjapan2015/mrds:0.0.1
cat <<"EOF" > db.sql
CREATE TABLE `region` (
`id` varchar(100) NOT NULL,
`region_id` varchar(100) NOT NULL,
`region_name` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `region` (`id`, `region_id`, `region_name`) VALUES ("ap-melbourne-1", "ap-melbourne-1", "オーストラリア南東部(メルボルン)");
INSERT INTO `region` (`id`, `region_id`, `region_name`) VALUES ("ap-osaka-1", "ap-osaka-1", "日本中央部(大阪)");
INSERT INTO `region` (`id`, `region_id`, `region_name`) VALUES ("ap-sydney-1", "ap-sydney-1", "オーストラリア東部(シドニー)");
INSERT INTO `region` (`id`, `region_id`, `region_name`) VALUES ("ap-tokyo-1", "ap-tokyo-1", "日本東部(東京)");
INSERT INTO `region` (`id`, `region_id`, `region_name`) VALUES ("sa-saopaulo-1", "sa-saopaulo-1", "ブラジル東部(サンパウロ)");
COMMIT;
EOF
导入数据,
docker exec -i mysql mysql -uroot -poracle -c --default-character-set=utf8 mysql < db.sql
curl localhost:8080/rest/mysql/region/ap-tokyo-1
输出如下,
{"id":"ap-tokyo-1","region_name":"日本東部(東京)","region_id":"ap-tokyo-1"}
curl localhost:8080/rest/mysql/region
输出如下,
[{"id":"ap-melbourne-1","region_name":"オーストラリア南東部(メルボルン)","region_id":"ap-melbourne-1"},{"id":"ap-osaka-1","region_name":"日本中央部(大阪)","region_id":"ap-osaka-1"},{"id":"ap-sydney-1","region_name":"オーストラリア東部(シドニー)","region_id":"ap-sydney-1"},{"id":"ap-tokyo-1","region_name":"日本東部(東京)","region_id":"ap-tokyo-1"},{"id":"sa-saopaulo-1","region_name":"ブラジル東部(サンパウロ)","region_id":"sa-saopaulo-1"}]
curl -X POST -H 'Content-Type: application/json' -d '{"id":"ap-chunchoeu-1","region_name":"韩国春川1","region_id":"ap-chunchoeu-1"}' localhost:8080/rest/mysql/region
确认数据正常插入,
curl localhost:8080/rest/mysql/region/ap-chunchoeu-1
输出结果如下,
{"id":"ap-chunchoeu-1","region_name":"韩国春川1","region_id":"ap-chunchoeu-1"}
curl -X POST -H 'Content-Type: application/json' -d '[{"id":"ap-chunchoeu-2","region_name":"韩国春川2","region_id":"ap-chunchoeu-2"}, {"id":"ap-chunchoeu-3","region_name":"韩国春川3","region_id":"ap-chunchoeu-3"}]' localhost:8080/rest/mysql/region/bulk
curl -X PUT -H 'Content-Type: application/json' -d '{"id":"ap-chunchoeu-1","region_name":"更新后韩国春川1","region_id":"ap-chunchoeu-1"}' localhost:8080/rest/mysql/region/ap-chunchoeu-1
curl -X DELETE localhost:8080/rest/mysql/region/ap-chunchoeu-1
完结!