[goa]golang微服務框架學習--安裝使用


 
當項目逐漸變大之后,服務增多,開發人員增加,單純的使用go來寫服務會遇到風格不統一,開發效率上的問題。
之前研究go的微服務架構go-kit最讓人頭疼的就是定義服務之后,還要寫很多重復的框架代碼,一直再想如何使用IDL描述服務,然后自動生成框架代碼。
直到遇到老這貨 goa,另外一個go的微服務框架。具體介紹看 這篇,還有 官網
 
這貨實現了框架的代碼自動生成(自動生成的代碼可以熱更新,因為生成代碼和自己寫的代碼是分開的),而且理念也比較時髦,基於API設計,利用插件來擴展業務邏輯。
 
於是想想要趕緊學習玩一下。
 
假定你已經裝了golang的環境。
所以,開始下載。
go get github.com/goadesign/goa
go get github.com/goadesign/goa/goagen

go install github.com/goadesign/goa
go install github.com/goadesign/goa/goagen

 安裝好之后,就可以用help命令來查看使用方式了。

qingping.zhang@qpzhangmac ~/dev goagen --help
The goagen tool generates artifacts from a goa service design package.

Each command supported by the tool produces a specific type of artifacts. For example
the "app" command generates the code that supports the service controllers.

The "bootstrap" command runs the "app", "main", "client" and "swagger" commands generating the
controllers supporting code and main skeleton code (if not already present) as well as a client
package and tool and the Swagger specification for the API.

Usage:
  goagen [command]

Available Commands:
  app         Generate application code
  main        Generate application scaffolding
  client      Generate client package and tool
  swagger     Generate Swagger
  js          Generate JavaScript client
  schema      Generate JSON Schema
  gen         Run third-party generator
  bootstrap   Equivalent to running the "app", "main", "client" and "swagger" commands.

Flags:
      --debug           enable debug mode, does not cleanup temporary files.
  -d, --design string   design package import path
  -o, --out string      output directory (default "/Users/qingpingzhang/dev")

Use "goagen [command] --help" for more information about a command.

 

然后需要yaml包的支持,所以需要提前安裝:

go get gopkg.in/yaml.v2

 

必須要在GOPATH的src的目錄下,新建一個目錄(例如:goa.demo),然后在它下面新建一個design的子目錄。

子design目錄下,新建文件design.go

 

//design.go

package design                                     // The convention consists of naming the design
                                                   // package "design"
import (
        . "github.com/goadesign/goa/design"        // Use . imports to enable the DSL
        . "github.com/goadesign/goa/design/apidsl"
)

var _ = API("cellar", func() {                     // API defines the microservice endpoint and
        Title("The virtual wine cellar")           // other global properties. There should be one
        Description("A simple goa service")        // and exactly one API definition appearing in
        Scheme("http")                             // the design.
        Host("localhost:8080")
})

var _ = Resource("bottle", func() {                // Resources group related API endpoints
        BasePath("/bottles")                       // together. They map to REST resources for REST
        DefaultMedia(BottleMedia)                  // services.

        Action("show", func() {                    // Actions define a single API endpoint together
                Description("Get bottle by id")    // with its path, parameters (both path
                Routing(GET("/:bottleID"))         // parameters and querystring values) and payload
                Params(func() {                    // (shape of the request body).
                        Param("bottleID", Integer, "Bottle ID")
                })
                Response(OK)                       // Responses define the shape and status code
                Response(NotFound)                 // of HTTP responses.
        })
})

// BottleMedia defines the media type used to render bottles.
var BottleMedia = MediaType("application/vnd.goa.example.bottle+json", func() {
        Description("A bottle of wine")
        Attributes(func() {                         // Attributes define the media type shape.
                Attribute("id", Integer, "Unique bottle ID")
                Attribute("href", String, "API href for making requests on the bottle")
                Attribute("name", String, "Name of wine")
                Required("id", "href", "name")
        })
        View("default", func() {                    // View defines a rendering of the media type.
                Attribute("id")                     // Media types may have multiple views and must
                Attribute("href")                   // have a "default" view.
                Attribute("name")
        })
})

 

然后使用命令,來自動生成框架代碼(這里需要注意,如果不加-o選項指定輸出目錄 , 這些代碼是會生成到當前目錄下的哦):

 qingping.zhang@qpzhangmac ~/gocode/src/goa.demo/  goagen bootstrap -d goa.demo/design
app
app/contexts.go
app/controllers.go
app/hrefs.go
app/media_types.go
app/user_types.go
app/test
app/test/bottle.go
main.go
bottle.go
client
client/cellar-cli
client/cellar-cli/main.go
client/cellar-cli/commands.go
client/client.go
client/bottle.go
client/datatypes.go
swagger
swagger/swagger.json
swagger/swagger.yaml

尼瑪,咔咔咔。。。生成好多。

 

框架會默認生成結構的返回值,所以,這里我們暫時不做任何修改,因為還沒有介紹生成的代碼目錄。

所以我們直接編譯,運行看看。

 qingping.zhang@qpzhangmac ~/gocode/src/goa.demo go build
 qingping.zhang@qpzhangmac ~/gocode/src/goa.demo ./goa.demo 2016/05/26 15:45:09 [INFO] mount ctrl=Bottle action=Show route=GET /bottles/:bottleID
2016/05/26 15:45:09 [INFO] listen transport=http addr=:8080

2016/05/26 15:46:21 [INFO] started req_id=qAWO65SPCG-1 GET=/bottles/1 from=127.0.0.1 ctrl=BottleController action=Show
2016/05/26 15:46:21 [INFO] params req_id=qAWO65SPCG-1 bottleID=1

 

然后再另外一個窗口發起請求,結果如下:

 qingping.zhang@qpzhangmac  ~  curl -i localhost:8080/bottles/1
HTTP/1.1 200 OK
Content-Type: application/vnd.goa.example.bottle
Date: Thu, 26 May 2016 07:07:40 GMT
Content-Length: 29

{"href":"","id":0,"name":""}

 

到這里,整個安裝使用就OK來。

后面再分析,自動生成的代碼目錄以及如何增加自己的業務邏輯代碼。

 

========================

update 2016.7.17

如果重復執行代碼生成命令時: goagen bootstrap -d goa.demo/design

main.go 和 bottle.go 是不會重新生成的,這就保證業務邏輯代碼不會被覆蓋。

但若是后面新增的Action,要自己在業務邏輯代碼里頭,手動添加函數體代碼。

========================

 


免責聲明!

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



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