[Goa]golang Micro-Service Framework learning-installation and use

Source: Internet
Author: User
Tags gocode
When the project becomes bigger, the service grows, the developer increases, the simple use of Go to write service will encounter the problem of the style is not uniform, development efficiency. Previous research on Go's micro-service architecture Go-kit The most frustrating thing is to define a service, write a lot of repetitive framework code, and always think about how to use IDL to describe the service, and then automatically generate the framework code. Until I met Goa, another go to the micro-service framework.   Specific introduction to see this article, there are official website.   This product implements the framework of the code automatically generated (automatically generated code can be hot update, because the generated code and its own code is separate), and the concept is also more fashionable, based on API design, the use of plug-ins to expand the business logic.   So I want to learn to play a bit quickly. Suppose you've installed a Golang environment. So, start downloading.
Go to Github.com/goadesign/goa go to
github.com/goadesign/goa/goagen go

install Github.com/goadesign/goa Go
Install Github.com/goadesign/goa/goagen

Once installed, you can use the Help command to see how it is used.

Qingping.zhang@qpzhangmac ~/dev Goagen--help The Goagen tool-generates artifacts from 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 code and main skeleton code (if not already present) as as as a client package and tool with swagger for th

E 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 T client schema Generate JSON schema gen Run third-party Generator bootstrap equivalent to running th

E "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/d EV ") use" Goagen [command]--help ' For more information about a command.

You will then need YAML package support, so you need to install it in advance:

Go to Gopkg.in/yaml.v2

You must create a new directory (for example: Goa.demo) in the directory of Gopath SRC, and then create a new subdirectory of the design underneath it.

New file Design.go under the sub-design directory

DESIGN.GO Package Design//The Convention consists of naming the 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 Microser Vice 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 RESTful 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 Q
                        Uerystring 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-making requests on the bottle") Attribute ("name", String, "name of Wine") Required ("id", "href", "name")}) View ("Default", Func () {//View defines a render
                ing of the media type.                   Attribute ("id")//Media types may have multiple views and must attribute ("href")
                Have a "default" view. Attribute ("name")})

Then use the command to generate the framework code automatically (note that if you specify the output directory without the-o option, the code will be generated to the current directory):

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

Ni-ma, ka-ka ... Generate a lot.

The framework defaults to generating the return value of the structure, so we don't make any changes here, because the generated code directory is not yet covered.

So we compile directly, run and see.

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 Transpo addr=:8080 2016/05/26 15:46:21 [INFO] started req_id=qawo65spcg-1 get=/bottles/1 from=127.0.0.1 rt=http ctrl= Bottlecontroller action=show 2016/05/26 15:46:21 [INFO] params req_id=qawo65spcg-1 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.