image.png
image.png
新增一个用户多个插件的注册顺序的演示
image.png
package auth
import (
"github.com/micro/cli/v2"
"github.com/micro/micro/v2/plugin"
"log"
"net/http"
)
func NewPlugin() plugin.Plugin {
return plugin.NewPlugin(
// 插件名
plugin.WithName("example"),
//查询命令喊的一些参数说明
plugin.WithFlag(&cli.StringFlag{
Name: "example_flag",
Usage: "This is an example plugin flag",
EnvVars: []string{"EXAMPLE_FLAG"},
Value: "avalue",
}),
// 配置插件初始化操作,cli.Context中包含了项目启动参数
plugin.WithInit(func(ctx *cli.Context) error {
println("我是自定义的认证中间件处理器-----------------------")
log.Println("Got value for example_flag", ctx.String("example_flag"))
return nil
}),
// 配置处理函数,注意与wrapper不同,他的参数是http包的ResponseWriter和Request
plugin.WithHandler(cAuthWrapper()),
)
}
//查询需要处理的事情
func cAuthWrapper() plugin.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
println("进去进来到了认证中间件来了!")
//这个地方很关键----必须执行下面这一步,保证其他插件和业务代码的执行,否则没有响应体返回
h.ServeHTTP(w, r)
})
}
}
package auth
import (
"github.com/micro/cli/v2"
"github.com/micro/micro/v2/plugin"
"log"
"net/http"
)
func NewPlugin2() plugin.Plugin {
return plugin.NewPlugin(
// 插件名
plugin.WithName("example"),
//查询命令喊的一些参数说明
plugin.WithFlag(&cli.StringFlag{
Name: "example_flag",
Usage: "This is an example plugin flag",
EnvVars: []string{"EXAMPLE_FLAG"},
Value: "avalue",
}),
// 配置插件初始化操作,cli.Context中包含了项目启动参数
plugin.WithInit(func(ctx *cli.Context) error {
println("我是自定义的认证中间件处理器222222-----------------------")
log.Println("Got value for example_flag", ctx.String("example_flag"))
return nil
}),
// 配置处理函数,注意与wrapper不同,他的参数是http包的ResponseWriter和Request
plugin.WithHandler(cAuthWrapper2()),
)
}
//查询需要处理的事情
func cAuthWrapper2() plugin.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
println("进去进来到了认证中间件来了222222222!")
//这个地方很关键----必须执行下面这一步,保证其他插件和业务代码的执行,否则没有响应体返回
h.ServeHTTP(w, r)
})
}
}
package main
import (
"github.com/micro/micro/v2/client/api"
"github.com/micro/micro/v2/cmd"
"github.com/micro/micro/v2/plugin"
"micro/plugins/auth"
)
func main() {
//第一种方式
err := api.Register(auth.NewPlugin())
if err != nil {
//log.Fatal("auth register")
}
// 第二种方式:
err =plugin.Register(auth.NewPlugin2())
if err != nil {
//log.Fatal("auth register")
}
cmd.Init()
}
PS: 后面的参数不可缺不然会提示相关的错误命令
D:\code\go\micro-greeter\api-gateway>go run main.go
我是自定义的认证中间件处理器222222-----------------------
2021-01-28 17:08:58.552198 I | Got value for example_flag avalue
No command provided to micro. Please refer to 'micro --help'
exit status 1
正确的启动网关命令:
go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --address=0.0.0.0:9000 --namespace=go.micro --type=service
查看我们的启动日志:
D:\code\go\micro-greeter\api-gateway>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --address=0.0.0.0:9000 --namespace=go.micro --type=service
我是自定义的认证中间件处理器222222-----------------------
2021-01-28 17:10:19.689117 I | Got value for example_flag avalue
我是自定义的认证中间件处理器-----------------------
2021-01-28 17:10:19.731005 I | Got value for example_flag avalue
2021-01-28 17:10:19 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-28 17:10:19 file=http/http.go:90 level=info service=api HTTP API Listening on [::]:9000
2021-01-28 17:10:19 file=v2@v2.9.1/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-28 17:10:19 file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:62154
2021-01-28 17:10:19 file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-b10fb08b-5834-4316-9690-2109eaca1899
最后注册的,先执行的安装!
此时我们访问我们的API接口:http://localhost:9000/greeter/call
2021-01-28 17:10:19.731005 I | Got value for example_flag avalue
2021-01-28 17:10:19 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-28 17:10:19 file=http/http.go:90 level=info service=api HTTP API Listening on [::]:9000
2021-01-28 17:10:19 file=v2@v2.9.1/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-28 17:10:19 file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:62154
2021-01-28 17:10:19 file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-b10fb08b-5834-4316-9690-2109eaca1899
进去进来到了认证中间件来了!
进去进来到了认证中间件来了222222222!
::1 - - [28/Jan/2021:17:10:47 +0800] "GET /greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。