W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
dubbo: application: # 應(yīng)用配置 name: dubbo-go module: local version: 1.0.0 owner: zhaoyunxing organization: dubbo-go metadata-type: local # 元數(shù)據(jù)上報方式,默認(rèn)為本地 metadata-report: # 元數(shù)據(jù)上報配置, 不包含此字段則不開啟元數(shù)據(jù)上報,應(yīng)用級服務(wù)發(fā)現(xiàn)依賴此字段,參考例子:https://github.com/apache/dubbo-go-samples/tree/master/registry/servicediscovery protocol: nacos # 元數(shù)據(jù)上報方式,支持nacos/zookeeper address: 127.0.0.1:8848 username: "" password: "" timeout: "3s" group: "dubbo" protocols: tripleProtocol: # triple協(xié)議定義,參考例子https://github.com/apache/dubbo-go-samples/tree/master/rpc/tri name: tri # 網(wǎng)絡(luò)協(xié)議,支持tri/dubbo/jsonrpc/grpc port: 20001 dubboProtocol: # dubbo協(xié)議定義,參考例子https://github.com/apache/dubbo-go-samples/tree/master/rpc/dubbo name: dubbo port: 20000 params: # dubbo 傳輸層配置,此字段不配置則使用協(xié)議默認(rèn)值 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 config-center: # 配置中心,參考例子:https://github.com/apache/dubbo-go-samples/tree/master/configcenter protocol: nacos # 支持 nacos/zookeeper/apollo address: 127.0.0.1:8848 group: dubbo namespace: dubbo timeout: 10s params: username: nacos password: 123456 registries: # 注冊中心配置,參考例子 https://github.com/apache/dubbo-go-samples/tree/master/metrics zk: protocol: zookeeper timeout: 3s address: 127.0.0.1:2181 nacos: timeout: 5s address: 127.0.0.1:8848 etcd: address: 127.0.0.1:2379 consumer: # 客戶端配置 request_timeout: 3s filter: myClientFilter # 客戶端 filters name,多個則逗號隔開 registry-ids: zk # 使用上面定義的注冊中心id max-wait-time-for-service-discovery: 3s # 服務(wù)發(fā)現(xiàn)最長等待時間 references: GreeterImpl: protocol: dubboProtocol serialization: hessian2 # 序列化方式 interface: com.apache.dubbo.sample.basic.IGreeter # 接口名,需要與服務(wù)端一致 provider: # 服務(wù)端配置 registry-ids: zk # 使用上面定義的注冊中心id services: DubboGreeterImpl: filter: myServerFilter, myServerFilter2 # server filters name protocol-ids: dubboProtocol # 使用上面定義的協(xié)議id serialization: hessian2 # hessian 序列化方式 interface: com.apache.dubbo.sample.basic.IGreeter # 接口名,需要與客戶端一致 TripleGreeterImpl: protocol-ids: tripleProtocol # 使用上面定義的協(xié)議id serialization: protobuf # pb 序列化方式 interface: com.apache.dubbo.sample.basic.TripleService # 接口名,需要與客戶端一致 logger: # 日志配置,參考例子:https://github.com/apache/dubbo-go-samples/tree/master/logger zap-config: level: info # 日志級別 lumberjack-config: filename: logs.log # 文件輸出目錄 maxSize: 1 maxAge: 3 maxBackups: 5 localTime: true compress: false metrics: # 數(shù)據(jù)上報配置,參考例子:https://github.com/apache/dubbo-go-samples/tree/master/metrics enable: true # 數(shù)據(jù)上報開關(guān),默認(rèn)開啟 path: /custom-metrics-path # 拉模式數(shù)據(jù)上報本地監(jiān)聽path 默認(rèn)/metrics port: 9091 # 拉模式數(shù)據(jù)上報本地監(jiān)聽端口,默認(rèn)9090
// export DUBBO_GO_CONFIG_PATH= PATH_TO_SAMPLES/helloworld/go-client/conf/dubbogo.yml func main() { // set consumer struct if needed config.SetConsumerService(grpcGreeterImpl) // config loader start if err := config.Load(); err != nil { panic(err) } logger.Info("start to test dubbo") req := &api.HelloRequest{ Name: "laurence", } // do RPC invocation reply, err := grpcGreeterImpl.SayHello(context.Background(), req) if err != nil { logger.Error(err) } logger.Infof("client response result: %v\n", reply) }
用戶無需使用配置文件,可直接在代碼中以 API 的調(diào)用的形式寫入配置,如前面"快速開始"部分所提供的例子:
func main() { // init rootConfig with config api rc := config.NewRootConfigBuilder(). SetConsumer(config.NewConsumerConfigBuilder(). SetRegistryIDs("zookeeper"). AddReference("GreeterClientImpl", config.NewReferenceConfigBuilder(). SetInterface("org.apache.dubbo.UserProvider"). SetProtocol("tri"). Build()). Build()). AddRegistry("zookeeper", config.NewRegistryConfigWithProtocolDefaultPort("zookeeper")). Build() // validate consumer greeterProvider if err := rc.Init(); err != nil{ panic(err) } // run rpc invocation testSayHello() }
配置 API 看上去寫法較為復(fù)雜,但單個配置結(jié)構(gòu)的構(gòu)造過程都是一致的,參考 Java Builder 的設(shè)計,我們在配置 API 模塊選用 New().SetA().SetB().Build()的方式來構(gòu)造單個配置結(jié)構(gòu)。
將上述例子中的 rootConfig 構(gòu)造過程,可以拆解為:
referenceConfig := config.NewReferenceConfigBuilder(). SetInterface("org.apache.dubbo.UserProvider"). SetProtocol("tri"). Build() consumerConfig := config.NewConsumerConfigBuilder(). SetRegistryIDs("zookeeper"). AddReference("GreeterClientImpl", referenceConfig). Build()). registryConfig := config.NewRegistryConfigWithProtocolDefaultPort("zookeeper") rc := config.NewRootConfigBuilder(). SetConsumer(consumerConfig). AddRegistry("zookeeper", registryConfig). Build()
Dubbo-go 服務(wù)框架支持將配置文件 ‘dubbogo.yml’ 的內(nèi)容預(yù)先放入配置中心,再通過配置注冊中心的地址。在本地 dubbogo.yml 配置文件內(nèi)只需寫入配置中心的信息即可,目前支持作為配置中心的中間件有:apollo、nacos、zookeeper
可參考配置中心Samples,凡是正確配置了config-center 配置的服務(wù),都會優(yōu)先從配置中心加載整個配置文件。
dubbo: config-center: protocol: apollo address: localhost:8080 app-id: demo_server cluster: default namespace: demo-provider-config # 框架從 apollo 配置中最更新對應(yīng)位置加載配置文件,并根據(jù)該配置文件啟動
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: