Gorush: A push notification server written in Go

appleboy 13,930 views 104 slides Oct 28, 2017
Slide 1
Slide 1 of 104
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104

About This Presentation

* Why I create the Gorush Project?
* Detail implementation in Golang
* Testing and deploy Go project.
* Run Gorush in Kubernetes.


Slide Content

A push notification server written in Go Gorush 1 Bo-Yi Wu M opcon @ 2017. 10 .2 8

Why I create the Gorush Project? Detail implementation in Golang Testing and deploy Go project. R un Gorush in Kubernetes . 2 Outline

Mediatek Engineer DevOps Golang , PHP, Node.js .. Some open source Gitea Gin Drone appleboy @GitHub appleboy @twitter appleboy @ slideshare appleboy46 @facebook About Me 3

drone/drone go-gitea/gitea gin -gonic /gin appleboy/ g orush Go Open Source in GitHub 4

drone/drone go-gitea/gitea gin -gonic /gin appleboy/ g orush Go Open Source in GitHub 5

https://mcs.mediatek.com / 6

Mediatek C loud Sandbox Trigger Push Email hook MQTT TCP HTTP Mediatek Cloud Sandbox 7

Mediatek C loud Sandbox Trigger Push Email hook MQTT TCP HTTP Mediatek Cloud Sandbox 8 Forcus on Push notification

https://github.com/appleboy/ gorush Gorush 9

A general push notification server for smartphone application Gorush 10

Written in Go Push requests to APNs and FCM Simple HTTP APIs ( JSON ) RPC Protocol ( gRPC ) Gorush 11

Overview Client FCM A PNs Gorush POST /push JSON Body HTTPS APNs Protocol 12

System Requirement Good Performance High Concurrency Simple net /http Package Easy to Learn Why I Choose Go 13

https://github.com/golang/go/wiki/ FromXToGo Why we switch From XX to Go 14

https://github.com/golangtw/ jobs Who use Go in Taiwan 15

16 Push System A rchitecture

Before S even years ago FCM APNs API Server API Server API Server PUSH 17

Before Two Years ago FCM APNs Worker Worker Worker PUSH Queue API API API d e-queue Asynchronous Notification AWS en-queue 18

Nginx Traefik Now … FCM APNs Gorush Gorush Gorush PUSH API API API Http Post /push Gorush = Queue + Worker 19

How to implement simple notification service Gorush Architecture 20

How to send notification Client FCM A PNs Gorush POST /push JSON Body HTTPS APNs Protocol 21

Http Server g RPC Server Push API Worker 22 Gorush Component

n et/http + Gin Route+ Facebook Grace 23

Push API using Gin Framework 24

Initial Worker Queue 25

API Request and Response 26

Send notification to Queue 27

Start Worker 28

Gorush Major Feature 29

GET /api/stat/ go GET /api/stat/ app GET /sys/ stats GET / metrics POST /api/ push GET / healthz API List 30

GET /api/stat/go 31

GET /api/stat / app 32

GET / sys/stats 33

GET /metrics ( P rometheus ) 34

GET /metrics ( P rometheus + G rafana ) $ for i in {1.. 99999} ; do bat - b.N =1000 - b.C =100 POST local: 8088/ api / push 35

GET /metrics ( P rometheus + G rafana ) 36

https://github.com/appleboy/gorush/#post- apipush POST /api/push 37

Push API S upport Sync Mode A sync vs. Sync Push 38

Notification Notification Notification Notification worker worker worker Client Notification worker WaitGroup 39

Notification Log Wait response S end to Work Queue 40

41

$ gorush Start Gorush Server 42

$ gorush – c config.yml Custom config file 43

$ GORUSH_CORE_PORT =8089 gorush Global Variable 44

/ etc / gorush / config.yml $HOME/. gorush / config.yml . (Current Folder) 45 Auto Load Config file

Server C onfg Route C onfig 46

Server Config Route Config 47

48 Go Support Auto TLS Integrate Let's Encrypt

A utocert package in Go 49

Write one line code to support renew Let’s Encrypt automatically https://goo.gl/ pfeC5Q 50

Android Config iOS Config Log C onfig 51

Send Android N otification API Key From Request 52

iOS Support .p12, . pem or .p8 53 Supports new Apple Token Based Authentication (JWT)

Send iOS N otification 54

Success or fail records 55

memory, blotdb, buntdb, leveldb, redis Storage 56

Interface 57

I nit Interface 58

p ackage flg Send Notification F rom CLI 59

Flag in Go 60

F lag doesn’t support Global variable Docker Container 61

Configuration Management 62

$ gorush -android – m= " message " -k ="API Key" – t= " token " Sned Android 63

$ gorush -ios -m="message" - i ="certificate path" - t="token" -topic="topic" S end iO S 64

write once run anywhere Cross Platform 65

https://github.com/mitchellh/ gox Simple Go Cross Compilation 66

67

https://github.com/facebookgo/ grace Graceful restart & zero downtime 68

How to resolve this problem? Facebook Grace doesn’t Support Windows 69

// +build windows https://golang.org/pkg/go/build / Go build Flag 70

Build Binary into Docker Container 71

Docker Multi-Stage Build 72

Build small image using docker multi-stage $ docker build -t appleboy/ gorush . https://goo.gl/ zQbhmu 73

Auto testing and deploy in GO 74

Drone 75

Continuous Delivery system built on container technology What is Drone? 76

How to setup deploy process? 77 Using . drone.yml file

How to build Go binary in Drone 78

Testing Code Analytic Code Code Quality Build Binary Deploy Binary Build Docker Image Deploy Docker Container Send Notification Pipeline Process in Go 79

How to build Go binary in Drone 80

First Step: Git Clone 81

How to build Go binary in Drone 82

Choose Image Testing Process 83

Co de Quality in Go ( golint ) 84

Check comment typos ( Misspell ) 85

Check code format in Go ( gofmt ) 86

Check vendor folder ( govendor ) 87

Testing in Go $ coverage all https://github.com/appleboy/golang- testing 88

How to build Go binary in Drone 89

Choose Image Step Name Run on for Git tag event 90

Build binary for Cross Platform ( gox ) 91

Deploy to GitHub Release Page 92

How to build Go binary in Drone 93

Drone Envars Drone Secrets Plugin Image 94

How to build Go binary in Drone 95

Plugin Image Push Notification From FB Bot 96

Happy Coding 97

Run Gorush in Kubernets 98

99 Container Health Check

100 Global Configuration Map

101

$ kubectl create -f k8s $ kubectl get services $ minikube service frontend Three Steps 102

Interesting In Drone http://bit.ly/devops-drone 103

Thanks All coming. Any Problem? 104