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

Source: Internet
Author: User
Tags gocode
This is a creation in Article, where the information may have evolved or changed. when the project gradually becomes larger, the service increases, the developers increase, the simple use of go to write services will encounter style is not unified, development efficiency problems. Previous research on Go's microservices architecture Go-kit The most frustrating thing is that after defining a service, you write a lot of repetitive framework code, and then you want to use IDL to describe the service, and then generate the framework code automatically. Until we meet the old one. Goa, another go microservices framework. Specific introduction Look at this, there are official website. The goods achieve the framework of the code automatically generated (automatically generated code can be hot update, because the generated code and their own code is separate), and the concept is more fashionable, based on the API design, the use of plug-ins to expand the business logic. So I want to learn to play a bit quickly. Assume that you have installed a Golang environment. So, start the download.
get github.com/goadesign/get github.com/goadesign/goa/Goagengo Install github.com /goadesign/goago 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--helpthe Goagen tool generates artifacts froma Goa service design package. Each command supported by the tool produces a specific type of artifacts. For ExampleThe"app"command generates the code that supports the service controllers. the"Bootstrap"Command runs the"app","Main","Client"and"Swagger"commands generating thecontrollers supporting code and main skeleton code (ifnot already present) asWell asa clientpackage and tool and the Swagger specification forThe API. Usage:goagen [command]available commands:app Generate application code main Generate application Scaff   Olding 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,--designstringDesign Package Import path-O,-- out stringOutput directory (default "/users/qingpingzhang/dev") Use"goagen [command]--help"  forMore information about a command.

The support for the YAML package is then required, so it needs to be installed in advance:

get gopkg. in/yaml.v2

You must create a new directory (for example: Goa.demo) in the SRC directory of Gopath, and then create a new sub-directory under it.

Sub-design directory, new file Design.go

//Design.goPackage 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 andTitle ("The virtual wine cellar")//Other global properties. There should be oneDescription ("A Simple Goa service")//And exactly one API definition appearing inScheme ("http")//The design.Host ("localhost:8080")})var_ = Resource ("Bottle", func () {//Resources Group related API endpointsBasePath ("/bottles")//together. They map to rest of resources for RESTDefaultmedia (Bottlemedia)//Services.Action ("Show", func () {//Actions Define a single API endpoint togetherDescription ("Get bottle by ID")//with its path, parameters (both pathRouting (GET ("/:bottleid"))//parameters and QueryString values) and payloadParams (func () {//(Shape of the request body).Param ("Bottleid", Integer,"Bottle ID")}) Response (OK)//responses define the shape and status codeResponse (NotFound)//of HTTP responses.        })})//Bottlemedia defines the media type used to render bottles.varBottlemedia = 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 has multiple views and mustAttribute ("href")//There is a "default" view.Attribute ("name")        })})

Then use the command to automatically generate the skeleton code (note here that if you specify an output directory without the-o option, the code is generated into the current directory):

Qingping.zhang@qpzhangmac ~/gocode/src/goa.demo/Goagen bootstrap-d goa.demo/ designappapp/Contexts.goapp/Controllers.goapp/Hrefs.goapp/Media_types.goapp/User_types.goapp/TestApp/test/bottle.gomain.gobottle.goclientclient/cellar-cliclient/cellar-cli/main.goclient/cellar-cli/commands.goclient/client.goclient/bottle.goclient/Datatypes.goswaggerswagger/Swagger.jsonswagger/swagger.yaml

A. Generate a lot.

The framework will generate the return value of the structure by default, so we won't make any changes here, because the generated code directory is not yet covered.

So we compile directly and run a look.

Go Build
Qingping.zhang@qpzhangmac ~/gocode/src/goa.demo./Goa.demo  ./ to/ -  the: $: the[INFO] Mount Ctrl=bottle action=show route=get/bottles/: Bottleid ./ to/ -  the: $: the[INFO] Listen transport=http addr=:8080 ./ to/ -  the: $: +[INFO] started req_id=qawo65spcg-1get=/bottles/1  from=127.0.0.1Ctrl=bottlecontroller action=Show ./ to/ -  the: $: +[INFO]paramsreq_id=qawo65spcg-1Bottleid=1

Then another window initiates the request, and the result is as follows:

Qingping.zhang@qpzhangmac ~ curl-i localhost:8080/bottles/1HTTP/1.1  $okcontent-type:application/Vnd.goa.example.bottleDate:Thu, -May .  -: -: +gmtcontent-length: in{"href":"","ID":0,"name":""}

Here, the entire installation is OK to use.

The subsequent analysis, the automatically generated code catalog, and how to add your own business logic code.

Related Article

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.