1kasa commented on PR #2941:
URL: https://github.com/apache/dubbo-go/pull/2941#issuecomment-3061013099
test caseļ¼
Due to the length of this article, let's take filter as an example to show
that the serviecs configuration under the provider is effective.
client:
```
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")
}
```
client_my_filter:
```
package main
import (
"context"
"fmt"
)
import (
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
)
func init() {
extension.SetFilter("myClientFilter", NewMyClientFilter)
}
func NewMyClientFilter() filter.Filter {
return &MyClientFilter{}
}
type MyClientFilter struct {
}
func (f *MyClientFilter) Invoke(ctx context.Context, invoker
protocol.Invoker, invocation protocol.Invocation) protocol.Result {
fmt.Println("MyClientFilter Invoke is called, method Name = ",
invocation.MethodName())
invocation.SetAttachment("request-key1", "request-value1")
invocation.SetAttachment("request-key2", []string{"request-value2.1",
"request-value2.2"})
return invoker.Invoke(ctx, invocation)
}
func (f *MyClientFilter) OnResponse(ctx context.Context, result
protocol.Result, invoker protocol.Invoker, protocol protocol.Invocation)
protocol.Result {
fmt.Println("MyClientFilter OnResponse is called")
fmt.Println("result attachment = ", result.Attachments())
return result
}
```
dubbogo.yml:
```
# 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
filter: myClientFilter
```
server:
```
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(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
}
func main() {
greet.SetProviderService(&GreetTripleServer{})
if err := dubbo.Load(); err != nil {
panic(err)
}
select {}
}
```
server_my_filter:
```
package main
import (
"context"
"fmt"
)
import (
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
)
func init() {
extension.SetFilter("myServerFilter", NewMyServerFilter)
}
func NewMyServerFilter() filter.Filter {
return &MyServerFilter{}
}
type MyServerFilter struct {
}
func (f *MyServerFilter) Invoke(ctx context.Context, invoker
protocol.Invoker, invocation protocol.Invocation) protocol.Result {
fmt.Println("MyServerFilter Invoke is called, method Name = ",
invocation.MethodName())
fmt.Printf("request attachments = %s\n", invocation.Attachments())
return invoker.Invoke(ctx, invocation)
}
func (f *MyServerFilter) OnResponse(ctx context.Context, result
protocol.Result, invoker protocol.Invoker, protocol protocol.Invocation)
protocol.Result {
fmt.Println("MyServerFilter OnResponse is called")
myAttachmentMap := make(map[string]interface{})
myAttachmentMap["key1"] = "value1"
myAttachmentMap["key2"] = []string{"value1", "value2"}
result.SetAttachments(myAttachmentMap)
return result
}
```
dubbogo.yml:
```
# 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
filter: myServerFilter
```
result:
The following two pictures show that the server filter and interface can be
obtained normally.
<img width="1379" height="681" alt="image"
src="https://github.com/user-attachments/assets/273b4c3f-6ff1-4c5a-af67-103c46f5b67f"
/>
<img width="1351" height="497" alt="image"
src="https://github.com/user-attachments/assets/e1cb3bee-6144-497b-b3dc-3be71a5d03e5"
/>
client
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]