Build MicroServices in Golang Bo-Yi Wu 2016.08.21 1
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
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 4
Why Microservices? • Scaling Agility • Migrating Legacy Applications • Sustainable Development Speed • Continuous Delivery • Independent Scalability • Technology Freedom 5
Golang Introduction 6
Who made this thing? 7Robert Griesemer, Rob Pike and Ken Thompson
Frequently Asked Questions https://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 Source Do Less. Enable More. https://blog.golang.org/open-source 10
Portable 11 https://github.com/mitchellh/gox
12
13 https://talks.golang.org/2014/gocon-tokyo.slide
14 https://talks.golang.org/2014/gocon-tokyo.slide
companies using Go 15
16 How We Moved Our API From Ruby to Go and Saved Our Sanity
17 ScaleDrone Websocket 平台從 Node.js 轉換到 Golang
18 HOW 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
Farewell Node.js TJ Holowaychuk https://goo.gl/WVxwtb 20
How to Write Go Code https://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
Your first program $ mkdir –p $GOPATH/src/github.com/user/hello $ touch $GOPATH/src/github.com/user/hello/hello.go 27
28
Build and install package $ go install github.com/user/hello 29
You will find hello binary $GOPATH/bin/hello $HOME/work/bin/hello 30
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_t est.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 Go https://golang.org/doc/effective_go.html 40
41
Code Review Comments https://github.com/golang/go/wiki/CodeReviewComments 42
Concurrency in go Go routines 43
44
45
Go synchronization 46
47
GOMAXPROCS maximum number of CPU 48
49 sets the maximum number of CPUs
Race Condition 50
51
sync/Mutex 52
53
sync/atomic 54
55
Channel in go 56
57
Command line in Go 58
59https://github.com/appleboy/gorush
Build a MicroService 60
• 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 61
62
Configuration • Yaml – https://github.com/go-gas/config • INI – https://github.com/go-ini/ini • JSON – https://github.com/spf13/viper 63
Logger Structured, pluggable logging for Go. https://github.com/Sirupsen/logrus 64
Build HTTP API Why You No Framework? https://goo.gl/ZlMtpN 65
Web Framework • Gin • Echo • Httprouter • Mux • Go-json-rest • Gas 66
Monitoring App Status 67
68
69
Golang Testing Tool Just one command 70
71 hello.go
72 hello_test.go
go test –v –cover 73
74
To run only specific test go test –run xxxxx 75
Tesing Coverage for go go test –coverprofile=covergae.out 76
Viewing the results go tool cover –html=coverage.out 77
78
linter for Go source code golint -set_exit_status package https://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 Reporting https://github.com/axw/gocov https://github.com/AlekSi/gocov-xml 82
83
Testing Reporting Convert go test output to junit xml https://github.com/jstemmer/go-junit-report 84
85
86
87
Go Lint Reporting https://github.com/golang/lint 88
89
90
Run test for multiple package? 91 +
92 + Testing Report Testing View
Docker image includes Golang coverage tools for testing https://github.com/appleboy/golang-testing 93
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 94
Testing without Docker Download coverage script and copy to bin folder 95
96 https://goo.gl/XpRfvp
Run docker command 97 docker run --rm -v $(PWD):$PROJECT_PATH -w=$PROJECT_PATH appleboy/golang-testing sh -c ”make install && coverage all"
Run docker-compose command https://goo.gl/0JMnlo 98
docker-compose config docker-compose run golang- testing docker-compose down 99
docker-compose.yml file https://goo.gl/0JMnlo 100
101
Golang Dockerfile https://goo.gl/Vnt7Zc 102
103
104
Output all report files 105 Xml and txt format
106
Golang Deployment 107
Build Go binary command 108
Go build command 109 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
Prepare Docker Image docker build -t $(IMAGE) -f Dockerfile.build . 116
117 Prepare golang Docker image Build go file Add bin file to Docker image Deploy image to Docker registry
Output binary file docker 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 image docker build –t hello-world -f Dockerfile.dist . 122
Test your production image docker 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
Any Question? 126

Write microservice in golang

Editor's Notes

  • #21 https://medium.com/@tjholowaychuk/farewell-node-js-4ba9e7f3e52b#.ytdscr9d0