Slide 1

Slide 1 text

Build MicroServices in Golang Bo-Yi Wu 2016.08.21 1  

Slide 2

Slide 2 text

Bo-Yi Wu (appleboy) https://blog.wu-boy.com/ https://github.com/appleboy 2

Slide 3

Slide 3 text

Agenda •  What are Microservices? •  Why Microservices? •  Golang Introduction •  Golang Testing Tool •  Golang Deployment 3

Slide 4

Slide 4 text

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  

Slide 5

Slide 5 text

Why Microservices? •  Scaling Agility •  Migrating Legacy Applications •  Sustainable Development Speed •  Continuous Delivery •  Independent Scalability •  Technology Freedom 5

Slide 6

Slide 6 text

Golang Introduction 6  

Slide 7

Slide 7 text

Who made this thing? 7   Robert Griesemer, Rob Pike and Ken Thompson

Slide 8

Slide 8 text

Frequently Asked Questions https://golang.org/doc/faq 8

Slide 9

Slide 9 text

Why Choose Golang •  Performance •  Multiple core •  Concurrency •  Compiled && portable •  Clean syntax •  Powerful standard library •  Strong type •  Open source 9

Slide 10

Slide 10 text

Go, Open Source Do Less. Enable More. https://blog.golang.org/open-source 10

Slide 11

Slide 11 text

Portable   11   https://github.com/mitchellh/gox

Slide 12

Slide 12 text

12  

Slide 13

Slide 13 text

13   https://talks.golang.org/2014/gocon-­‐tokyo.slide  

Slide 14

Slide 14 text

14   https://talks.golang.org/2014/gocon-­‐tokyo.slide  

Slide 15

Slide 15 text

companies using Go 15  

Slide 16

Slide 16 text

16   How We Moved Our API From Ruby to Go and Saved Our Sanity

Slide 17

Slide 17 text

17   ScaleDrone Websocket 平台從 Node.js 轉換到 Golang

Slide 18

Slide 18 text

18   HOW WE BUILT UBER ENGINEERING’S HIGHEST QUERY PER SECOND SERVICE USING GO

Slide 19

Slide 19 text

Companies currently using Go throughout the world https://github.com/golang/go/wiki/GoUsers 19

Slide 20

Slide 20 text

Farewell Node.js TJ Holowaychuk https://goo.gl/WVxwtb 20

Slide 21

Slide 21 text

How to Write Go Code https://golang.org/doc/code.html 21

Slide 22

Slide 22 text

Getting Started •  Workspaces •  GOPATH environment variable •  Import paths •  Your first program •  Your first library •  Package names •  Testing 22

Slide 23

Slide 23 text

Workspaces •  Src – contains Go source files •  Pkg – contains package objects •  Bin – contains executable commands 23

Slide 24

Slide 24 text

24  

Slide 25

Slide 25 text

GOPATH Variable $ mkdir $HOME/work $ export GOPATH=$HOME/work $ export PATH=$PATH:$GOPATH/bin 25

Slide 26

Slide 26 text

Import paths $ mkdir -p $GOPATH/src/github.com/user 26

Slide 27

Slide 27 text

Your first program $ mkdir –p $GOPATH/src/github.com/user/hello $ touch $GOPATH/src/github.com/user/hello/hello.go 27  

Slide 28

Slide 28 text

28  

Slide 29

Slide 29 text

Build and install package $ go install github.com/user/hello 29

Slide 30

Slide 30 text

You will find hello binary $GOPATH/bin/hello $HOME/work/bin/hello 30  

Slide 31

Slide 31 text

Your first library $ mkdir $GOPATH/src/github.com/user/stringutil 31

Slide 32

Slide 32 text

32   SWAP

Slide 33

Slide 33 text

33  

Slide 34

Slide 34 text

34  

Slide 35

Slide 35 text

Tesing in Go $GOPATH/src/github.com/user/stringutil/ reverse_test.go 35

Slide 36

Slide 36 text

36  

Slide 37

Slide 37 text

Learn GoLang For Great Good Part: Unit Testing in Go https://goo.gl/WhAV5P 37

Slide 38

Slide 38 text

38   Teamwork in http://aib.edu.au/blog/top-­‐tips-­‐effective-­‐teamwork/  

Slide 39

Slide 39 text

39   Code Commit Check Style Unit Testing Code Review Develop

Slide 40

Slide 40 text

Effective Go https://golang.org/doc/effective_go.html 40

Slide 41

Slide 41 text

41  

Slide 42

Slide 42 text

Code Review Comments https://github.com/golang/go/wiki/CodeReviewComments 42

Slide 43

Slide 43 text

Concurrency in go Go routines 43

Slide 44

Slide 44 text

44  

Slide 45

Slide 45 text

45  

Slide 46

Slide 46 text

Go synchronization 46  

Slide 47

Slide 47 text

47  

Slide 48

Slide 48 text

GOMAXPROCS maximum number of CPU 48

Slide 49

Slide 49 text

49   sets the maximum number of CPUs

Slide 50

Slide 50 text

Race Condition 50  

Slide 51

Slide 51 text

51  

Slide 52

Slide 52 text

sync/Mutex 52  

Slide 53

Slide 53 text

53  

Slide 54

Slide 54 text

sync/atomic 54  

Slide 55

Slide 55 text

55  

Slide 56

Slide 56 text

Channel in go 56

Slide 57

Slide 57 text

57  

Slide 58

Slide 58 text

Command line in Go 58  

Slide 59

Slide 59 text

59   https://github.com/appleboy/gorush

Slide 60

Slide 60 text

