Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
az2az opened this issue May 11, 2024 · 6 comments
Open

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

az2az opened this issue May 11, 2024 · 6 comments
Labels
good first issue Good for newcomers

Comments

@az2az
Copy link

az2az commented May 11, 2024

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
Copy link
Author

az2az commented May 11, 2024

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

@YarBor
Copy link
Contributor

YarBor commented May 15, 2024

我查看了现有的实现,
一种可能的解决办法是,更改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
Copy link
Author

az2az commented May 15, 2024

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

@AlexStocks AlexStocks added the good first issue Good for newcomers label May 15, 2024
@AlexStocks
Copy link
Contributor

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

@adi-kmt
Copy link

adi-kmt commented May 16, 2024

@AlexStocks can I pick this up?

@AlexStocks
Copy link
Contributor

can I pick this up?

Welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants