Use the dubbogo.yml Configuration File to Develop Applications

Use the dubbogo.yml configuration file to develop applications

1. Introduction

This document demonstrates how to use a yaml configuration file in the framework for microservice development, which is an alternative development model compared to API. You can fully develop using a yml configuration file or put some global configurations in the configuration file, while defining services only in the API.

In this mode, you must specify the configuration file path through DUBBO_GO_CONFIG_PATH:

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

2. Usage Instructions

You can view the complete example source code here.

2.1 Run Example

  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

Define services using IDL ./proto/greet.proto with the triple protocol.

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 Client Instructions

The yaml file defined by the client:

  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

Call dubbo.Load() to read and load the file.

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

2.3 Server Instructions

The yaml file defined by the server:

  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

Call dubbo.Load() to read and load the file.

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

3. Example Details

3.1 Server Introduction

Server Proto File

Source file path: 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. }

Server Handler File

In the server, define GreetTripleServer:

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

Implement the GreetServiceHandler interface and register it via greet.SetProviderService(&GreetTripleServer{}), also load the configuration file with dubbo.Load().

Source file path: 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 Client Introduction

In the client, definegreet.GreetServiceImpl instance and register with greet.SetConsumerService(svc); load the configuration file with dubbo.Load().

Source file path: 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 Case Effect

Start the server first, then start the client, and you can observe the client printing ConfigTest successfully configuration loading and calling success.

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

4 More Configurations

Specify Filters

If you want to specify multiple filters, you can separate them with ‘,’.

  • Consumer Side

    1. dubbo:
    2. consumer:
    3. filter: echo,token,tps,myCustomFilter # Custom filters can be specified
  • Provider Side

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

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)