当前位置:网站首页>Micro API gateway Middleware

Micro API gateway Middleware

2022-06-23 22:22:00 Is paidaxing there

Look at us from above Api The gateway still has a lot to do :

image.png

Mciro Examples of plug-in forms provided

1: Create a new Gateway project structure

image.png

Add a demonstration of the registration sequence of multiple plug-ins for a user

image.png

2: Define a plug-in auth.go

package auth
import (
    "github.com/micro/cli/v2"
    "github.com/micro/micro/v2/plugin"
    "log"
    "net/http"
)
func NewPlugin() plugin.Plugin {
    return plugin.NewPlugin(
        //  The plugin name 
        plugin.WithName("example"),
        // Some parameter descriptions of the query command 
        plugin.WithFlag(&cli.StringFlag{
            Name:   "example_flag",
            Usage:  "This is an example plugin flag",
            EnvVars: []string{"EXAMPLE_FLAG"},
            Value: "avalue",
        }),
        //  Configure plug-in initialization ,cli.Context Project startup parameters are included in 
        plugin.WithInit(func(ctx *cli.Context) error {
            println(" I am a custom authentication middleware processor -----------------------")
            log.Println("Got value for example_flag", ctx.String("example_flag"))
            return nil
        }),
        //  Configure handler , Pay attention to and wrapper Different , His parameters are http Bag ResponseWriter and Request
        plugin.WithHandler(cAuthWrapper()),
    )
}
// Query what needs to be done 
func cAuthWrapper() plugin.Handler {
    return func(h http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            println(" Come in, come in, come to the authentication middleware !")
            // This place is very important ---- The following step must be performed , Ensure the execution of other plug-ins and business code , Otherwise, no response body returns 
            h.ServeHTTP(w, r)
        })
    }
}

2: Define a plug-in auth2.go

package auth
import (
    "github.com/micro/cli/v2"
    "github.com/micro/micro/v2/plugin"
    "log"
    "net/http"
)
func NewPlugin2() plugin.Plugin {
    return plugin.NewPlugin(
        //  The plugin name 
        plugin.WithName("example"),
        // Some parameter descriptions of the query command 
        plugin.WithFlag(&cli.StringFlag{
            Name:   "example_flag",
            Usage:  "This is an example plugin flag",
            EnvVars: []string{"EXAMPLE_FLAG"},
            Value: "avalue",
        }),
        //  Configure plug-in initialization ,cli.Context Project startup parameters are included in 
        plugin.WithInit(func(ctx *cli.Context) error {
            println(" I am a custom authentication middleware processor 222222-----------------------")
            log.Println("Got value for example_flag", ctx.String("example_flag"))
            return nil
        }),
        //  Configure handler , Pay attention to and wrapper Different , His parameters are http Bag ResponseWriter and Request
        plugin.WithHandler(cAuthWrapper2()),
    )
}
// Query what needs to be done 
func cAuthWrapper2() plugin.Handler {
    return func(h http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            println(" Come in, come in, come to the authentication middleware 222222222!")
            // This place is very important ---- The following step must be performed , Ensure the execution of other plug-ins and business code , Otherwise, no response body returns 
            h.ServeHTTP(w, r)
        })
    }
}

3: Register plug-ins , In our main.go Register our plug-ins inside

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()  {
    // The first way 
    err := api.Register(auth.NewPlugin())
    if err != nil {
        //log.Fatal("auth register")
    }
    //  The second way :
    err =plugin.Register(auth.NewPlugin2())
    if err != nil {
        //log.Fatal("auth register")
    }
    cmd.Init()
}

4: Start our gateway , The main relevant startup command parameters are ( The following parameters are indispensable ):

PS: The following parameters are indispensable, or you will be prompted with relevant wrong commands

D:\code\go\micro-greeter\api-gateway>go run main.go
 I am a custom authentication middleware processor 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

Start the gateway command correctly :

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

Check our startup log :

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
 I am a custom authentication middleware processor 222222-----------------------
2021-01-28 17:10:19.689117 I | Got value for example_flag avalue
 I am a custom authentication middleware processor -----------------------
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  [email protected]/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

Last registered , Install first !

At this point we visit our API Interface :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  [email protected]/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
 Come in, come in, come to the authentication middleware !
 Come in, come in, come to the authentication middleware 222222222!
::1 - - [28/Jan/2021:17:10:47 +0800] "GET /greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
原网站

版权声明
本文为[Is paidaxing there]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112151541452199.html