Skip to content

Commit

Permalink
feat: add client conf
Browse files Browse the repository at this point in the history
  • Loading branch information
xujianhai666 committed Mar 21, 2020
1 parent 1056368 commit f45f679
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
47 changes: 44 additions & 3 deletions protocol/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
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)
}
Expand Down
17 changes: 17 additions & 0 deletions protocol/grpc/codec.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
/*
* 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


import (
"bytes"
"encoding/json"
Expand Down
50 changes: 50 additions & 0 deletions protocol/grpc/config.go
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

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

func (c *ServerConfig) Validate() error {
return nil
}

0 comments on commit f45f679

Please sign in to comment.