apache / dubbo-go

Go Implementation For Apache Dubbo .
https://dubbo.apache.org/
Apache License 2.0
4.69k stars 917 forks source link

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

Open az2az opened 4 months ago

az2az commented 4 months ago

Environment

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 commented 4 months ago

使用的dubbo-go版本是3.2.0-rc1。3.1.1的版本不存在此问题

YarBor commented 4 months ago

我查看了现有的实现, 一种可能的解决办法是,更改yaml的声明方式:

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

感谢指导,确实是,调一下位置就可以了。

AlexStocks commented 4 months ago

这个 issue 不要 close,我已经打标 ”good first issue“

adi-kmt commented 4 months ago

@AlexStocks can I pick this up?

AlexStocks commented 4 months ago

can I pick this up?

Welcome.