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