Skip to content

Commit

Permalink
feat: support grpc-gateway (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq authored Nov 30, 2022
1 parent 4f8bd4e commit d63062f
Show file tree
Hide file tree
Showing 25 changed files with 801 additions and 726 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ jobs:
go-version: ${{ matrix.go }}
id: go

- name: Install ginkgo
run: |
go install github.com/onsi/ginkgo/v2/ginkgo@latest
sudo cp ~/go/bin/ginkgo /usr/local/bin
- name: Get dependencies
run: |
go env
Expand Down
11 changes: 7 additions & 4 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ managed:
enabled: true

# Go
go_package_prefix:
default: github.com/douyu/jupiter
# go_package_prefix:
# default: github.com/douyu/jupiter
plugins:
- remote: buf.build/protocolbuffers/plugins/go:v1.27.1-1
out: .
out: proto
opt:
- paths=source_relative
- remote: buf.build/library/plugins/go-grpc:v1.1.0-2
out: .
out: proto
opt:
- require_unimplemented_servers=false
- paths=source_relative
- remote: buf.build/grpc-ecosystem/plugins/grpc-gateway:v2.11.3-1
out: proto
opt: paths=source_relative
File renamed without changes.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/gogf/gf v1.16.9
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3
github.com/hnlq715/struct2interface v0.1.2
github.com/iancoleman/strcase v0.2.0
github.com/json-iterator/go v1.1.12
Expand Down Expand Up @@ -59,7 +60,7 @@ require (
go.uber.org/zap v1.23.0
golang.org/x/mod v0.7.0
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -99,7 +100,6 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/grokify/html-strip-tags-go v0.0.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92Bcuy
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 h1:lLT7ZLSzGLI08vc9cpd+tYmNWjdKDqyr/2L+f6U12Fk=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w=
github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE=
github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -1019,8 +1020,8 @@ google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6D
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0=
google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa h1:I0YcKz0I7OAhddo7ya8kMnvprhcWM045PmkBdMO9zN0=
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc h1:Nf+EdcTLHR8qDNN/KfkQL0u0ssxt9OhbaWCl5C0ucEI=
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc/go.mod h1:dbqgFATTzChvnt+ujMdZwITVAJHFtfyN1qUhDqEiIlk=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.0/go.mod h1:chYK+tFQF0nDUGJgXMSgLCQk3phJEuONr2DCgLDdAQM=
Expand Down
14 changes: 7 additions & 7 deletions pkg/client/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/douyu/jupiter/pkg/util/xtest/server/yell"
"github.com/douyu/jupiter/proto/testproto"
"github.com/douyu/jupiter/proto/testproto/v1"
"github.com/stretchr/testify/assert"
)

Expand All @@ -17,11 +17,11 @@ func TestDirectGrpc(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
res, err := directClient.SayHello(ctx, &testproto.HelloRequest{
res, err := directClient.SayHello(ctx, &testproto.SayHelloRequest{
Name: "hello",
})
assert.Nil(t, err)
assert.Equal(t, res.Message, yell.RespFantasy.Message)
assert.Equal(t, res.Data.Name, yell.RespFantasy.Data.Name)
})
}

Expand All @@ -35,11 +35,11 @@ func TestConfigBlockTrue(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
res, err := testproto.NewGreeterClient(conn).SayHello(ctx, &testproto.HelloRequest{
res, err := testproto.NewGreeterServiceClient(conn).SayHello(ctx, &testproto.SayHelloRequest{
Name: "hello",
})

assert.ErrorContains(t, err, "missing address")
assert.ErrorContains(t, err, "code = Unavailable desc = last connection error")
assert.Nil(t, res)
})
}
Expand All @@ -54,7 +54,7 @@ func TestAsyncConnect(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

res, err := testproto.NewGreeterClient(conn).SayHello(ctx, &testproto.HelloRequest{
res, err := testproto.NewGreeterServiceClient(conn).SayHello(ctx, &testproto.SayHelloRequest{
Name: "hello",
})
assert.NotNil(t, err)
Expand All @@ -71,7 +71,7 @@ func TestAsyncConnect(t *testing.T) {
defer cancel()

fmt.Println(conn.GetState())
res, err := testproto.NewGreeterClient(conn).SayHello(ctx, &testproto.HelloRequest{
res, err := testproto.NewGreeterServiceClient(conn).SayHello(ctx, &testproto.SayHelloRequest{
Name: "hello",
})
fmt.Println(err, res)
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/grpc/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"github.com/douyu/jupiter/pkg/server"
"github.com/douyu/jupiter/pkg/util/xtest/server/yell"
"github.com/douyu/jupiter/pkg/xlog"
"github.com/douyu/jupiter/proto/testproto"
"github.com/douyu/jupiter/proto/testproto/v1"
"google.golang.org/grpc"
)

var directClient testproto.GreeterClient
var directClient testproto.GreeterServiceClient

var testconf = `
[jupiter.logger.jupiter]
Expand Down Expand Up @@ -44,7 +44,7 @@ func init() {
cfg.Addr = l.Addr().String()

conn := newGRPCClient(cfg)
directClient = testproto.NewGreeterClient(conn)
directClient = testproto.NewGreeterServiceClient(conn)
}

func startServer(addr, name string) (net.Listener, *grpc.Server) {
Expand All @@ -59,7 +59,7 @@ func startServer(addr, name string) (net.Listener, *grpc.Server) {
grpcServer := &yell.FooServer{}
grpcServer.SetName(name)

testproto.RegisterGreeterServer(gserver, grpcServer)
testproto.RegisterGreeterServiceServer(gserver, grpcServer)
go func() {
if err := gserver.Serve(l); err != nil {
panic("failed serve:" + err.Error())
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/grpc/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"context"
"testing"

"github.com/douyu/jupiter/proto/testproto"
"github.com/douyu/jupiter/proto/testproto/v1"
"github.com/stretchr/testify/assert"
)

func TestDNS(t *testing.T) {
config := DefaultConfig()
config.Addr = "dns:///localhost:9528"
config.Debug = true
cc := testproto.NewGreeterClient(config.Build())
cc := testproto.NewGreeterServiceClient(config.Build())

res, err := cc.SayHello(context.Background(), &testproto.HelloRequest{})
res, err := cc.SayHello(context.Background(), &testproto.SayHelloRequest{})
assert.Nil(t, err)
assert.NotNil(t, res)
}
10 changes: 5 additions & 5 deletions pkg/client/grpc/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/douyu/jupiter/pkg/registry/etcdv3"
"github.com/douyu/jupiter/pkg/server"
"github.com/douyu/jupiter/proto/testproto"
"github.com/douyu/jupiter/proto/testproto/v1"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
Expand All @@ -16,14 +16,14 @@ import (
func TestETCD(t *testing.T) {
config := DefaultConfig()
config.Addr = "etcd:///grpc:srv1:v1:unkown-mode"
cc := testproto.NewGreeterClient(config.Build())
cc := testproto.NewGreeterServiceClient(config.Build())

results := make(map[string]int)

n := 10
for i := 0; i < n; i++ {
peer := peer.Peer{}
res, err := cc.SayHello(context.Background(), &testproto.HelloRequest{}, grpc.Peer(&peer))
res, err := cc.SayHello(context.Background(), &testproto.SayHelloRequest{}, grpc.Peer(&peer))
assert.Nil(t, err)
assert.NotNil(t, res)
results[peer.Addr.String()] += 1
Expand All @@ -45,7 +45,7 @@ func TestETCD(t *testing.T) {

for i := 0; i < n; i++ {
peer := peer.Peer{}
res, err := cc.SayHello(context.Background(), &testproto.HelloRequest{}, grpc.Peer(&peer))
res, err := cc.SayHello(context.Background(), &testproto.SayHelloRequest{}, grpc.Peer(&peer))
assert.Nil(t, err)
assert.NotNil(t, res)
results[peer.Addr.String()] += 1
Expand All @@ -64,7 +64,7 @@ func TestETCD(t *testing.T) {

for i := 0; i < n; i++ {
peer := peer.Peer{}
res, err := cc.SayHello(context.Background(), &testproto.HelloRequest{}, grpc.Peer(&peer))
res, err := cc.SayHello(context.Background(), &testproto.SayHelloRequest{}, grpc.Peer(&peer))
assert.Nil(t, err)
assert.NotNil(t, res)
results[peer.Addr.String()] += 1
Expand Down
3 changes: 2 additions & 1 deletion pkg/core/tests/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func RunGRPCTestCase(gtc GRPCTestCase) {

err = clientConn.Invoke(ctx, gtc.Method, gtc.Args, reply.Interface(),
grpc.Header(&metadata))
assert.Equal(ginkgoT, gtc.ExpectError, err)
assert.Equal(ginkgoT, gtc.ExpectError, err,
"expected: %s\nactually: %s", gtc.ExpectError, err)

assert.True(ginkgoT, proto.Equal(gtc.ExpectReply, reply.Interface().(proto.Message)),
"expected: %s\nactually: %s", gtc.ExpectReply, reply.Interface().(proto.Message))
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/tests/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func RunHTTPTestCase(htc HTTPTestCase) {

if htc.ExpectStatus > 0 {
assert.Equal(ginkgoT, htc.ExpectStatus, res.StatusCode(),
"expected: %s\nactually: %s", htc.ExpectStatus, res.StatusCode())
"expected: %d\nactually: %d", htc.ExpectStatus, res.StatusCode())
}

if len(htc.ExpectHeader) > 0 {
Expand Down
44 changes: 22 additions & 22 deletions pkg/util/xtest/server/yell/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"time"

"github.com/douyu/jupiter/proto/testproto"
"github.com/douyu/jupiter/proto/testproto/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand All @@ -30,16 +30,16 @@ func (s *FooServer) SetHook(f func(context.Context)) {
var ErrFoo = errors.New("error foo")

// RespFantasy ...
var RespFantasy = &testproto.HelloReply{Message: "fantasy"}
var RespFantasy = &testproto.SayHelloResponse{Data: &testproto.SayHelloResponse_Data{Name: "fantasy"}}

// RespBye ...
var RespBye = &testproto.HelloReply{Message: "bye"}
var RespBye = &testproto.SayHelloResponse{Data: &testproto.SayHelloResponse_Data{Name: "test"}}

// StatusFoo ...
var StatusFoo = status.Errorf(codes.DataLoss, ErrFoo.Error())

// SayHello ...
func (s *FooServer) SayHello(ctx context.Context, in *testproto.HelloRequest) (out *testproto.HelloReply, err error) {
func (s *FooServer) SayHello(ctx context.Context, in *testproto.SayHelloRequest) (out *testproto.SayHelloResponse, err error) {
// sleep to test cost time
time.Sleep(20 * time.Millisecond)
switch in.Name {
Expand All @@ -59,23 +59,23 @@ func (s *FooServer) SayHello(ctx context.Context, in *testproto.HelloRequest) (o
return
}

// StreamHello ...
func (s *FooServer) StreamHello(ss testproto.Greeter_StreamHelloServer) (err error) {
// // StreamHello ...
// func (s *FooServer) StreamHello(ss testproto.Greeter_StreamHelloServer) (err error) {

for {
in, _ := ss.Recv()
switch in.Name {
case "bye":
return ss.Send(RespBye)
case "needErr":
return StatusFoo
default:
return ss.Send(RespFantasy)
}
}
}
// for {
// in, _ := ss.Recv()
// switch in.Name {
// case "bye":
// return ss.Send(RespBye)
// case "needErr":
// return StatusFoo
// default:
// return ss.Send(RespFantasy)
// }
// }
// }

// StreamHello ...
func (s *FooServer) WhoServer(ctx context.Context, in *testproto.WhoServerReq) (out *testproto.WhoServerReply, err error) {
return &testproto.WhoServerReply{Message: s.name}, nil
}
// // StreamHello ...
// func (s *FooServer) WhoServer(ctx context.Context, in *testproto.WhoServerReq) (out *testproto.WhoServerReply, err error) {
// return &testproto.WhoServerReply{Message: s.name}, nil
// }
7 changes: 7 additions & 0 deletions proto/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 80720a488c9a414bb8d4a9f811084989
4 changes: 4 additions & 0 deletions proto/buf.yml → proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
version: v1

deps:
- buf.build/googleapis/googleapis

lint:
ignore:
- vendor
Expand Down
Loading

0 comments on commit d63062f

Please sign in to comment.