service providers

2022-04-14 10:11 更新

提示用戶配置服務(wù)提供

service providers

第一步:編寫提供端的服務(wù)

  1. 編寫需要被編碼的結(jié)構(gòu)體,由于使用 Hessian2 作為編碼協(xié)議,User 需要實(shí)現(xiàn) JavaClassName 方法,它的返回值在dubbo中對應(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. 編寫業(yè)務(wù)邏輯,UserProvider 相當(dāng)于dubbo中的一個服務(wù)實(shí)現(xiàn)。需要實(shí)現(xiàn) Reference 方法,返回值是這個服務(wù)的唯一標(biāo)識,對應(yīng)dubbo的 beans 和 path 字段。
    type UserProvider struct {
    }
    
    func (u *UserProvider) GetUser(ctx context.Context, req []interface{}) (*User, error) {
    	println("req:%#v", req)
    	rsp := User{"A001", "hellowworld", 18, time.Now()}
    	println("rsp:%#v", rsp)
    	return &rsp, nil
    }
    
    func (u *UserProvider) Reference() string {
    	return "UserProvider"
    }
  3. 注冊服務(wù)和對象
    func init() {
    	config.SetProviderService(new(UserProvider))
    	// ------for hessian2------
    	hessian.RegisterPOJO(&User{})
    }

第二步:編寫主程序

  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/logger"
    	_ "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"
    )
  2. main 函數(shù)
    func main() {
      config.Load()
    }

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

  1. 設(shè)置配置文件 log.yml, server.yml
    log.yml
    level: "error"
    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:
    server.yml
    # dubbo server yaml configure file
    
    # application config
    application:
      organization: "dubbo.io"
      name: "UserInfoServer"
      module: "dubbo-go user-info server"
      version: "0.0.1"
      environment: "dev"
    
    # registry config
    registries:
      "demoZk":
        protocol: "zookeeper"
        timeout: "3s"
        address: "127.0.0.1:2181"
    
    # service config
    services:
      "UserProvider":
        registry: "demoZk"
        protocol: "dubbo"
        interface: "org.apache.dubbo.UserProvider"
        loadbalance: "random"
        warmup: "100"
        cluster: "failover"
        methods:
          - name: "GetUser"
            retries: 1
            loadbalance: "random"
    
    # protocol config
    protocols:
      "dubbo":
        name: "dubbo"
        port: 20000
    
    protocol_conf:
      dubbo:
        session_number: 700
        session_timeout: "180s"
        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: "server"
    主要編輯以下部分:
    registries 結(jié)點(diǎn)下需要配置zk的數(shù)量和地址
    services 結(jié)點(diǎn)下配置服務(wù)的具體信息,需要配置 interface 配置,修改為對應(yīng)服務(wù)的接口名,服務(wù)的key對應(yīng)第一步中 
    Provider 的 Reference 返回值
  2. 把上面的兩個配置文件分別配置為環(huán)境變量
    export CONF_PROVIDER_FILE_PATH="xxx"
    export APP_LOG_CONF_FILE="xxx"

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


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號