Giter Club home page Giter Club logo

go-zero-demo's Introduction

go-zero-demo

示例用法(在api中调用go-zero-rpc)

  1. 目录结构:
|—— demo/   // 根目录
****|—— api/
********|—— test.api/
****|—— rpc/
********|—— test.proto/
  1. 文件内容:

    1. demo/api/test.api
        type (
            Req {
                Id string `path:"id"`
            }
            Resp {
                Id   string `json:"id"`
                Name string `json:"name"`
            }
        )
        service test {
            @handler testGet
            get /test/:id (Req) returns (Resp)
        }
    
    1. demo/rpc/test.proto
        syntax = "proto3";
        package test;
        option go_package = "./test";
        service Test {
            rpc test(Req) returns(Resp);
        }
        message Req {
            uint32 id = 1;
        }
        message Resp {
            uint32 id = 1;
            string name = 2;
        }
    
  2. 初始化go module

cd demo
go mod init demo
  1. 生成api
cd demo/api
goctl api go -api test.api -dir .
  1. 生成rpc
cd demo/rpc
goctl rpc protoc test.proto --go_out=./types --go-grpc_out=./types --zrpc_out=.
  1. 代码地址: 点击访问

  2. 启动rpc

cd demo/rpc
go run test.go
  1. 启动api
cd demo/api
go run test.go
  1. 访问测试: localhost:8888/test/1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.