Skip to content

Commit

Permalink
fix dubbo grpc json support
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxiaoliang authored and xuxiaoliang committed Jun 2, 2020
1 parent 7296533 commit 9390b0f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
17 changes: 11 additions & 6 deletions protocol/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func init() {
return
}
protocolConf := config.GetConsumerConfig().ProtocolConf
defaultClientConfig := GetDefaultClientConfig()
customClientConfig := GetCustomClientConfig()

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

clientConf = &customClientConfig
if clientConf == nil || len(clientConf.ContentType) == 0 {
defaultClientConfig := GetDefaultClientConfig()
clientConf = &defaultClientConfig
}
if err := clientConf.Validate(); err != nil {
logger.Warnf("[CheckValidity] error: %v", err)
return
panic(err)
}
}

Expand All @@ -82,7 +87,7 @@ 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()
dailOpts := make([]grpc.DialOption, 0)
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)))
Expand Down
8 changes: 6 additions & 2 deletions protocol/grpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package grpc


import (
"bytes"
"encoding/json"
Expand All @@ -28,6 +27,11 @@ import (
"google.golang.org/grpc/encoding"
)

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

func init() {
encoding.RegisterCodec(JSON{
Marshaler: jsonpb.Marshaler{
Expand All @@ -43,7 +47,7 @@ type JSON struct {
}

func (_ JSON) Name() string {
return "json"
return CODEC_JSON
}

func (j JSON) Marshal(v interface{}) (out []byte, err error) {
Expand Down
22 changes: 18 additions & 4 deletions protocol/grpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package grpc

import (
perrors "github.com/pkg/errors"
)



type (
// ServerConfig
ServerConfig struct {
Expand All @@ -24,24 +31,31 @@ 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:"application/grpc+proto" yaml:"content_type" json:"content_type,omitempty"`
ContentType string `default:"proto" yaml:"content_type" json:"content_type,omitempty"`
}
)

// GetDefaultClientConfig ...
func GetDefaultClientConfig() ClientConfig {
return ClientConfig{
ContentType: "application/grpc+proto",
ContentType: "proto",
}
}

// GetDefaultServerConfig ...
func GetDefaultServerConfig() ServerConfig {
return ServerConfig{
}
return ServerConfig{}
}

func GetCustomClientConfig() ClientConfig {
return ClientConfig{}
}

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)
}
return nil
}

Expand Down

0 comments on commit 9390b0f

Please sign in to comment.