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 方法请求
原文地址:https://www.cnblogs.com/zhrea/p/6805904.html