Skip to content
Snippets Groups Projects
Commit 65e2b21e authored by xuxiaoliang's avatar xuxiaoliang
Browse files

fix dubbo plugin and feat gprc json support

parent 927a315e
No related branches found
No related tags found
No related merge requests found
......@@ -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")
......@@ -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 {
......@@ -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)
......
......@@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package grpc
import (
......@@ -28,12 +29,14 @@ 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,
......@@ -41,16 +44,19 @@ func init() {
})
}
type JSON struct {
// grpcJson ...
type grpcJson struct {
jsonpb.Marshaler
jsonpb.Unmarshaler
}
func (_ JSON) Name() string {
// implements grpc encoding package Codec interface method
func (_ grpcJson) Name() string {
return CODEC_JSON
}
func (j JSON) Marshal(v interface{}) (out []byte, err error) {
// implements grpc encoding package Codec interface method
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)
......@@ -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 method
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)
......
......@@ -21,8 +21,6 @@ import (
perrors "github.com/pkg/errors"
)
type (
// ServerConfig
ServerConfig struct {
......@@ -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,
}
}
......@@ -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
}
......@@ -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(` ) `)
}
......@@ -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("}")
......@@ -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("}")
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment