从单机存储进化为接口和存储的分离
概述
接口服务层对外提供REST服务,数据服务层提供数据存储功能。两者之间通过消息队列进行通信,数据服务层的所有数据服务注册dataServer Exchange, 以便client给接口服务层发消息后,接口服务收到GET请求时,定位对象被保存在哪一个数据服务节点,通过dataServer Exchange找到对应的数据服务。为保证dataServer Exchange中所有的数据服务都是有效,也为了让接口服务层知道数据服务节点。建立另一个apiServers Exchange作为心跳检测,由每一个数据服务节点持续发送心跳消息。
架构图
测试代码及步骤
配置好unbuntu环境后
2010 sudo apt install golang-go2011 sudo apt install gccgo-go 2012 sudo apt-get install rabbitmq-server2014 sudo rabbitmq-plugins enable rabbitmq_management2015 wget localhost:15672/cli/rabbitmqadmin2017 python3 rabbitmqadmin declare exchange name=apiServers type=fanout2018 python3 rabbitmqadmin declare exchange name=dataServers type=fanout2019 sudo rabbitmqctl add_user test test2020 sudo rabbitmqctl set_permissions -p / test ".*" ".*" ".*"
配置运行环境变量
export GOPATH=~/go-implement-your-object-storage-master
go get github.com/streadway/amqp
sh ./tools/starttestenv.sh chapter2
测试代码
输入
curl -v 10.29.2.2:12345/objects/1234.txt -XPUT -d “add test 123”
查看
curl -v 10.29.2.2:12345/objects/1234.txt
定位在哪个服务器存储
curl -v 10.29.2.2:12345/locate/1234.txt
从服务器10.29.2.2:12345直接拿数据
curl -v 10.29.1.2:12345/objects/1234.txt
$ curl -v 10.29.2.2:12345/objects/1234.txt -XPUT -d "add test 123"
* Trying 10.29.2.2...
* TCP_NODELAY set
* Connected to 10.29.2.2 (10.29.2.2) port 12345 (#0)
> PUT /objects/1234.txt HTTP/1.1
> Host: 10.29.2.2:12345
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Length: 12
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 12 out of 12 bytes
< HTTP/1.1 200 OK
< Date: Fri, 10 Apr 2020 12:35:21 GMT
< Content-Length: 0
<
* Connection #0 to host 10.29.2.2 left intact
kou@ubuntu:~/go-implement-your-object-storage-master$ curl -v 10.29.2.2:12345/objects/1234.txt
* Trying 10.29.2.2...
* TCP_NODELAY set
* Connected to 10.29.2.2 (10.29.2.2) port 12345 (#0)
> GET /objects/1234.txt HTTP/1.1
> Host: 10.29.2.2:12345
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 10 Apr 2020 12:35:27 GMT
< Content-Length: 12
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host 10.29.2.2 left intact
add test 123kou@ubuntu:~/go-implement-your-object-storage-master$ curl -v 10.29.2.2:12345/locate/1234.* Trying 10.29.2.2...
* TCP_NODELAY set
* Connected to 10.29.2.2 (10.29.2.2) port 12345 (#0)
> GET /locate/1234.txt HTTP/1.1
> Host: 10.29.2.2:12345
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 10 Apr 2020 12:35:35 GMT
< Content-Length: 17
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host 10.29.2.2 left intact
"10.29.1.2:12345" $curl -v 10.29.1.2:12345/objects/1234.txt
* Trying 10.29.1.2...
* TCP_NODELAY set
* Connected to 10.29.1.2 (10.29.1.2) port 12345 (#0)
> GET /objects/1234.txt HTTP/1.1
> Host: 10.29.1.2:12345
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 10 Apr 2020 12:38:13 GMT
< Content-Type: text/plain; charset=utf-8
< Transfer-Encoding: chunked
<
* Connection #0 to host 10.29.1.2 left intact
goland添加环境变量
Edit congratulation>environment
rabbitmq监控
http://127.0.0.1:15672/