介绍micro
Micro is a microservice toolkit. Its purpose is to simplify distributed systems development.github地址
Go Micro is a pluggable RPC framework for microservices. It is part of the Micro toolkit. github地址
使用go-micro简易搭建一个服务端
- 安装并运行consul,consul是一个服务发现软件。micro依赖于服务发现机制
安装:
Consul is the default registry/discovery for go-micro apps. It's however pluggable.https://www.consul.io/intro/getting-started/install.html
运行:
consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul
下载我的示例rep
gomicrotest
注意:有一些go的依赖需要git下来,然后需要需要安装protobuf,具体使用方法google启动 micro web
micro 自带了一个控制台 ,可以查看所有服务和接口
http://localhost:8082/
micro web
- 启动server
进入目录 server/test
go run main.go
启动成功后 在micro web http://localhost:8082/query 能选择服务的接口 mymicro -- TestUser.GetUser
直接点击 execute 可以看到 输出 证明服务正常
- 启动client
进入目录 client
go run main.go
可以看到 输出内容 证明client连接正常
- postman 测试
可以在 micro web页面 打开检查 然后在network下面拿到 刚刚的rpc 连接 然后 copy as curl
如下:
curl 'http://localhost:8082/rpc' -H 'Pragma: no-cache' -H 'Origin: http://localhost:8082' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' -H 'Content-type: application/json' -H 'Accept: */*' -H 'Cache-Control: no-cache' -H 'Referer: http://localhost:8082/query' -H 'Connection: keep-alive' --data-binary '{"service":"mymicro","method":"TestUser.GetUser","request":{}}' --compressed
postman 下 用post方法 设置下面参数进行访问
地址:http://localhost:8082/rpc
headers:Content-type: application/json
body:{"service":"mymicro","method":"TestUser.GetUser","request":{}}