Skip to content

Commit

Permalink
feat: Support for multiple rpc service generation and rpc grouping (#…
Browse files Browse the repository at this point in the history
…1972)

* Add group & compatible flag

* Add group & compatible flag

* Support for multiple rpc service generation and rpc grouping

* Support for multiple rpc service generation and rpc grouping

* Format code

* Format code

* Add comments

* Fix unit test

* Refactor function name

* Add example & Update grpc readme

* go mod tidy

* update mod

* update mod
  • Loading branch information
kesonan committed Jul 21, 2022
1 parent 1b51d0c commit ca3c687
Show file tree
Hide file tree
Showing 47 changed files with 2,289 additions and 361 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -54,6 +54,6 @@ require (
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/net v0.0.0-20220531201128-c960675eff93 // indirect
google.golang.org/genproto v0.0.0-20220602131408-e326c6e8e9c8 // indirect
google.golang.org/genproto v0.0.0-20220602131408-e326c6e8e9c8
k8s.io/klog/v2 v2.40.1 // indirect
)
17 changes: 17 additions & 0 deletions tools/goctl/example/rpc/hello.proto
@@ -0,0 +1,17 @@
syntax = "proto3";

package hello;

option go_package = "./hello";

message HelloReq {
string in = 1;
}

message HelloResp {
string msg = 1;
}

service Greet {
rpc SayHello(HelloReq) returns (HelloResp);
}
37 changes: 37 additions & 0 deletions tools/goctl/example/rpc/hello/client/greet/greet.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tools/goctl/example/rpc/hello/etc/hello.yaml
@@ -0,0 +1,6 @@
Name: hello.rpc
ListenOn: 127.0.0.1:8080
Etcd:
Hosts:
- 127.0.0.1:2379
Key: hello.rpc
39 changes: 39 additions & 0 deletions tools/goctl/example/rpc/hello/hello.go
@@ -0,0 +1,39 @@
package main

import (
"flag"
"fmt"

"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/config"
greetServer "github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/server/greet"
"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/svc"
"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/pb/hello"

"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

var configFile = flag.String("f", "etc/hello.yaml", "the config file")

func main() {
flag.Parse()

var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)

s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
hello.RegisterGreetServer(grpcServer, greetServer.NewGreetServer(ctx))

if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer)
}
})
defer s.Stop()

fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
s.Start()
}
7 changes: 7 additions & 0 deletions tools/goctl/example/rpc/hello/internal/config/config.go
@@ -0,0 +1,7 @@
package config

import "github.com/zeromicro/go-zero/zrpc"

type Config struct {
zrpc.RpcServerConf
}
@@ -0,0 +1,30 @@
package greetlogic

import (
"context"

"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/svc"
"github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/pb/hello"

"github.com/zeromicro/go-zero/core/logx"
)

type SayHelloLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}

func NewSayHelloLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SayHelloLogic {
return &SayHelloLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}

func (l *SayHelloLogic) SayHello(in *hello.HelloReq) (*hello.HelloResp, error) {
// todo: add your logic here and delete this line

return &hello.HelloResp{}, nil
}
28 changes: 28 additions & 0 deletions tools/goctl/example/rpc/hello/internal/server/greet/greetserver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tools/goctl/example/rpc/hello/internal/svc/servicecontext.go
@@ -0,0 +1,13 @@
package svc

import "github.com/zeromicro/go-zero/tools/goctl/example/rpc/hello/internal/config"

type ServiceContext struct {
Config config.Config
}

func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
}
}

0 comments on commit ca3c687

Please sign in to comment.