“This is the 12th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
B station video explanation address
www.bilibili.com/video/BV1h4…
Making the source code
Github.com/CocaineCong…
3. Access gateway
3.1 proto file
- api-gateway/services/proto
Copy the two proto files from our user module and do the same to generate a PB file.
Note that the proto file is regenerated in api-gateway
3.2 Access Routes
- api-gateway/weblib/router.go
To access gin routes in this file, the middleware part is omitted.
func NewRouter(service ...interface{}) *gin.Engine {
ginRouter := gin.Default()
// Using middleware, receive service invocation instances
ginRouter.Use(middlewares.Cors(), middlewares.InitMiddleware(service), middlewares.ErrorMiddleware())
// Use session middleware
store := cookie.NewStore([]byte("something-very-secret"))
ginRouter.Use(sessions.Sessions("mysession", store))
// Test the connection
v1.GET("pong".func(c *gin.Context) {
c.JSON(200."success")})// User services
v1.POST("/user/register", handlers.UserRegister)
v1.POST("/user/login", handlers.UserLogin)
}
return ginRouter
}
Copy the code
3.3 Interface Writing
3.3.1 User Registration
- Define request parameters
var userReq services.UserRequest
Copy the code
- Binding parameters
PanicIfUserError(ginCtx.Bind(&userReq))
Copy the code
- Obtaining service Instances
userService := ginCtx.Keys["userService"].(services.UserService)
Copy the code
- Retrieve service objects
userRes, err := userService.UserRegister(context.Background(), &userReq)
Copy the code
- Return the data
ginCtx.JSON(http.StatusOK, gin.H{"data": userRes})
Copy the code
- The complete code
func UserRegister(ginCtx *gin.Context) {
var userReq services.UserRequest
PanicIfUserError(ginCtx.Bind(&userReq))
// Fetch the service instance from gin. Keys
userService := ginCtx.Keys["userService"].(services.UserService)
userRes, err := userService.UserRegister(context.Background(), &userReq)
PanicIfUserError(err)
ginCtx.JSON(http.StatusOK, gin.H{"data": userRes})
}
Copy the code
3.3.2 User Login
func UserLogin(ginCtx *gin.Context) {
var userReq services.UserRequest
// Define the request parameters
PanicIfUserError(ginCtx.Bind(&userReq))
// Bind the service
userService := ginCtx.Keys["userService"].(services.UserService)
// Fetch the service instance from gin. Keys
userRes, err := userService.UserLogin(context.Background(), &userReq)
// Call a function on the server
PanicIfUserError(err)
token, err := util.GenerateToken(uint(userRes.UserDetail.ID))
/ / token is generated
iferr ! =nil {
userRes.Code = e.ERROR_AUTH_TOKEN
}
ginCtx.JSON(200, gin.H{"code": userRes.Code, "msg": e.GetMsg(userRes.Code), "data": gin.H{"admin": userRes.UserDetail, "token": token}})
// The response is returned
}
Copy the code
3.4 access etcd
- Define user module microservice instances
etcdReq := etcd.NewRegistry(
registry.Addrs("127.0.0.1:2379"),/ / user
userMicroService := micro.NewService(
micro.Name("userService.client"),
micro.WrapClient(wrappers.NewUserWrapper),
)
// User service invocation instance
userService := services.NewUserService("rpcUserService", userMicroService.Client())
Copy the code
- Define an instance of the HTTP microservice module, expose the HTTP interface using GIN and register it with ETCD
server := web.NewService(
web.Name("httpService"),
web.Address(": 4000"),
// Handle service invocation instances using GIN
web.Handler(weblib.NewRouter(userService)), // Put the userService microservice instance into it
web.Registry(etcdReq),
web.RegisterTTL(time.Second*30),
web.RegisterInterval(time.Second*15),
web.Metadata(map[string]string{"protocol": "http"}),)Copy the code
Check whether the HTTP service is registered with the ETCD
3.5 test
Run it in the user/main.go file
go run main.go --registry=etcd --registry_address=127.0. 01.:2379
Copy the code
Run the command in api-gateway/main.go
go run main.go --registry=etcd --registry_address=127.0. 01.:2379
Copy the code
- User registration
- The user login