当项目逐渐变大之后,服务增多,开发人员增加,单纯的使用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,要自己在业务逻辑代码里头,手动添加函数体代码。

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

GO --微服务框架(一) goa的更多相关文章

  1. GO --微服务框架(二) goa

    之前用过go语言的反射来做一些代码生成,参考这篇. 但是这种方式,入侵太强,需要执行对应的申明调用, 所以对GOA框架的自动生成非常感兴趣,于是仔细研究了一下,发现用的比较巧妙, 这里先卖个关子,先看 ...

  2. [goa]golang微服务框架学习--安装使用

      当项目逐渐变大之后,服务增多,开发人员增加,单纯的使用go来写服务会遇到风格不统一,开发效率上的问题. 之前研究go的微服务架构go-kit最让人头疼的就是定义服务之后,还要写很多重复的框架代码, ...

  3. 基于thrift的微服务框架

    前一阵开源过一个基于spring-boot的rest微服务框架,今天再来一篇基于thrift的微服务加框,thrift是啥就不多了,大家自行百度或参考我之前介绍thrift的文章, thrift不仅支 ...

  4. 基于spring-boot的rest微服务框架

    周末在家研究spring-boot,参考github上的一些开源项目,整了一个rest微服务框架,取之于民,用之于民,在github上开源了,地址如下: https://github.com/yjmy ...

  5. 【GoLang】go 微服务框架 && Web框架学习资料

    参考资料: 通过beego快速创建一个Restful风格API项目及API文档自动化:  http://www.cnblogs.com/huligong1234/p/4707282.html Go 语 ...

  6. 【GoLang】golang 微服务框架 go-kit

    golang-Microservice Go kit - A toolkit for microservices kubernetes go-kit_百度搜索 Peter Bourgon谈使用Go和& ...

  7. Java微服务框架

    Java的微服务框架dobbo.spring boot.redkale.spring cloud 消息中间件RabbitMQ.Kafka.RocketMQ

  8. 基于.NET CORE微服务框架 -surging的介绍和简单示例 (开源)

    一.前言 至今为止编程开发已经11个年头,从 VB6.0,ASP时代到ASP.NET再到MVC, 从中见证了.NET技术发展,从无畏无知的懵懂少年,到现在的中年大叔,从中的酸甜苦辣也只有本人自知.随着 ...

  9. 基于.NET CORE微服务框架 -谈谈surging API网关

    1.前言 对于最近surging更新的API 网关大家也有所关注,也收到了不少反馈提出是否能介绍下Api网关,那么我们将在此篇文章中剥析下surging的Api 网关 开源地址:https://git ...

随机推荐

  1. HDU 5371 Manacher Hotaru's problem

    求出一个连续子序列,这个子序列由三部分ABC构成,其中AB是回文串,A和C相同,也就是BC也是回文串. 求这样一个最长的子序列. Manacher算法是在所有两个相邻数字之间插入一个特殊的数字,比如- ...

  2. JBuilder生成Exe

    首先保证工程可以通过绿箭头执行 然后在File菜单中选择New,先建立Archive下的Application 接下来的界面中大部分可以直接选择“Next”,除了下面的第3步,会询问是否需要将工程引用 ...

  3. github仓库主页介绍

  4. ogre3D程序实例解析1-平移旋转与缩放

    接着上篇写 http://www.cnblogs.com/songliquan/p/3294902.html 旋转 这里有必要看一下关于旋转的源代码:    virtual void pitch(co ...

  5. 01-python进阶-拾遗

    列表复习append(x)追交到链尾extend(L)追加一个列表 等价于 +=insert(i,x)在位置i处插入xremove(x) 删除一个值为x的元素 如果没有抛出异常sort() 直接修改列 ...

  6. queue 类

    一:普通队列 1.队列特征:先进先出,它只允许在一端(队尾)进行插入元素操作,在另一端(队头)进行删除元素操作 2. 存取类函数 front():用来取出queue中的队头元素,对应于front()函 ...

  7. 【Luogu】P2634聪聪可可(树形DP)

    题目链接 水题,时限放得非常宽,暴力DP随便套上一波register就能卡过去. 唯一的遗憾是5A. 树形DP,s[i][j]表示以i为根的子树里距i的距离%3=j的点数,f[i]表示i为根的子树内一 ...

  8. HDU——1003Max Sum(子序列最大和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  9. BZOJ4316 小C的独立集 【仙人掌】

    题目 图论小王子小C经常虐菜,特别是在图论方面,经常把小D虐得很惨很惨. 这不,小C让小D去求一个无向图的最大独立集,通俗地讲就是:在无向图中选出若干个点,这些点互相没有边连接,并使取出的点尽量多. ...

  10. NOIP2017赛前模拟(4):总结

    题目: 1.打牌 给定n个整数(n<=1000000),按照扑克牌对子(x,x)或者顺子(x,x+1,x+2)打出牌···问最多可以打出多少次对子或者顺子?牌的大小<=1000000 2. ...