client

2022-04-13 14:11 更新

快速上手dubbo-go,編寫一個(gè)簡單的hellowworld應(yīng)用

client

第一步:編寫消費(fèi)端的服務(wù)

  1. 編寫需要被編碼的結(jié)構(gòu)體,由于使用 Hessian2 作為編碼協(xié)議,User 需要實(shí)現(xiàn) JavaClassName 方法,它的返回值在dubbo中對(duì)應(yīng)User類的類名。
    type User struct {
    	Id   string
    	Name string
    	Age  int32
    	Time time.Time
    }
    
    func (u User) JavaClassName() string {
    	return "org.apache.dubbo.User"
    }
  2. 與服務(wù)端不同的是,提供服務(wù)的方法作為結(jié)構(gòu)體的參數(shù),不需要編寫具體業(yè)務(wù)邏輯。另外,Provider 不對(duì)應(yīng)dubbo中的實(shí)現(xiàn),而是對(duì)應(yīng)一個(gè)接口。
    type UserProvider struct {
    	GetUser func(ctx context.Context, req []interface{}, rsp *User) error
    }
    
    func (u *UserProvider) Reference() string {
    	return "UserProvider"
    }
  3. 注冊(cè)服務(wù)和對(duì)象
    func init() {
    	config.SetConsumerService(userProvider)
    	hessian.RegisterPOJO(&User{})
    }

第二步:編寫消費(fèi)端主程序

  1. 引入必需的dubbo-go包
    import (
    	hessian "github.com/apache/dubbo-go-hessian2"
    	_ "github.com/apache/dubbo-go/cluster/cluster_impl"
    	_ "github.com/apache/dubbo-go/cluster/loadbalance"
    	_ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
    	"github.com/apache/dubbo-go/config"
    	_ "github.com/apache/dubbo-go/filter/filter_impl"
    	_ "github.com/apache/dubbo-go/protocol/dubbo"
    	_ "github.com/apache/dubbo-go/registry/protocol"
    	_ "github.com/apache/dubbo-go/registry/zookeeper"
    
    	gxlog "github.com/dubbogo/gost/log"
    )
  2. main 函數(shù)
    func main() {
    	config.Load()
    	time.Sleep(3 * time.Second)
    
    	gxlog.CInfo("\n\n\nstart to test dubbo")
    	user := &pkg.User{}
    	err := userProvider.GetUser(context.TODO(), []interface{}{"A001"}, user)
    	if err != nil {
    		gxlog.CError("error: %v\n", err)
    		os.Exit(1)
    		return
    	}
    	gxlog.CInfo("response result: %v\n", user)
    }

第三步:編寫配置文件并配置環(huán)境變量

  1. 編輯配置文件
    log.yml,client.yml
    level: "debug"
    development: true
    disableCaller: false
    disableStacktrace: false
    sampling:
    encoding: "console"
    
    # encoder
    encoderConfig:
      messageKey: "message"
      levelKey: "level"
      timeKey: "time"
      nameKey: "logger"
      callerKey: "caller"
      stacktraceKey: "stacktrace"
      lineEnding: ""
      levelEncoder: "capital"
      timeEncoder: "iso8601"
      durationEncoder: "seconds"
      callerEncoder: "short"
      nameEncoder: ""
    
    outputPaths:
      - "stderr"
    errorOutputPaths:
      - "stderr"
    initialFields:

    log.yml

    # dubbo client yaml configure file
    
    check: true
    # client
    request_timeout: "3s"
    # connect timeout
    connect_timeout: "3s"
    
    # application config
    application:
      organization: "dubbo.io"
      name: "UserInfoClient"
      module: "dubbo-go user-info client"
      version: "0.0.1"
      environment: "dev"
    
    # registry config
    registries:
      "demoZk":
        protocol: "zookeeper"
        timeout: "3s"
        address: "127.0.0.1:2181"
        username: ""
        password: ""
    
    # reference config
    references:
      "UserProvider":
        registry: "demoZk"
        protocol: "dubbo"
        interface: "org.apache.dubbo.UserProvider"
        cluster: "failover"
        methods:
          - name: "GetUser"
            retries: 3
    
    # protocol config
    protocol_conf:
      dubbo:
        reconnect_interval: 0
        connection_number: 1
        heartbeat_period: "5s"
        session_timeout: "180s"
        pool_size: 64
        pool_ttl: 600
        getty_session_param:
          compress_encoding: false
          tcp_no_delay: true
          tcp_keep_alive: true
          keep_alive_period: "120s"
          tcp_r_buf_size: 262144
          tcp_w_buf_size: 65536
          pkg_rq_size: 1024
          pkg_wq_size: 512
          tcp_read_timeout: "1s"
          tcp_write_timeout: "5s"
          wait_timeout: "1s"
          max_msg_len: 1024000
          session_name: "client"
  2. 把上面的兩個(gè)配置文件分別配置為環(huán)境變量,為防止log的環(huán)境變量和服務(wù)端的log環(huán)境變量沖突,建議所有的環(huán)境變量不要做全局配置,在當(dāng)前起效即可。
    export CONF_CONSUMER_FILE_PATH="xxx"
    export APP_LOG_CONF_FILE="xxx"

本文章源碼詳情見git:https://github.com/apache/dubbo-go-samples/tree/1.5/helloworld/go-client


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)