基于 Protobuf 的 RPC/REST 迷你框架 pbgo

码农软件 · 软件分类 · REST/RESTful项目 · 2019-03-10 16:28:38

软件介绍

基于Protobuf定义接口规范,通过pbgo提供的插件生成RPC和REST相关代码。

创建hello.proto文件,定义接口规范:

syntax = "proto3";
package hello_pb;

import "github.com/chai2010/pbgo/pbgo.proto";

message String {
    string value = 1;
}

service EchoService {
    rpc Echo (String) returns (String) {
        option (pbgo.rest_api) = {
            get: "/echo/:value"
        };
    }
}

用pbgo插件生成代码:

$ protoc -I=. -I=$(GOPATH)/src --pbgo_out=. hello.proto

创建REST服务:

type EchoService struct{}

func (p *EchoService) Echo(request *hello_pb.String, reply *hello_pb.String) error {
    *reply = *request
    return nil
}

func main() {
    router := hello_pb.EchoServiceHandler(new(EchoService))
    log.Fatal(http.ListenAndServe(":8080", router))
}

测试REST服务:

$ curl localhost:8080/echo/gopher
{"value":"gopher"}

$ curl localhost:8080/echo/gopher?value=cgo
{"value":"cgo"}

详细的例子请参考:https://github.com/chai2010/pbgo/blob/master/examples/hello.p

本文地址:https://codercto.com/soft/d/1051.html

Head First Servlets & JSP(中文版)

Head First Servlets & JSP(中文版)

(美)巴萨姆、(美)塞若、(美)贝茨 / 苏钰函、林剑 / 中国电力出版社 / 2006-10 / 98.00元

《Head First Servlets·JSP》(中文版)结合SCWCD考试大纲讲述了关于如何编写servlets和JSP代码,如何使用JSP表达式语言,如何部署Web应用,如何开发定制标记,以及会话状态、包装器、过滤器、企业设计模式等方面的知识,以一种轻松、幽默而又形象的方式让你了解、掌握servlets和JSP,并将其运用到你的项目中去。《Head First Servlets·JSP》(中......一起来看看 《Head First Servlets & JSP(中文版)》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具