Go Micro 入門筆記


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 方法請求


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM