micro api
micro very powerful, this article will explain the function of the command-line micro api detailed
important things to say three times
- In this paper all the code https://idea.techidea8.com/open/idea.shtml?id=6
- In this paper all the code https://idea.techidea8.com/open/idea.shtml?id=6
- In this paper all the code https://idea.techidea8.com/open/idea.shtml?id=6
main role
The main role is to provide support for micro http gateway service. If the name for the back-end services go.micro.srv.hello
, providing call
method, we used to call the micro-services via http protocol.
curl http://127.0.0.0:8080/hello/call?name=123
micro api
All parameters following instructions
$micro api -h
NAME:
micro api - Run the api gateway
USAGE:
micro api [command options] [arguments...]
OPTIONS:
--address value Set the api address e.g 0.0.0.0:8080 [%MICRO_API_ADDRESS%]
--handler value Specify the request handler to be used for mapping HTTP requests to services; {api, event, http, rpc} [%MICRO_API_HANDLER%]
--namespace value Set the namespace used by the API e.g. com.example.api [%MICRO_API_NAMESPACE%]
--resolver value Set the hostname resolver used by the API {host, path, grpc} [%MICRO_API_RESOLVER%]
--enable_rpc Enable call the backend directly via /rpc [%MICRO_API_ENABLE_RPC%]
translate
--address value 设置网关访问的ip和端口,如设置成0.0.0.0:8080,则可以通过http://127.0.0.1:8080/进行访问,默认为0.0.0.0:8080
--handler value 指定映射具体哪种类型的处理程序,可选择{api, event, http, rpc} ,默认是rpc
--namespace value 指定暴露哪些微服务,通过命名空间匹配,如指定value为`go.micro.srv`,则该空间下所有微服务都能被访问
--resolver value 路径和微服务的对应关系,默认是micro,可选项为{host, path, grpc}
--enable_rpc 是否支持直接通过rcp接口进行访问,默认false
apihander
We --handler=api
type options, to achieve http api
mapping between the type of processing services, the code reading as follows
- Download
handlerapi.zip
and unzip%GOPATH%/techidea8.com/microapp/doc/
the decompression after the directory is as follows
E:\winlion\gopath\src\techidea8.com\microapp\doc\handerapi>ls
go.mod go.sum handerapi.go proto/
- Initialization module
#这一步必须做,否则会报错`build command-line-arguments: cannot load techidea8.com/microapp/doc/handerapi/proto:`
>go mod init
>go: creating new go.mod: module techidea8.com/microapp/doc/handerapi
- Generating proto file, replace GOPATH own path.
>protoc --proto_path=E:/winlion/gopath/src --proto_path=. --go_out=. --micro_out=. proto/handerapi.proto
#注意网络上很多文档使用--proto_path=import_proto_path:. 这种格式指定path但是在win10这种格式是行不通的
#另外一种可行的格式是 -IE:/winlion/gopath/src -I. 如下也是可行的
>protoc -IE:/winlion/gopath/src -I. --go_out=. --micro_out=. proto/handerapi.proto
- run
>go run handerapi.go
2019/08/24 20:59:32 Transport [http] Listening on [::]:54208
2019/08/24 20:59:32 Broker [http] Connected to [::]:54209
2019/08/24 20:59:32 Registry [mdns] Registering node: go.micro.api.example-71c8b1fa-f84b-4cf6-957f-617f67a4083c
- Check the service status
>micro list services
go.micro.api
go.micro.api.example
- test
>curl "http://localhost:8080/example/example1/func1?name=winlion"
{"msg":"我们已经收到你的请求啦winlion"}
eventhander
We pass --handler=event
options to achieve the release event
event, the key code is interpreted as follows
- Download
handlerevent.zip
and unzip%GOPATH%/techidea8.com/microapp/doc/
the decompression after the directory is as follows
E:\winlion\gopath\src\techidea8.com\microapp\doc\handlerevent>ls
handlerevent.go
The core code is as follows
service := micro.NewService(micro.Name("test1"))
service.Init()
//订阅go.micro.evt.test事件,注意这个test
micro.RegisterSubscriber("go.micro.evt.test", service.Server(), new(TestEvent))
if err := service.Run(); err != nil {
log.Fatal(err)
}
- Run Service
>go run handlerevent.go
2019/08/24 23:17:18 Transport [http] Listening on [::]:57093
2019/08/24 23:17:18 Broker [http] Connected to [::]:57094
2019/08/24 23:17:19 Registry [mdns] Registering node: test1-7efd3b0a-5455-4456-870d-3b34e80f1354
2019/08/24 23:17:19 Subscribing test1-7efd3b0a-5455-4456-870d-3b34e80f1354 to topic: go.micro.evt.test
- Check the service status
>micro list services
go.micro.api
test1
topic:go.micro.evt.test
- Start event publishing support
>micro api --handler=event --namespace=go.micro.evt
2019/08/24 23:06:28 Registering API Event Handler at /
2019/08/24 23:06:28 HTTP API Listening on [::]:8080
2019/08/24 23:06:28 Transport [http] Listening on [::]:56778
2019/08/24 23:06:28 Broker [http] Connected to [::]:56779
2019/08/24 23:06:28 Registry [mdns] Registering node: go.micro.api-c342fe3f-4a06-4955-be3f-79284e580467
- test
>curl -d "{\"message\": \"Hello,Winlion\"}" "http://localhost:8080/test/login" -X POST
#服务器侧会显示如下数据
>2019/08/24 23:18:54 Process 收到事件 login {"message": "Hello,Winlion"}
Note that, in the win system, published by curl json data, need double quotes, and requires the use of escape characters
\
Note http: // localhost: 8080 / test / login the test corresponding to the test go.micro.evt.test
eventhander
We pass --handler=event
options to achieve the release event
event, the key code is interpreted as follows
- Download
handlerevent.zip
and unzip%GOPATH%/techidea8.com/microapp/doc/
the decompression after the directory is as follows
E:\winlion\gopath\src\techidea8.com\microapp\doc\handlerevent>ls
handlerevent.go
The core code is as follows
service := micro.NewService(micro.Name("test1"))
service.Init()
//订阅go.micro.evt.test事件,注意这个test
micro.RegisterSubscriber("go.micro.evt.test", service.Server(), new(TestEvent))
if err := service.Run(); err != nil {
log.Fatal(err)
}
- Run Service
>go run handlerevent.go
2019/08/24 23:17:18 Transport [http] Listening on [::]:57093
2019/08/24 23:17:18 Broker [http] Connected to [::]:57094
2019/08/24 23:17:19 Registry [mdns] Registering node: test1-7efd3b0a-5455-4456-870d-3b34e80f1354
2019/08/24 23:17:19 Subscribing test1-7efd3b0a-5455-4456-870d-3b34e80f1354 to topic: go.micro.evt.test
- Check the service status
>micro list services
go.micro.api
test1
topic:go.micro.evt.test
- Start event publishing support
>micro api --handler=event --namespace=go.micro.evt
2019/08/24 23:06:28 Registering API Event Handler at /
2019/08/24 23:06:28 HTTP API Listening on [::]:8080
2019/08/24 23:06:28 Transport [http] Listening on [::]:56778
2019/08/24 23:06:28 Broker [http] Connected to [::]:56779
2019/08/24 23:06:28 Registry [mdns] Registering node: go.micro.api-c342fe3f-4a06-4955-be3f-79284e580467
- test
>curl -d "{\"message\": \"Hello,Winlion\"}" "http://localhost:8080/test/login" -X POST
#服务器侧会显示如下数据
>2019/08/24 23:18:54 Process 收到事件 login {"message": "Hello,Winlion"}
Note that, in the win system, published by curl json data, need double quotes, and requires the use of escape characters
\
Note http: // localhost: 8080 / test / login the test corresponding to the test go.micro.evt.test
rpchander
We --handler=rpc
type options, to achieve http rpc
mapping between the service, rpc api type and type is quite similar, you can refer apihanlder relevant code, rpchander code package as `handlerrpc ·
- Download
handlerrpc.zip
and unzip%GOPATH%/techidea8.com/microapp/doc/
the decompression after the directory is as follows
E:\winlion\gopath\src\techidea8.com\microapp\doc\handlerrpc>ls
handerapi.go proto/
- Initialization module
#这一步必须做,否则会报错`build command-line-arguments: cannot load techidea8.com/microapp/doc/handerapi/proto:`
>go mod init
>go: creating new go.mod: module techidea8.com/microapp/doc/handlerrpc
- Generating proto file, replace GOPATH own path.
>protoc --proto_path=. --go_out=. --micro_out=. proto/handlerrpc.proto
#注意网络上很多文档使用--proto_path=import_proto_path:. 这种格式指定path但是在win10这种格式是行不通的
#另外一种可行的格式是 -IE:/winlion/gopath/src -I. 如下也是可行的
>protoc -IE:/winlion/gopath/src -I. --go_out=. --micro_out=. proto/handerapi.proto
- run
>go run handlerrpc.go
2019/08/24 20:59:32 Transport [http] Listening on [::]:54208
2019/08/24 20:59:32 Broker [http] Connected to [::]:54209
2019/08/24 20:59:32 Registry [mdns] Registering node: go.micro.api.example-71c8b1fa-f84b-4cf6-957f-617f67a4083c
- Check the service status
>micro list services
go.micro.api
go.micro.api.model1
- test
>curl -H "Content-Type: application/json" -d "{\"arg\": \"Winlion\"}" "http://localhost:8080/model1/model1/action1"
{"data":"接收到数据Winlion"}
And api handler rpchandler difference lies apihandler cross proto model data package once again on the basis rpc.
httphander
We --handler=http
type options, http
service mapping service, the core code is as follows
- New
handlerweb.go
code is as follows
//handlerweb.go
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/micro/go-micro/web"
)
//hello 处理函数,打印helloworld
func hello(w http.ResponseWriter, req *http.Request) {
//json支持
w.Header().Add("content-type", "application/json;charset=utf-8")
if err := json.NewEncoder(w).Encode(map[string]interface{}{
"code": 0,
"message": "hello,world",
}); err != nil {
//页面报错显示信息
http.Error(w, err.Error(), http.StatusNotFound)
}
}
func main() {
//new 一个web服务
service := web.NewService(web.Name("go.micro.web.hello"))
//绑定映射方法
service.HandleFunc("/", hello)
//初始化
service.Init()
//运行
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
- Start the application
>go run handlerweb.go
2019/08/24 23:58:14 Listening on [::]:57984
- Start api service
>micro api --handler=http --namespace=go.micro.web
2019/08/24 23:58:42 Registering API HTTP Handler at /{service:[a-zA-Z0-9]+}
2019/08/24 23:58:42 HTTP API Listening on [::]:8080
2019/08/24 23:58:42 Transport [http] Listening on [::]:57992
2019/08/24 23:58:42 Broker [http] Connected to [::]:57993
2019/08/24 23:58:42 Registry [mdns] Registering node: go.micro.api-fecb79d6-0175-4d1c-9243-29c1d616b70d
- test
>curl http://127.0.0.1:8080/hello/
{"code":0,"message":"hello,world"}
#注意其中的hello 对应微服务`go.micro.web.hello`中的hello
enable_rpc
This effect is on rpc direct access support, provided enable_rpc = true through the interface provides access to the following
- Open rpc support
>>micro api --handler=rpc --namespace=go.micro.api --enable_rpc=true
- New Micro Services
>micro new --type=srv techidea8.com/microapp/doc/enablerpc
Creating service go.micro.srv.enablerpc in E:\winlion\gopath\src\techidea8.com\microapp\doc\e
.
├── main.go
├── plugin.go
├── handler
│ └── enablerpc.go
├── subscriber
│ └── enablerpc.go
├── proto\enablerpc
│ └── enablerpc.proto
├── Dockerfile
├── Makefile
├── README.md
└── go.mod
download protobuf for micro:
brew install protobuf
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u github.com/micro/protoc-gen-micro
compile the proto file enablerpc.proto:
cd E:\winlion\gopath\src\techidea8.com\microapp\doc\enablerpc
protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/enablerpc/enablerpc.proto
Note that, because the win does not recognize --proto_path =:. $ GOPATH / src , so we need to deal with its own script below, wherein E:/winlion/gopath
please replace the respective GOPATH
>cd /d E:\winlion\gopath\src\techidea8.com\microapp\doc\enablerpc
>protoc --proto_path=. --proto_path=E:/winlion/gopath/src --go_out=. --micro_out=. proto/enablerpc/enablerpc.proto
- Start Service
#启动微服务
>go run main.go
#启动rpc支持
> micro api --handler=rpc --namespace=go.micro.srv --enable_rpc=true
- test
> curl -H "Content-Type: application/json" -d "{\"service\": \"go.micro.srv.enablerpc\",\"method\": \"Enablerpc.Call\", \"request\": {\"name\": \"Winlion\"}}" "http://localhost:8080/rpc"
#返回如下数据
{"msg":"Hello Winlion"}
Before reading this article you may need to read as follows
Recommended Reading
Language open source project golang go backstage management framework restgo-admin
Support touch gestures, you can slide around the calendar plugin