Skip to content

Commit

Permalink
fix dubbo plugin and feat gprc json support
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxiaoliang authored and xuxiaoliang committed Jun 8, 2020
1 parent 9390b0f commit 1614725
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
12 changes: 6 additions & 6 deletions protocol/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ var (
func init() {
// load clientconfig from consumer_config
// default use grpc
defaultClientConfig := GetDefaultClientConfig()
clientConf = &defaultClientConfig
consumerConfig := config.GetConsumerConfig()

if consumerConfig.ApplicationConfig == nil {
return
}
protocolConf := config.GetConsumerConfig().ProtocolConf
customClientConfig := GetCustomClientConfig()

if protocolConf == nil {
logger.Info("protocol_conf default use dubbo config")
Expand All @@ -61,15 +63,13 @@ func init() {
if err != nil {
panic(err)
}
err = yaml.Unmarshal(grpcConfByte, &customClientConfig)
err = yaml.Unmarshal(grpcConfByte, clientConf)
if err != nil {
panic(err)
}
}

clientConf = &customClientConfig
if clientConf == nil || len(clientConf.ContentType) == 0 {
defaultClientConfig := GetDefaultClientConfig()
if clientConf == nil || len(clientConf.ContentSubType) == 0 {
clientConf = &defaultClientConfig
}
if err := clientConf.Validate(); err != nil {
Expand All @@ -90,7 +90,7 @@ func NewClient(url common.URL) *Client {
dailOpts := make([]grpc.DialOption, 0, 4)
dailOpts = append(dailOpts, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithUnaryInterceptor(
otgrpc.OpenTracingClientInterceptor(tracer, otgrpc.LogPayloads())),
grpc.WithDefaultCallOptions(grpc.CallContentSubtype(clientConf.ContentType)))
grpc.WithDefaultCallOptions(grpc.CallContentSubtype(clientConf.ContentSubType)))
conn, err := grpc.Dial(url.Location, dailOpts...)
if err != nil {
panic(err)
Expand Down
19 changes: 13 additions & 6 deletions protocol/grpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package grpc

import (
Expand All @@ -28,29 +29,34 @@ import (
)

const (
CODEC_JSON = "json"
// json
CODEC_JSON = "json"
// proto
CODEC_PROTO = "proto"
)

func init() {
encoding.RegisterCodec(JSON{
encoding.RegisterCodec(grpcJson{
Marshaler: jsonpb.Marshaler{
EmitDefaults: true,
OrigName: true,
},
})
}

type JSON struct {
// grpcJson ...
type grpcJson struct {
jsonpb.Marshaler
jsonpb.Unmarshaler
}

func (_ JSON) Name() string {
// implements grpc encoding package Codec interface
func (_ grpcJson) Name() string {
return CODEC_JSON
}

func (j JSON) Marshal(v interface{}) (out []byte, err error) {
// implements grpc encoding package Codec interface
func (j grpcJson) Marshal(v interface{}) (out []byte, err error) {
if pm, ok := v.(proto.Message); ok {
b := new(bytes.Buffer)
err := j.Marshaler.Marshal(b, pm)
Expand All @@ -62,7 +68,8 @@ func (j JSON) Marshal(v interface{}) (out []byte, err error) {
return json.Marshal(v)
}

func (j JSON) Unmarshal(data []byte, v interface{}) (err error) {
// implements grpc encoding package Codec interface
func (j grpcJson) Unmarshal(data []byte, v interface{}) (err error) {
if pm, ok := v.(proto.Message); ok {
b := bytes.NewBuffer(data)
return j.Unmarshaler.Unmarshal(b, pm)
Expand Down
17 changes: 9 additions & 8 deletions protocol/grpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
perrors "github.com/pkg/errors"
)



type (
// ServerConfig
ServerConfig struct {
Expand All @@ -31,14 +29,14 @@ type (
// ClientConfig
ClientConfig struct {
// content type, more information refer by https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
ContentType string `default:"proto" yaml:"content_type" json:"content_type,omitempty"`
ContentSubType string `default:"proto" yaml:"content_sub_type" json:"content_sub_type,omitempty"`
}
)

// GetDefaultClientConfig ...
func GetDefaultClientConfig() ClientConfig {
return ClientConfig{
ContentType: "proto",
ContentSubType: CODEC_PROTO,
}
}

Expand All @@ -47,18 +45,21 @@ func GetDefaultServerConfig() ServerConfig {
return ServerConfig{}
}

func GetCustomClientConfig() ClientConfig {
// GetClientConfig ...
func GetClientConfig() ClientConfig {
return ClientConfig{}
}

// clientConfig Validate ...
func (c *ClientConfig) Validate() error {
if c.ContentType != CODEC_JSON && c.ContentType != CODEC_PROTO {
return perrors.Errorf(" dubbo-go grpc codec currently only support protobuf、json, %s isn't supported,"+
" please check protocol content_type config", c.ContentType)
if c.ContentSubType != CODEC_JSON && c.ContentSubType != CODEC_PROTO {
return perrors.Errorf(" dubbo-go grpc codec currently only support proto、json, %s isn't supported,"+
" please check protocol content_sub_type config", c.ContentSubType)
}
return nil
}

// severConfig Validate ...
func (c *ServerConfig) Validate() error {
return nil
}
5 changes: 2 additions & 3 deletions protocol/grpc/protoc-gen-dubbo/plugin/dubbo/dubbo.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func (g *dubboGrpc) GenerateImports(file *generator.FileDescriptor) {
g.P(`dgrpc "github.com/apache/dubbo-go/protocol/grpc"`)
g.P(`"github.com/apache/dubbo-go/protocol/invocation"`)
g.P(`"github.com/apache/dubbo-go/protocol"`)
g.P(`"github.com/apache/dubbo-go/config"`)
g.P(` ) `)
}

Expand Down Expand Up @@ -266,7 +265,7 @@ func (g *dubboGrpc) generateServerMethod(servName, fullServName string, method *
g.P(`invo := invocation.NewRPCInvocation("`, methName, `", args, nil)`)

g.P("if interceptor == nil {")
g.P("result := base.GetProxyImpl().Invoke(invo)")
g.P("result := base.GetProxyImpl().Invoke(context.Background(), invo)")
g.P("return result.Result(), result.Error()")
g.P("}")

Expand All @@ -276,7 +275,7 @@ func (g *dubboGrpc) generateServerMethod(servName, fullServName string, method *
g.P("}")

g.P("handler := func(ctx ", contextPkg, ".Context, req interface{}) (interface{}, error) {")
g.P("result := base.GetProxyImpl().Invoke(invo)")
g.P("result := base.GetProxyImpl().Invoke(context.Background(), invo)")
g.P("return result.Result(), result.Error()")
g.P("}")

Expand Down

0 comments on commit 1614725

Please sign in to comment.