Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Write microservice in golang

11,759 views

Published on

* What are Microservices?
* Why Microservices?
* Golang Introduction
* Golang Testing Tool
* Golang Deployment with Docker

Published in: Software
  • Be the first to comment

Write microservice in golang

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

×