v3 refactor (#1868)

* Move to v3

Co-authored-by: Ben Toogood <bentoogood@gmail.com>
This commit is contained in:
Asim Aslam 2020-07-27 13:22:00 +01:00 committed by Vasiliy Tolstov
parent c69131b98c
commit b021fcb130
15 changed files with 57 additions and 67 deletions

View File

@ -8,8 +8,8 @@ import (
"github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/micro/go-micro/v2/codec" "github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v2/codec/bytes" "github.com/micro/go-micro/v3/codec/bytes"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/encoding" "google.golang.org/grpc/encoding"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"

View File

@ -3,7 +3,7 @@ package grpc
import ( import (
"context" "context"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v3/server"
) )
func setServerOption(k, v interface{}) server.Option { func setServerOption(k, v interface{}) server.Option {

View File

@ -3,7 +3,7 @@ package grpc
import ( import (
"net/http" "net/http"
"github.com/micro/go-micro/v2/errors" "github.com/micro/go-micro/v3/errors"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
) )

View File

@ -5,7 +5,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v3/registry"
) )
func extractValue(v reflect.Type, d int) *registry.Value { func extractValue(v reflect.Type, d int) *registry.Value {

View File

@ -5,7 +5,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v3/registry"
) )
type testHandler struct{} type testHandler struct{}

22
grpc.go
View File

@ -15,17 +15,17 @@ import (
"time" "time"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v3/broker"
"github.com/micro/go-micro/v2/errors" "github.com/micro/go-micro/v3/errors"
pberr "github.com/micro/go-micro/v2/errors/proto" pberr "github.com/micro/go-micro/v3/errors/proto"
"github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v3/logger"
meta "github.com/micro/go-micro/v2/metadata" meta "github.com/micro/go-micro/v3/metadata"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v3/server"
"github.com/micro/go-micro/v2/util/addr" "github.com/micro/go-micro/v3/util/addr"
"github.com/micro/go-micro/v2/util/backoff" "github.com/micro/go-micro/v3/util/backoff"
mgrpc "github.com/micro/go-micro/v2/util/grpc" mgrpc "github.com/micro/go-micro/v3/util/grpc"
mnet "github.com/micro/go-micro/v2/util/net" mnet "github.com/micro/go-micro/v3/util/net"
"golang.org/x/net/netutil" "golang.org/x/net/netutil"
"google.golang.org/grpc" "google.golang.org/grpc"

View File

@ -5,21 +5,20 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/micro/go-micro/v2" bmemory "github.com/micro/go-micro/v3/broker/memory"
bmemory "github.com/micro/go-micro/v2/broker/memory" "github.com/micro/go-micro/v3/client"
"github.com/micro/go-micro/v2/client" gcli "github.com/micro/go-micro/v3/client/grpc"
gcli "github.com/micro/go-micro/v2/client/grpc" "github.com/micro/go-micro/v3/errors"
"github.com/micro/go-micro/v2/errors" pberr "github.com/micro/go-micro/v3/errors/proto"
pberr "github.com/micro/go-micro/v2/errors/proto" rmemory "github.com/micro/go-micro/v3/registry/memory"
rmemory "github.com/micro/go-micro/v2/registry/memory" "github.com/micro/go-micro/v3/router"
"github.com/micro/go-micro/v2/router" rtreg "github.com/micro/go-micro/v3/router/registry"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v3/server"
gsrv "github.com/micro/go-micro/v2/server/grpc" gsrv "github.com/micro/go-micro/v3/server/grpc"
tgrpc "github.com/micro/go-micro/v2/transport/grpc" pb "github.com/micro/go-micro/v3/server/grpc/proto"
tgrpc "github.com/micro/go-micro/v3/transport/grpc"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
pb "github.com/micro/go-micro/v2/server/grpc/proto"
) )
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.
@ -117,7 +116,7 @@ func TestGRPCServer(t *testing.T) {
r := rmemory.NewRegistry() r := rmemory.NewRegistry()
b := bmemory.NewBroker() b := bmemory.NewBroker()
tr := tgrpc.NewTransport() tr := tgrpc.NewTransport()
rtr := router.NewRouter(router.Registry(r)) rtr := rtreg.NewRouter(router.Registry(r))
s := gsrv.NewServer( s := gsrv.NewServer(
server.Broker(b), server.Broker(b),
@ -136,10 +135,7 @@ func TestGRPCServer(t *testing.T) {
h := &testServer{} h := &testServer{}
pb.RegisterTestHandler(s, h) pb.RegisterTestHandler(s, h)
if err := micro.RegisterSubscriber("test_topic", s, h.Handle); err != nil { if err := s.Subscribe(s.NewSubscriber("test_topic", h.Handle)); err != nil {
t.Fatal(err)
}
if err := micro.RegisterSubscriber("error_topic", s, h.HandleError); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -159,11 +155,10 @@ func TestGRPCServer(t *testing.T) {
} }
}() }()
pub := micro.NewEvent("test_topic", c)
pubErr := micro.NewEvent("error_topic", c)
cnt := 4 cnt := 4
for i := 0; i < cnt; i++ { for i := 0; i < cnt; i++ {
if err = pub.Publish(ctx, &pb.Request{Name: fmt.Sprintf("msg %d", i)}); err != nil { msg := c.NewMessage("test_topic", &pb.Request{Name: fmt.Sprintf("msg %d", i)})
if err = c.Publish(ctx, msg); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -171,9 +166,6 @@ func TestGRPCServer(t *testing.T) {
if h.msgCount != cnt { if h.msgCount != cnt {
t.Fatalf("pub/sub not work, or invalid message count %d", h.msgCount) t.Fatalf("pub/sub not work, or invalid message count %d", h.msgCount)
} }
if err = pubErr.Publish(ctx, &pb.Request{}); err == nil {
t.Fatal("this must return error, as we return error from handler")
}
cc, err := grpc.Dial(s.Options().Address, grpc.WithInsecure()) cc, err := grpc.Dial(s.Options().Address, grpc.WithInsecure())
if err != nil { if err != nil {

View File

@ -3,8 +3,8 @@ package grpc
import ( import (
"reflect" "reflect"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v3/server"
) )
type rpcHandler struct { type rpcHandler struct {

View File

@ -5,12 +5,11 @@ import (
"crypto/tls" "crypto/tls"
"net" "net"
"github.com/micro/go-micro/v2/auth" "github.com/micro/go-micro/v3/broker/http"
"github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v2/codec" "github.com/micro/go-micro/v3/registry/mdns"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v3/server"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v3/transport"
"github.com/micro/go-micro/v2/transport"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/encoding" "google.golang.org/grpc/encoding"
) )
@ -67,11 +66,10 @@ func MaxMsgSize(s int) server.Option {
func newOptions(opt ...server.Option) server.Options { func newOptions(opt ...server.Option) server.Options {
opts := server.Options{ opts := server.Options{
Auth: auth.DefaultAuth,
Codecs: make(map[string]codec.NewCodec), Codecs: make(map[string]codec.NewCodec),
Metadata: map[string]string{}, Metadata: map[string]string{},
Broker: broker.DefaultBroker, Broker: http.NewBroker(),
Registry: registry.DefaultRegistry, Registry: mdns.NewRegistry(),
Transport: transport.DefaultTransport, Transport: transport.DefaultTransport,
Address: server.DefaultAddress, Address: server.DefaultAddress,
Name: server.DefaultName, Name: server.DefaultName,

View File

@ -12,9 +12,9 @@ import (
import ( import (
context "context" context "context"
api "github.com/micro/go-micro/v2/api" api "github.com/micro/go-micro/v3/api"
client "github.com/micro/go-micro/v2/client" client "github.com/micro/go-micro/v3/client"
server "github.com/micro/go-micro/v2/server" server "github.com/micro/go-micro/v3/server"
) )
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.

View File

@ -1,8 +1,8 @@
package grpc package grpc
import ( import (
"github.com/micro/go-micro/v2/codec" "github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v2/codec/bytes" "github.com/micro/go-micro/v3/codec/bytes"
) )
type rpcRequest struct { type rpcRequest struct {

View File

@ -1,7 +1,7 @@
package grpc package grpc
import ( import (
"github.com/micro/go-micro/v2/codec" "github.com/micro/go-micro/v3/codec"
) )
type rpcResponse struct { type rpcResponse struct {

View File

@ -14,8 +14,8 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v3/logger"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v3/server"
) )
var ( var (

View File

@ -3,7 +3,7 @@ package grpc
import ( import (
"context" "context"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v3/server"
"google.golang.org/grpc" "google.golang.org/grpc"
) )

View File

@ -7,12 +7,12 @@ import (
"runtime/debug" "runtime/debug"
"strings" "strings"
"github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v3/broker"
"github.com/micro/go-micro/v2/errors" "github.com/micro/go-micro/v3/errors"
"github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v3/logger"
"github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v3/metadata"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v3/server"
) )
const ( const (