Build a MicroService 60  

Slide 61

Slide 61 text

•  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  

Slide 62

Slide 62 text

62  

Slide 63

Slide 63 text

Configuration   •  Yaml – https://github.com/go-gas/config •  INI – https://github.com/go-ini/ini •  JSON – https://github.com/spf13/viper 63  

Slide 64

Slide 64 text

Logger Structured, pluggable logging for Go. https://github.com/Sirupsen/logrus 64

Slide 65

Slide 65 text

Build HTTP API Why You No Framework? https://goo.gl/ZlMtpN 65

Slide 66

Slide 66 text

Web Framework •  Gin •  Echo •  Httprouter •  Mux •  Go-json-rest •  Gas 66

Slide 67

Slide 67 text

Monitoring App Status 67  

Slide 68

Slide 68 text

68  

Slide 69

Slide 69 text

69  

Slide 70

Slide 70 text

Golang Testing Tool   Just one command 70  

Slide 71

Slide 71 text

71   hello.go

Slide 72

Slide 72 text

72   hello_test.go

Slide 73

Slide 73 text

go test –v –cover 73  

Slide 74

Slide 74 text

74  

Slide 75

Slide 75 text

To run only specific test go test –run xxxxx 75

Slide 76

Slide 76 text

Tesing Coverage for go go test –coverprofile=covergae.out 76

Slide 77

Slide 77 text

Viewing the results go tool cover –html=coverage.out 77

Slide 78

Slide 78 text

78  

Slide 79

Slide 79 text

linter for Go source code golint -set_exit_status package https://github.com/golang/lint 79

Slide 80

Slide 80 text

Gofmt formats Go programs. go list ./... | grep -v vendor | xargs go fmt https://golang.org/cmd/gofmt/ 80

Slide 81

Slide 81 text

81   + Testing Report Testing View

Slide 82

Slide 82 text

Coverage Reporting https://github.com/axw/gocov https://github.com/AlekSi/gocov-xml 82

Slide 83

Slide 83 text

83  

Slide 84

Slide 84 text

Testing Reporting Convert go test output to junit xml https://github.com/jstemmer/go-junit-report 84

Slide 85

Slide 85 text

85  

Slide 86

Slide 86 text

86  

Slide 87

Slide 87 text

87  

Slide 88

Slide 88 text

Go Lint Reporting https://github.com/golang/lint 88

Slide 89

Slide 89 text

89  

Slide 90

Slide 90 text

90  

Slide 91

Slide 91 text

Run test for multiple package? 91   +

Slide 92

Slide 92 text

92   + Testing Report Testing View

Slide 93

Slide 93 text

Docker image includes Golang coverage tools for testing https://github.com/appleboy/golang-testing 93  

Slide 94

Slide 94 text

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  

Slide 95

Slide 95 text

Testing without Docker Download coverage script and copy to bin folder 95  

Slide 96

Slide 96 text

96   https://goo.gl/XpRfvp

Slide 97

Slide 97 text

Run docker command 97   docker run --rm \ -v $(PWD):$PROJECT_PATH \ -w=$PROJECT_PATH \ appleboy/golang-testing \ sh -c ”make install && coverage all"

Slide 98

Slide 98 text

Run docker-compose command https://goo.gl/0JMnlo 98

Slide 99

Slide 99 text

docker-compose config docker-compose run golang-testing docker-compose down 99  

Slide 100

Slide 100 text

docker-compose.yml file https://goo.gl/0JMnlo 100

Slide 101

Slide 101 text

101  

Slide 102

Slide 102 text

Golang Dockerfile https://goo.gl/Vnt7Zc 102

Slide 103

Slide 103 text

103  

Slide 104

Slide 104 text

104  

Slide 105

Slide 105 text

Output all report files 105 Xml and txt format

Slide 106

Slide 106 text

106  

Slide 107

Slide 107 text

Golang Deployment   107  

Slide 108

Slide 108 text

Build Go binary command 108  

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

110   Prepare golang Docker image Build go file Add bin file to Docker image Deploy image to Docker registry

Slide 111

Slide 111 text

111   Prepare golang Docker image Build go file Add bin file to Docker image Deploy image to Docker registry

Slide 112

Slide 112 text

112   Clone source Build binary file Tar binary file

Slide 113

Slide 113 text

113   Clone source into docker

Slide 114

Slide 114 text

114   Build binary file

Slide 115

Slide 115 text

115   Tar binary file

Slide 116

Slide 116 text

Prepare Docker Image docker build -t $(IMAGE) -f Dockerfile.build . 116  

Slide 117

Slide 117 text

117   Prepare golang Docker image Build go file Add bin file to Docker image Deploy image to Docker registry

Slide 118

Slide 118 text

Output binary file docker run $(BUILD_IMAGE) > build.tar.gz 118

Slide 119

Slide 119 text

119   Prepare golang Docker image Build go file Add bin file to Docker image Deploy image to Docker registry

Slide 120

Slide 120 text

Prepare production image 120

Slide 121

Slide 121 text

121   Untar binary file

Slide 122

Slide 122 text

Build production image docker build –t hello-world -f Dockerfile.dist . 122

Slide 123

Slide 123 text

Test your production image docker run -d -p 8088:8000 hello-world 123

Slide 124

Slide 124 text

124   Prepare golang Docker image Build go file Add bin file to Docker image Deploy image to Docker registry docker push $(ACCOUNT)/$(IMAGE):$(tag)

Slide 125

Slide 125 text

Gas web framework https://github.com/go-gas/gas 125

Slide 126

Slide 126 text

Any Question? 126