Writing microservices with Go Micro
Go Micro 定義:
- Go語言實現
- 插件化
- 基於RPC
Go Micro 提供的接口:
- 服務發現
- 編碼
- Client/Server
- 發布/訂閱
Writing a service
1. Initialisation
micro.Flags
2. Defining the API
使用protobuf定義服務的接口。這種方式規范了api,並使用服務端與客戶端接口保持一致。
greeter.proto
syntax = "proto3";
service Greeter {
rpc Hello(HelloRequest) returns (HelloResponse) {}
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string greeting = 2;
}
上面示例proto 定義了一個服務的接口 handle Greeter,其有方法Hello,參數是 HelloRequest,返回HelloResponse
Generate the API interface
use protoc and protoc-gen-go
生成的protobuf 可以在 server 或者client的handler中使用
Implement the handler
在具體的service中,需要實現 proto 中的服務接口
Running the service
Writing a Client
客戶端 也使用proto 的 handler 方法請求