1.ラッパーの中間キーを作成します。
package Wrappers
import (
"github.com/micro/go-micro/client"
"context"
"fmt"
)
//Wrapper
type logWrapper struct{
client.Client
}
//Wrapper方法
func (this *logWrapper)Call(ctx context.Context, req client.Request, rsp interface{
}, opts ...client.CallOption) error{
fmt.Print("用户调用了接口")
return this.Client.Call(ctx, req, rsp)
}
//实例化Wrapper
func NewLogWrapper(c client.Client) client.Client {
return &logWrapper{
c}
}
2.ラッパーを使用します。
package main
import (
"github.com/micro/go-plugins/registry/consul"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/web"
"github.com/micro/go-micro"
"go-micro-grpc/Services"
"log"
"go-micro-grpc/Weblib"
"go-micro-grpc/Wrappers"
)
func main(){
//consul注册中心
consulReg := consul.NewRegistry(
registry.Addrs("192.168.56.10:8500"),
)
myService := micro.NewService(
micro.Name("prodservice.client"),
//使用Wrapper
micro.WrapClient(Wrappers.NewLogWrapper),
)
prodService := Services.NewProdService("prodservice", myService.Client())
//创建web服务器
httpServer := web.NewService(
web.Name("httpprodservice"), // 服务名
web.Address(":8001"), //端口号
web.Handler(Weblib.NewGinRouter(prodService)), // 路由
web.Registry(consulReg), // 注册服务
)
//初始化服务器
httpServer.Init()
//运行
err := httpServer.Run()
if err != nil{
log.Panic(err)
}
}
3.ラッパーをテストします。
Webサーバーを要求するとトリガーされ、結果は次のようになります。