[goa]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
// :: [INFO] mount ctrl=Bottle action=Show route=GET /bottles/:bottleID
// :: [INFO] listen transport=http addr=: // :: [INFO] started req_id=qAWO65SPCG- GET=/bottles/ from=127.0.0.1 ctrl=BottleController action=Show
// :: [INFO] params req_id=qAWO65SPCG- bottleID=
然后再另外一个窗口发起请求,结果如下:
qingping.zhang@qpzhangmac ~ curl -i localhost:/bottles/
HTTP/1.1 OK
Content-Type: application/vnd.goa.example.bottle
Date: Thu, May :: GMT
Content-Length: {"href":"","id":,"name":""}
到这里,整个安装使用就OK来。
后面再分析,自动生成的代码目录以及如何增加自己的业务逻辑代码。
========================
update 2016.7.17
如果重复执行代码生成命令时: goagen bootstrap -d goa.demo/design
main.go 和 bottle.go 是不会重新生成的,这就保证业务逻辑代码不会被覆盖。
但若是后面新增的Action,要自己在业务逻辑代码里头,手动添加函数体代码。
========================
[goa]golang微服务框架学习--安装使用的更多相关文章
- [goa]golang微服务框架学习(二)-- 代码自动生成
之前用过go语言的反射来做一些代码生成,参考这篇. 但是这种方式,入侵太强,需要执行对应的申明调用, 所以对GOA框架的自动生成非常感兴趣,于是仔细研究了一下,发现用的比较巧妙, 这里先卖个关子,先看 ...
- [goa]golang微服务框架学习(三)-- 使用swagger-ui展示API
既然goa框架自动生成啦swagger-json文件,那么如何用swagger-ui展示出来呢? 这里分三步: 1.下载swagger-ui的web代码 2.添加swagger.json 和 swag ...
- kratos微服务框架学习笔记一(kratos-demo)
目录 kratos微服务框架学习笔记一(kratos-demo) kratos本体 demo kratos微服务框架学习笔记一(kratos-demo) 今年大部分时间飘过去了,没怎么更博和githu ...
- 【GoLang】golang 微服务框架 go-kit
golang-Microservice Go kit - A toolkit for microservices kubernetes go-kit_百度搜索 Peter Bourgon谈使用Go和& ...
- golang微服务框架go-micro 入门笔记2.2 micro工具之微应用利器micro web
micro web micro 功能非常强大,本文将详细阐述micro web 命令行的功能 阅读本文前你可能需要进行如下知识储备 golang分布式微服务框架go-micro 入门笔记1:搭建go- ...
- golang微服务框架go-micro 入门笔记2.4 go-micro service解读
本章节阐述go-micro 服务发现原理 go-micro架构 下图来自go-micro官方 阅读本文前你可能需要进行如下知识储备 golang分布式微服务框架go-micro 入门笔记1:搭建go- ...
- golang微服务框架go-micro 入门笔记2.3 micro工具之消息接收和发布
本章节阐述micro消息订阅和发布相关内容 阅读本文前你可能需要进行如下知识储备 golang分布式微服务框架go-micro 入门笔记1:搭建go-micro环境, golang微服务框架go-mi ...
- golang微服务框架go-micro 入门笔记1.搭建 go-micro环境
微服务的本质是让专业的人做专业的事情,做出更好的东西. golang具备高并发,静态编译等特性,在性能.安全等方面具备非常大的优势.go-micro是基于golang的微服务编程框架,go-micro ...
- 【GoLang】golang 微服务框架 介绍
原文如下: rpcx是一个类似阿里巴巴 Dubbo 和微博 Motan 的分布式的RPC服务框架,基于Golang net/rpc实现. 谈起分布式的RPC框架,比较出名的是阿里巴巴的dubbo,包括 ...
随机推荐
- org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [41] did not match expected type [java.lang.Integer (n/a)];
题记:以前记录过一些自己遇到的BUG,这个行为,让我一看报错的提示信息就能定位到问题的所在,后来记得比较多了,好多是重复性的再加上比较忙就没有详细的记录了,今天的工作量比较小,就顺便记录一下,以便以后 ...
- OpenWrt自定义和官方一样的固件
我用的OpenWrt版本是Barrier Breaker 14.07,硬件是NetGear WNDR4300. 我自定义固件的目的是把固件的根分区扩到最大(100MB,总FLASH是128MB),试过 ...
- centos7使用传统网卡名
http://serverfault.com/questions/692897/centos-7-disable-predictable-network-interface-names-during- ...
- cnblogs,我回来了
之前是在Github上搭了个博客,原因只有一个:可以弄个比较个性的域名,逼格高. 不过用起来倒是麻烦,一是经常纠结自己的主页是不是不够逼格?二就是身在墙内,访问速度不理想. 所以,还是安心的在这里,写 ...
- 常见HTTP状态码(200、301、302、500等)
HTTP状态码,它是用以表示网页服务器HTTP响应状态的3位数字代码.状态码的第一个数字代表了响应的五种状态之一. 1XX系列:指定客户端应相应的某些动作,代表请求已被接受,需要继续处理.由于 HTT ...
- RunTime的简单使用
Runtime也就是运行时,是苹果官方提供的一套C语音库,那有什么作用呢?简单的来说,Runtime可以做很多的底层操作,比如说访问隐藏的一些成员变量,成员方法,当然包括了私有的成员变量,成员方法. ...
- smarty 模板的入门使用
<?php require_once 'inc/libs/Smarty.class.php'; $s=new Smarty(); // echo $s::SMARTY_VERSION; // e ...
- 装饰模式(Decorator pattern)
装饰模式(Decorator pattern): 又名包装模式(Wrapper pattern), 它以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式以对客户透明的方式动态的给 ...
- js计时事件
通过在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行,我们称之为计时事件. 1. setTimeout()--暂停指定的时间后执行指定的代码 clearTimeout ()--停止se ...
- LinQ的组合+分页
前台代码: 名称:<asp:TextBox ID="Textname" runat="server"></asp:TextBox> 油耗 ...