go-micro v2版本 微服務框架 實戰二 三層架構開發模式


項目github地址:https://github.com/sunlongv520/go-micro-code/tree/master/gjj

 

如果代碼讀起來費勁 清先參考三層架構基礎篇

Go web框架構建三層架構

go-micro中集成三層架構開發模式

D:\gocode1.14.3\gocode\gjj\src\cmd\course_http_server.go

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/micro/go-micro/v2/web"
	"jtthink/framework/gin_"
	_ "jtthink/src/lib"
	"log"
)

func main()  {

	r:=gin.New()

	gin_.BootStrap(r)

	//web改成micro 就是grpc,並直接注冊到etcd里面
	service:=web.NewService(
		web.Name("api.jtthink.com.http.course"),
		web.Handler(r),
	 )

	service.Init()
	if err:= service.Run(); err != nil {
		log.Println(err)
	}

}

  

三層架構:

核心:

gin_.BootStrap(r)

D:\gocode1.14.3\gocode\gjj\framework\gin_\ServiceUtil.go
package gin_

import "github.com/gin-gonic/gin"

var HandlerMap map[string]map[string]gin.HandlerFunc

func init()  {
    HandlerMap=make( map[string]map[string]gin.HandlerFunc)
}
func SetHandler(methods string,path string,handler gin.HandlerFunc)  {
    if _,ok:=HandlerMap[path];ok{
        HandlerMap[path][methods]=handler
    }else{
        HandlerMap[path]=make(map[string]gin.HandlerFunc)

    }
    HandlerMap[path][methods]=handler
}
func BootStrap(router *gin.Engine)  {
    for path,hmap:=range  HandlerMap{
        for method,handler:=range hmap{
            router.Handle(method,path,handler)
        }
    }
}

 

D:\gocode1.14.3\gocode\gjj\src\lib\CourseLib.go

package lib

import (
    "github.com/gin-gonic/gin"
    "jtthink/framework/gin_"
    "jtthink/src/Course"
    "jtthink/src/service"

    "log"
)

func init()  {
    courseService:=service.NewCourseServiceImpl()
    gin_.NewBuilder().WithService(courseService).
        WithMiddleware(Test_Middleware()).
        WithEndpoint(Courselist_Endpoint(courseService)).
        WithRequest(Courselist_Request()).
        WithResponse(Course_Response()).Build("/test","GET")
}

func Test_Middleware()  gin_.Middleware {
    return func(next gin_.Endpoint) gin_.Endpoint {
        return func(context *gin.Context, request interface{}) (response interface{}, err error) {
            log.Println("abc")
            return next(context,request)
        }
    }
}

//獲取列表相關
func Courselist_Endpoint(c *service.CourseServiceImpl)   gin_.Endpoint {
    return func(context *gin.Context, request interface{}) (response interface{}, err error) {
        rsp:=&Course.ListResponse{}
        err=c.ListForTop(context,request.(*Course.ListRequest),rsp)
        return rsp,err
    }
}
//這個函數的作用是怎么處理請求
func Courselist_Request() gin_.EncodeRequestFunc{
    return func(context *gin.Context) (i interface{}, e error) {
        bReq:=&Course.ListRequest{}
        err:=context.BindQuery(bReq) //使用的是query 參數
        if err!=nil{
            return nil,err
        }
        return bReq,nil
    }
}
//這個函數作用是:怎么處理響應結果
func Course_Response()  gin_.DecodeResponseFunc  {
    return func(context *gin.Context, res interface{}) error {
        context.JSON(200,res)
        return nil
    }
}

 

framework

 

D:\gocode1.14.3\gocode\gjj\framework\gin_\ServiceBuilder.go

package gin_

type ServiceBuilder struct {
    service interface{}
    endPoint Endpoint
    requestFunc EncodeRequestFunc
    responseFunc DecodeResponseFunc
    methods string
    middlewares []Middleware

}

func NewBuilder() *ServiceBuilder  {
     return &ServiceBuilder{middlewares:make([]Middleware,0)}

}

func(this *ServiceBuilder) WithService(obj interface{}) *ServiceBuilder {
    this.service=obj
    return this
}
func(this *ServiceBuilder) WithMiddleware(obj Middleware) *ServiceBuilder {
    this.middlewares=append(this.middlewares,obj)
    return this
}
func(this *ServiceBuilder) WithEndpoint(obj Endpoint) *ServiceBuilder {
    this.endPoint=obj
    return this
}
func(this *ServiceBuilder) WithRequest(obj EncodeRequestFunc) *ServiceBuilder {
    this.requestFunc=obj
    return this
}
func(this *ServiceBuilder) WithResponse(obj DecodeResponseFunc) *ServiceBuilder {
    this.responseFunc=obj
    return this
}
func(this *ServiceBuilder) WithMethods(methods string) *ServiceBuilder {
    this.methods=methods
    return this
}
func(this *ServiceBuilder) Build(path string,methods string)  *ServiceBuilder{

    finalEndpoint:=this.endPoint
    for _,m:=range this.middlewares{ //遍歷中間件
        finalEndpoint=m(finalEndpoint)
    }
     handler:=RegisterHandler(finalEndpoint,this.requestFunc,this.responseFunc)

    SetHandler(methods,path,handler)
    return this
}

 

D:\gocode1.14.3\gocode\gjj\framework\gin_\ServiceHelper.go

package gin_

import (
    "fmt"
    "github.com/gin-gonic/gin"
)

type Middleware func(next Endpoint) Endpoint
//業務最終函數原型
type Endpoint func(ctx *gin.Context,request interface{}) (response interface{}, err error)
//怎么取參數
type EncodeRequestFunc func(*gin.Context) (interface{}, error)
//怎么處理業務結果
type DecodeResponseFunc func(*gin.Context, interface{}) error

func RegisterHandler(endpoint Endpoint,encodeFunc EncodeRequestFunc, decodeFunc DecodeResponseFunc) func(context *gin.Context){
    return func(ctx *gin.Context) {
        defer func() {
            if r:=recover();r!=nil{
                ctx.JSON(500,gin.H{"error":fmt.Sprintf("fatal error:%s",r)})
                return
            }
        }()
        //參數:=怎么取參數(context)
        //業務結果,error:=業務最終函數(context,參數)
        //
        //
        //怎么處理業務結果(業務結果)
        req,err:=encodeFunc(ctx) //獲取參數
        if err!=nil{
            ctx.JSON(400,gin.H{"error":"param error:"+err.Error()})
            return
        }
        rsp,err:=endpoint(ctx,req) //執行業務過程
        if err!=nil{
            ctx.JSON(400,gin.H{"error":"response error:"+err.Error()})
            return
        }
        err=decodeFunc(ctx,rsp) //處理 業務執行 結果
        if err!=nil{
            ctx.JSON(500,gin.H{"error":"server error:"+err.Error()})
            return
        }

    }
}

D:\gocode1.14.3\gocode\gjj\framework\gin_\ServiceUtil.go

package gin_

import "github.com/gin-gonic/gin"

var HandlerMap map[string]map[string]gin.HandlerFunc

func init()  {
    HandlerMap=make( map[string]map[string]gin.HandlerFunc)
}
func SetHandler(methods string,path string,handler gin.HandlerFunc)  {
    if _,ok:=HandlerMap[path];ok{
        HandlerMap[path][methods]=handler
    }else{
        HandlerMap[path]=make(map[string]gin.HandlerFunc)

    }
    HandlerMap[path][methods]=handler
}
func BootStrap(router *gin.Engine)  {
    for path,hmap:=range  HandlerMap{
        for method,handler:=range hmap{
            router.Handle(method,path,handler)
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM