tree: 992f90c66d3e2dccc6cd81eb86e77905ff48b271 [path history] [tgz]
  1. go-client/
  2. go-server/
  3. proto/
  4. README.md
  5. README_zh.md
config_yaml/README.md

Dubbo-go Config_Yaml

1.Introduction

This example demonstrates how to configure using yaml configuration files in Dubbo-go framework

2.Run

.
├── go-client
│   ├── cmd
│   │   └── main.go
│   └── conf
│       └── dubbogo.yml
├── go-server
│   ├── cmd
│   │   └── main.go
│   └── conf
│       └── dubbogo.yml
└─── proto
    ├── greet.pb.go
    ├── greet.proto
    └── greet.triple.go

The service is defined using IDL (./proto/greet.proto) and utilizes the Triple protocol.

build Proto

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

Server

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

Client

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

2.1 Client usage instructions

Client-defined dubbogo.yaml

# dubbo client yaml configure file
dubbo:
  registries:
    demoZK:
      protocol: zookeeper
      timeout: 3s
      address: 127.0.0.1:2181
  consumer:
    references:
      GreetServiceImpl:
        protocol: tri
        interface: com.apache.dubbo.sample.Greeter
        registry: demoZK
        retries: 3
        timeout: 3000

Read and load files through dubbo.Load().

//...
func main() {
	//...
	if err := dubbo.Load(); err != nil {
		//...
	}
	//...
}

2.2 Server usage instructions

Server-defined dubbogo.yaml

# dubbo server yaml configure file
dubbo:
  registries:
    demoZK:
      protocol: zookeeper
      timeout: 10s
      address: 127.0.0.1:2181
  protocols:
    tripleProtocol:
      name: tri
      port: 20000
  provider:
    services:
      GreetTripleServer:
        interface: com.apache.dubbo.sample.Greeter

Read and load files through dubbo.Load().

//...
func main() {
	//...
	if err := dubbo.Load(); err != nil {
		//...
	}
	//...
}

3.Example

3.1 Server

IDL

Source file path :dubbo-go-sample/context/proto/greet.proto

syntax = "proto3";

package greet;

option go_package = "github.com/apache/dubbo-go-samples/config_yaml/proto;greet";

message GreetRequest {
  string name = 1;
}

message GreetResponse {
  string greeting = 1;
}

service GreetService {
  rpc Greet(GreetRequest) returns (GreetResponse) {}
}

Server Handler

On the server side, define GreetTripleServer struct:

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

Implement the GreetServiceHandler interface and register it through greet.SetProviderService(srv common.RPCService)

Load the configuration file through dubbo.Load()

Source file path :dubbo-go-sample/config_yaml/go-server/cmd/main.go


package main import ( "context" "errors" "fmt" "dubbo.apache.org/dubbo-go/v3" _ "dubbo.apache.org/dubbo-go/v3/imports" greet "github.com/apache/dubbo-go-samples/config_yaml/proto" ) type GreetTripleServer struct { } func (srv *GreetTripleServer) Greet(_ context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) { name := req.Name if name != "ConfigTest" { errInfo := fmt.Sprintf("name is not right: %s", name) return nil, errors.New(errInfo) } resp := &greet.GreetResponse{Greeting: req.Name + "-Success"} return resp, nil } func main() { greet.SetProviderService(&GreetTripleServer{}) if err := dubbo.Load(); err != nil { panic(err) } select {} }

3.2 Client

In the client, define greet.GreetServiceImpl instance and register it through greet.SetConsumerService(srv common.RPCService):

Load the configuration file through dubbo.Load()

Source file path :dubbo-go-sample/config_yaml/go-client/cmd/main.go

package main

import (
	"context"
	"dubbo.apache.org/dubbo-go/v3"
	_ "dubbo.apache.org/dubbo-go/v3/imports"
	greet "github.com/apache/dubbo-go-samples/config_yaml/proto"
	"github.com/dubbogo/gost/log/logger"
)

var svc = new(greet.GreetServiceImpl)

func main() {
	greet.SetConsumerService(svc)
	if err := dubbo.Load(); err != nil {
		panic(err)
	}
	req, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "ConfigTest"})
	if err != nil || req.Greeting != "ConfigTest-Success" {
		panic(err)
	}
	logger.Info("ConfigTest successfully")
}

3.3 Show

Start the server first, then the client. If the client prints ConfigTest Successful, it means the configuration is loaded and the call success.

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