Write microservice in golang
Transcript of Write microservice in golang
1
Build MicroServices in GolangBo-Yi Wu
2016.08.21
Bo-Yi Wu (appleboy)https://blog.wu-boy.com
https://github.com/appleboy 2
Agenda• What are Microservices?• Why Microservices?• Golang Introduction• Golang Testing Tool• Golang Deployment
3
4
What are Microservices?• Fulfill only one task• Application is a suite of small services and
size• Communicate with lightweight
mechanisms, often REST • Written in different programming languages• Use different data storage technologies
Why Microservices?• Scaling Agility • Migrating Legacy Applications • Sustainable Development Speed • Continuous Delivery • Independent Scalability • Technology Freedom
5
6
Golang Introduction
7
Who made this thing?
Robert Griesemer, Rob Pike and Ken Thompson
Frequently Asked Questionshttps://golang.org/doc/faq
8
Why Choose Golang• Performance• Multiple core• Concurrency• Compiled && portable• Clean syntax• Powerful standard library• Strong type• Open source
9
Go, Open SourceDo Less. Enable More.
https://blog.golang.org/open-source
10
11
Portable
https://github.com/mitchellh/gox
12
13https://talks.golang.org/2014/gocon-tokyo.slide
14https://talks.golang.org/2014/gocon-tokyo.slide
15
companies using Go
16How We Moved Our API From Ruby to Go and Saved Our Sanity
17ScaleDrone Websocket 平台從 Node.js 轉換到 Golang
18HOW WE BUILT UBER ENGINEERING’S HIGHEST QUERY PER SECOND SERVICE USING GO
Companies currently using Go throughout the world
https://github.com/golang/go/wiki/GoUsers
19
How to Write Go Codehttps://golang.org/doc/code.html
21
Getting Started• Workspaces• GOPATH environment variable• Import paths• Your first program• Your first library• Package names• Testing
22
Workspaces• Src– contains Go source files
• Pkg– contains package objects
• Bin– contains executable commands
23
24
GOPATH Variable$ mkdir $HOME/work$ export GOPATH=$HOME/work$ export PATH=$PATH:$GOPATH/bin
25
Import paths$ mkdir -p $GOPATH/src/github.com/user
26
27
Your first program$ mkdir –p $GOPATH/src/github.com/user/hello$ touch $GOPATH/src/github.com/user/hello/hello.go
28
Build and install package$ go install github.com/user/hello
29
30
You will find hello binary$GOPATH/bin/hello
$HOME/work/bin/hello
Your first library$ mkdir $GOPATH/src/github.com/user/stringutil
31
32
SWAP
33
34
Tesing in Go$GOPATH/src/github.com/user/stringutil/
reverse_test.go
35
36
Learn GoLang For Great Good Part: Unit Testing in Go
https://goo.gl/WhAV5P
37
38
Teamwork in
http://aib.edu.au/blog/top-tips-effective-teamwork/
39
Check Style
Unit Testing
Code Review
Develop
Effective Gohttps://golang.org/doc/effective_go.html
40
41
Code Review Commentshttps://github.com/golang/go/wiki/CodeReviewComments
42
Concurrency in go
Go routines
43
44
45
46
Go synchronization
47
GOMAXPROCSmaximum number of CPU
48
49
sets the maximum number of CPUs
50
Race Condition
51
52
sync/Mutex
53
54
sync/atomic
55
Channel in go
56
57
58
Command line in Go
59https://github.com/appleboy/gorush
60
Build a MicroService
61
• Configuration– Yaml, JSON or INI format
• Logger– Access and error log format– write file or output to console
• Web API– JSON or XML format
• App and System status– Memory, go version, uptime, response time …
• Deployment / Upgrades– Docker image or binary file
62
63
Configuration• Yaml– https://github.com/go-gas/config
• INI– https://github.com/go-ini/ini
• JSON– https://github.com/spf13/viper
LoggerStructured, pluggable logging for Go.https://github.com/Sirupsen/logrus
64
Build HTTP APIWhy You No Framework?
https://goo.gl/ZlMtpN
65
Web Framework• Gin• Echo• Httprouter• Mux• Go-json-rest• Gas 66
67
Monitoring App Status
68
69
70
Golang Testing ToolJust one command
71hello.go
72hello_test.go
73
go test –v –cover
74
To run only specific test go test –run xxxxx
75
Tesing Coverage for gogo test –
coverprofile=covergae.out
76
Viewing the resultsgo tool cover –html=coverage.out
77
78
linter for Go source codegolint -set_exit_status packagehttps://github.com/golang/lint
79
Gofmt formats Go programs.go list ./... | grep -v vendor | xargs go fmt
https://golang.org/cmd/gofmt/
80
81
+Testing Report Testing View
Coverage Reportinghttps://github.com/axw/gocov
https://github.com/AlekSi/gocov-xml
82
83
Testing ReportingConvert go test output to junit xml
https://github.com/jstemmer/go-junit-report
84
85
86
87
Go Lint Reportinghttps://github.com/golang/lint
88
89
90
91
Run test for multiple package?
+
92
+Testing Report Testing View
93
Docker image includes Golang coverage tools for testing
https://github.com/appleboy/golang-testing
94
Feature• Convert go test output to junit xml• Coverage testing tool• XML (Cobertura) export• Linter for Go source code• Package Management for Golang• Testing multiple Golang package
95
Testing without Docker
Download coverage script and copy to bin folder
97
Run docker command
docker run --rm \ -v $(PWD):$PROJECT_PATH \ -w=$PROJECT_PATH \ appleboy/golang-testing \ sh -c ”make install && coverage all"
99
docker-compose configdocker-compose run golang-
testingdocker-compose down
101
103
104
Output all report files
105
Xml and txt format
106
107
Golang Deployment
108
Build Go binary command
109
Go build command
GOOS=linux \GOARCH=amd64 \CGO_ENABLED=0 \go build \ -ldflags="-s -w -X main.Version=${v}" \ -o bin/hello go-hello
110
Prepare golang Docker image
Build go file
Add bin file to
Docker image
Deploy image to Docker registry
111
Prepare golang Docker image
Build go file
Add bin file to
Docker image
Deploy image to Docker registry
112
Clone source
Build binary
file
Tar binary
file
113
Clone source into docker
114
Build binary file
115Tar binary file
116
Prepare Docker Image
docker build -t $(IMAGE) -f Dockerfile.build .
117
Prepare golang Docker image
Build go file
Add bin file to
Docker image
Deploy image to Docker registry
Output binary filedocker run $(BUILD_IMAGE) > build.tar.gz
118
119
Prepare golang Docker image
Build go file
Add bin file to
Docker image
Deploy image to Docker registry
Prepare production image
120
121
Untar binary file
Build production imagedocker build –t hello-world -f Dockerfile.dist .
122
Test your production imagedocker run -d -p 8088:8000 hello-world
123
124
Prepare golang Docker image
Build go file
Add bin file to
Docker image
Deploy image to Docker registry
docker push $(ACCOUNT)/$(IMAGE):$(tag)
Gas web framework
https://github.com/go-gas/gas
125
126
Any Question?