dubbo-go icon indicating copy to clipboard operation
dubbo-go copied to clipboard

使用配置来提供服务,filter不生效

Open az2az opened this issue 9 months ago • 6 comments

Environment

  • Server:
  • Client:
  • Protocol:
  • Registry:

Issue description

使用dubbo-go-samples 的config_yaml测试用例,发现使用配置文件加载filter时,不生效,而使用代码的方式加载能生效(以下代码,使用Load()可以正常使用filter,而LoadByConfig()则filter不生效)。

package main

import (
	"context"
	"errors"
	"fmt"
	"os"
	"path"
	//
	"dubbo.apache.org/dubbo-go/v3"
	"dubbo.apache.org/dubbo-go/v3/common/extension"
	"dubbo.apache.org/dubbo-go/v3/filter"
	_ "dubbo.apache.org/dubbo-go/v3/imports"
	"dubbo.apache.org/dubbo-go/v3/protocol"
	"dubbo.apache.org/dubbo-go/v3/server"
	"github.com/apache/dubbo-go-samples/go-server/proto"
)

type GreetTripleServer struct {
}

func (srv *GreetTripleServer) Greet(ctx 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
}

const VALIDATE_FILTER_NAME = "ValidateFilter"

type validateFilter struct{}

func (p *validateFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
	result := &protocol.RPCResult{}
	result.SetError(errors.New("已被过滤器拦截"))
	return result
}

func (p *validateFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
	return result
}

func main() {
	LoadByConfig()
}

func Load() {
	srv, err := server.NewServer(
		server.WithServerProtocol(
			protocol.WithPort(20000),
			protocol.WithTriple(),
		),
	)
	if err != nil {
		panic(err)
	}
	//
	extension.SetFilter(VALIDATE_FILTER_NAME, func() filter.Filter {
		return &validateFilter{}
	})
	if err = greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{},
		server.WithFilter(VALIDATE_FILTER_NAME),
	); err != nil {
		panic(err)
	}
	//
	if err := srv.Serve(); err != nil {
		panic(err)
	}
}

func LoadByConfig() {
	extension.SetFilter(VALIDATE_FILTER_NAME, func() filter.Filter {
		return &validateFilter{}
	})
	//
	greet.SetProviderService(&GreetTripleServer{})
	//
	s, getwdError := os.Getwd()
	if getwdError != nil {
		panic(getwdError)
	}
	configPath := path.Join(s, "/go-server/conf/dubbogo.yaml")
	//
	if err := dubbo.Load(dubbo.WithPath(configPath)); err != nil {
		panic(err)
	}
	//
	select {}
}

###############配置如下所示######################

dubbo:
  registries:
    demoZK:
      protocol: nacos
      timeout: 10s
      address: 127.0.0.1:8848
  protocols:
    tripleProtocol:
      name: tri
      port: 20000
  provider:
    services:
      GreetTripleServer:
        filter: ValidateFilter
        interface: com.apache.dubbo.sample.Greeter

Logs

Click me to check logs
Copy logs to here.

az2az avatar May 11 '24 13:05 az2az