开发者社区 > 云原生 > 中间件 > 正文

在试用dubbo-go v3.2.0-rc1,发现使用配置文件加载filter时,不生效,啥原因?

在试用dubbo-go v3.2.0-rc1,使用dubbo-go-samples 的config_yaml测试用例,发现使用配置文件加载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 {}
}
上述代码Load()可以正常使用filter,而LoadByConfig()不行,配置如下所示:

dubbo server yaml configure file

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

展开
收起
嘟嘟嘟嘟嘟嘟 2024-05-15 13:57:40 79 0
0 条回答
写回答
取消 提交回答

为企业提供高效、稳定、易扩展的中间件产品。

相关电子书

更多
Dubbo开源现状与2.7规划 立即下载
Dubbo分布式服务治理实战 立即下载
《Dubbo 3.0 前瞻》 立即下载