当前位置:网站首页>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"
边栏推荐
- WordPress preview email for wocomerce 1.6.8 cross site scripting
- Tencent cloud server ubuntu18 installs MySQL and logs in remotely
- Don't let your server run naked -- security configuration after purchasing a new server (Basics)
- 2021-12-18: find all letter ectopic words in the string. Given two characters
- Talking about using email to attack social engineering
- there can be only one auto column and it must be defined as a key
- Go build command (go language compilation command) complete introduction
- Pourquoi une seule valeur apparaît - elle sur votre carte de données?
- JWT implementation
- Using the provider to transform the shit like code, the amount of code is reduced by 2/3!
猜你喜欢

Why is only one value displayed on your data graph?

openGauss Developer Day 2022正式开启,与开发者共建开源数据库根社区

為什麼你的數據圖譜分析圖上只顯示一個值?

Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse

Pourquoi une seule valeur apparaît - elle sur votre carte de données?

ICML2022 | 基于对比学习的离线元强化学习的鲁棒任务表示

脚本之美│VBS 入门交互实战

Hackinglab penetration test question 8:key can't find it again

应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构

为什么你的数据图谱分析图上只显示一个值?
随机推荐
2008R2 CFS with NFS protocol
How to set the life cycle of API gateway
Practice of business level disaster recovery switching drill
How does the fortress machine view the account assigned by the server? What are the specific steps?
Digital transformation solution for raw material industry chain supply chain platform
Hackinglab penetration test question 8:key can't find it again
Learn about reentrantlock
there can be only one auto column and it must be defined as a key
Digital transformation solution for supply chain platform of mechanical equipment industry
Pourquoi une seule valeur apparaît - elle sur votre carte de données?
Recommend several idea plug-ins
Redis source code analysis -- QuickList of redis list implementation principle
Knowda: all in one knowledge mixture model for data augmentation in feed shot NLP
Object declaration
SAP mm ml81n creates a service receipt for a purchase order and reports an error - no matching Po items selected-
Achieve scoring (Star scoring) effect through native JS
Service API version design and Practice
EDI mandatory manual
Method of thread synchronization in kotlin
ICML2022 | 基于对比学习的离线元强化学习的鲁棒任务表示