-
Notifications
You must be signed in to change notification settings - Fork 932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support json protocol with grpc #437
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,14 +25,53 @@ import ( | |||||
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc" | ||||||
"github.com/opentracing/opentracing-go" | ||||||
"google.golang.org/grpc" | ||||||
"gopkg.in/yaml.v2" | ||||||
) | ||||||
|
||||||
import ( | ||||||
"github.com/apache/dubbo-go/common" | ||||||
"github.com/apache/dubbo-go/common/constant" | ||||||
"github.com/apache/dubbo-go/common/logger" | ||||||
"github.com/apache/dubbo-go/config" | ||||||
) | ||||||
|
||||||
var ( | ||||||
clientConf *ClientConfig | ||||||
) | ||||||
|
||||||
func init() { | ||||||
// load clientconfig from consumer_config | ||||||
// default use grpc | ||||||
consumerConfig := config.GetConsumerConfig() | ||||||
if consumerConfig.ApplicationConfig == nil { | ||||||
return | ||||||
} | ||||||
protocolConf := config.GetConsumerConfig().ProtocolConf | ||||||
defaultClientConfig := GetDefaultClientConfig() | ||||||
if protocolConf == nil { | ||||||
logger.Info("protocol_conf default use dubbo config") | ||||||
} else { | ||||||
grpcConf := protocolConf.(map[interface{}]interface{})[GRPC] | ||||||
if grpcConf == nil { | ||||||
logger.Warnf("grpcConf is nil") | ||||||
return | ||||||
} | ||||||
grpcConfByte, err := yaml.Marshal(grpcConf) | ||||||
if err != nil { | ||||||
panic(err) | ||||||
} | ||||||
err = yaml.Unmarshal(grpcConfByte, &defaultClientConfig) | ||||||
if err != nil { | ||||||
panic(err) | ||||||
} | ||||||
} | ||||||
clientConf = &defaultClientConfig | ||||||
if err := clientConf.Validate(); err != nil { | ||||||
logger.Warnf("[CheckValidity] error: %v", err) | ||||||
return | ||||||
} | ||||||
} | ||||||
|
||||||
// Client ... | ||||||
type Client struct { | ||||||
*grpc.ClientConn | ||||||
|
@@ -43,9 +82,11 @@ type Client struct { | |||||
func NewClient(url common.URL) *Client { | ||||||
// if global trace instance was set , it means trace function enabled. If not , will return Nooptracer | ||||||
tracer := opentracing.GlobalTracer() | ||||||
conn, err := grpc.Dial(url.Location, grpc.WithInsecure(), grpc.WithBlock(), | ||||||
grpc.WithUnaryInterceptor( | ||||||
otgrpc.OpenTracingClientInterceptor(tracer, otgrpc.LogPayloads()))) | ||||||
dailOpts := make([]grpc.DialOption, 0) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
specify capacity as above |
||||||
dailOpts = append(dailOpts, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithUnaryInterceptor( | ||||||
otgrpc.OpenTracingClientInterceptor(tracer, otgrpc.LogPayloads())), | ||||||
grpc.WithDefaultCallOptions(grpc.CallContentSubtype(clientConf.ContentType))) | ||||||
conn, err := grpc.Dial(url.Location, dailOpts...) | ||||||
if err != nil { | ||||||
panic(err) | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package grpc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add blank line |
||
|
||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
) | ||
|
||
import ( | ||
"github.com/golang/protobuf/jsonpb" | ||
"github.com/golang/protobuf/proto" | ||
"google.golang.org/grpc/encoding" | ||
) | ||
|
||
func init() { | ||
encoding.RegisterCodec(JSON{ | ||
Marshaler: jsonpb.Marshaler{ | ||
EmitDefaults: true, | ||
OrigName: true, | ||
}, | ||
}) | ||
} | ||
|
||
type JSON struct { | ||
jsonpb.Marshaler | ||
jsonpb.Unmarshaler | ||
} | ||
|
||
func (_ JSON) Name() string { | ||
return "json" | ||
} | ||
|
||
func (j JSON) Marshal(v interface{}) (out []byte, err error) { | ||
if pm, ok := v.(proto.Message); ok { | ||
b := new(bytes.Buffer) | ||
err := j.Marshaler.Marshal(b, pm) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return b.Bytes(), nil | ||
} | ||
return json.Marshal(v) | ||
} | ||
|
||
func (j JSON) Unmarshal(data []byte, v interface{}) (err error) { | ||
if pm, ok := v.(proto.Message); ok { | ||
b := bytes.NewBuffer(data) | ||
return j.Unmarshaler.Unmarshal(b, pm) | ||
} | ||
return json.Unmarshal(data, v) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package grpc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add blank line |
||
|
||
type ( | ||
// ServerConfig | ||
ServerConfig struct { | ||
} | ||
|
||
// ClientConfig | ||
ClientConfig struct { | ||
// content type, more information refer by https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests | ||
ContentType string `default:"application/grpc+proto" yaml:"content_type" json:"content_type,omitempty"` | ||
} | ||
) | ||
|
||
// GetDefaultClientConfig ... | ||
func GetDefaultClientConfig() ClientConfig { | ||
return ClientConfig{ | ||
ContentType: "application/grpc+proto", | ||
} | ||
} | ||
|
||
// GetDefaultServerConfig ... | ||
func GetDefaultServerConfig() ServerConfig { | ||
return ServerConfig{ | ||
} | ||
} | ||
|
||
func (c *ClientConfig) Validate() error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always return nil? Should we check the ContentType? |
||
return nil | ||
} | ||
|
||
func (c *ServerConfig) Validate() error { | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the clientConfg always return nil.