使用 dubbogo.yml 配置文件开发应用

使用 dubbogo.yml 配置文件开发应用

1.介绍

本文档演示如何在框架中使用 yaml 配置文件进行微服务开发,是相比于 API 的另一种微服务开发模式。你可以完全使用 yml 配置文件进行开发,也可以将部分全局配置放到配置文件,而只在 API 中完成服务定义。

这种模式下,一定要通过 DUBBO_GO_CONFIG_PATH 指定配置文件路径:

  1. export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml"

2. 使用说明

可在此查看 完整示例源码

2.1 运行示例

  1. .
  2. ├── go-client
  3. ├── cmd
  4. └── main.go
  5. └── conf
  6. └── dubbogo.yml
  7. ├── go-server
  8. ├── cmd
  9. └── main.go
  10. └── conf
  11. └── dubbogo.yml
  12. └─── proto
  13. ├── greet.pb.go
  14. ├── greet.proto
  15. └── greet.triple.go

通过 IDL./proto/greet.proto 定义服务 使用triple协议

build Proto

  1. cd path_to_dubbogo-sample/config_yaml/proto
  2. protoc --go_out=. --go-triple_out=. ./greet.proto

Server

  1. export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml"
  2. cd path_to_dubbogo-sample/config_yaml/go-server/cmd
  3. go run .

Client

  1. export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml"
  2. cd path_to_dubbogo-sample/config_yaml/go-client/cmd
  3. go run .

2.2 客户端使用说明

客户端定义的 yaml 文件:

  1. # dubbo client yaml configure file
  2. dubbo:
  3. registries:
  4. demoZK:
  5. protocol: zookeeper
  6. timeout: 3s
  7. address: 127.0.0.1:2181
  8. consumer:
  9. references:
  10. GreetServiceImpl:
  11. protocol: tri
  12. interface: com.apache.dubbo.sample.Greeter
  13. registry: demoZK
  14. retries: 3
  15. timeout: 3000

通过 dubbo.Load() 调用进行文件的读取以及加载

  1. //...
  2. func main() {
  3. //...
  4. if err := dubbo.Load(); err != nil {
  5. //...
  6. }
  7. //...
  8. }

2.3 服务端使用说明

服务端定义的 yaml 文件

  1. # dubbo server yaml configure file
  2. dubbo:
  3. registries:
  4. demoZK:
  5. protocol: zookeeper
  6. timeout: 10s
  7. address: 127.0.0.1:2181
  8. protocols:
  9. tripleProtocol:
  10. name: tri
  11. port: 20000
  12. provider:
  13. services:
  14. GreetTripleServer:
  15. interface: com.apache.dubbo.sample.Greeter

通过 dubbo.Load() 调用进行文件的读取以及加载

  1. //...
  2. func main() {
  3. //...
  4. if err := dubbo.Load(); err != nil {
  5. //...
  6. }
  7. //...
  8. }

3.示例详解

3.1服务端介绍

服务端proto文件

源文件路径:dubbo-go-sample/context/proto/greet.proto

  1. syntax = "proto3";
  2. package greet;
  3. option go_package = "github.com/apache/dubbo-go-samples/config_yaml/proto;greet";
  4. message GreetRequest {
  5. string name = 1;
  6. }
  7. message GreetResponse {
  8. string greeting = 1;
  9. }
  10. service GreetService {
  11. rpc Greet(GreetRequest) returns (GreetResponse) {}
  12. }

服务端handler文件

在服务端中,定义 GreetTripleServer:

  1. type GreetServiceHandler interface {
  2. Greet(context.Context, *GreetRequest) (*GreetResponse, error)
  3. }

实现 GreetServiceHandler 接口,通过 greet.SetProviderService(&GreetTripleServer{}) 进行注册 ,同样使用 dubbo.Load() 进行加载配置文件

源文件路径:dubbo-go-sample/config_yaml/go-server/cmd/main.go

  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "dubbo.apache.org/dubbo-go/v3"
  7. _ "dubbo.apache.org/dubbo-go/v3/imports"
  8. greet "github.com/apache/dubbo-go-samples/config_yaml/proto"
  9. )
  10. type GreetTripleServer struct {
  11. }
  12. func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
  13. name := req.Name
  14. if name != "ConfigTest" {
  15. errInfo := fmt.Sprintf("name is not right: %s", name)
  16. return nil, errors.New(errInfo)
  17. }
  18. resp := &greet.GreetResponse{Greeting: req.Name + "-Success"}
  19. return resp, nil
  20. }
  21. func main() {
  22. greet.SetProviderService(&GreetTripleServer{})
  23. if err := dubbo.Load(); err != nil {
  24. panic(err)
  25. }
  26. select {}
  27. }

3.2 客户端介绍

在客户端中,定义greet.GreetServiceImpl实例,greet.SetConsumerService(svc)进行注册: 通过 dubbo.Load() 进行配置文件的加载

源文件路径:dubbo-go-sample/config_yaml/go-client/cmd/main.go

  1. package main
  2. import (
  3. "context"
  4. "dubbo.apache.org/dubbo-go/v3"
  5. _ "dubbo.apache.org/dubbo-go/v3/imports"
  6. greet "github.com/apache/dubbo-go-samples/config_yaml/proto"
  7. "github.com/dubbogo/gost/log/logger"
  8. )
  9. var svc = new(greet.GreetServiceImpl)
  10. func main() {
  11. greet.SetConsumerService(svc)
  12. if err := dubbo.Load(); err != nil {
  13. panic(err)
  14. }
  15. req, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "ConfigTest"})
  16. if err != nil || req.Greeting != "ConfigTest-Success" {
  17. panic(err)
  18. }
  19. logger.Info("ConfigTest successfully")
  20. }

3.3 案例效果

先启动服务端,再启动客户端,可以观察到客户端打印了ConfigTest successfully配置加载以及调用成功

  1. 2024-03-11 15:47:29 INFO cmd/main.go:39 ConfigTest successfully

4 更多配置

指定 Filter

如果要指定多个 filter 时,可用 ‘,’ 分隔

  • Consumer 端

    1. dubbo:
    2. consumer:
    3. filter: echo,token,tps,myCustomFilter # 可指定自定义filter
  • Provider 端

    1. dubbo:
    2. provider:
    3. services:
    4. GreeterProvider:
    5. filter: myCustomFilter,echo,tps

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)