0% found this document useful (0 votes)
6 views

golang-microservices-20171109

The document discusses the advantages of using the Go programming language for developing microservices, particularly in conjunction with Docker Swarm mode and Spring Cloud. It highlights Go's efficiency, low memory usage, and rapid build times compared to JVM-based solutions, making it suitable for scalable microservice architectures. The presentation also covers practical implementation considerations, including centralized configuration, logging, and distributed tracing, along with various demos showcasing these features.

Uploaded by

Ouedraogo Jean
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

golang-microservices-20171109

The document discusses the advantages of using the Go programming language for developing microservices, particularly in conjunction with Docker Swarm mode and Spring Cloud. It highlights Go's efficiency, low memory usage, and rapid build times compared to JVM-based solutions, making it suitable for scalable microservice architectures. The presentation also covers practical implementation considerations, including centralized configuration, logging, and distributed tracing, along with various demos showcasing these features.

Uploaded by

Ouedraogo Jean
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

LEAN & MEAN - GO MICROSERVICES WITH

DOCKER SWARM MODE AND SPRING CLOUD

ERIK LUPANDER
2017-11-09 | CALLISTAENTERPRISE.SE
ABOUT ME

• Erik Lupander, consultant at Callista Enterprise.


• Primarily a Java dude.
• ”Discovered” Go about 2 years ago.

2
Love at first sight!

3
ON THE AGENDA…

• Background: The footprint problem.


• The Go programming language.
• Go in the context of:
• Microservices
• Spring Cloud/Netflix OSS
• Docker Swarm mode.
• Demos!

4
THE FOOTPRINT PROBLEM

Can Go help us help us reduce the


footprint of a microservice?

5
THE FOOTPRINT PROBLEM

• JVM-based solutions comes with a hefty footprint.


• If you need to run tens or even hundreds of microservice
instances, cost is definitely a factor.
• t2.micro (1GB) —> t2.small (2GB) doubles the cost / h.
• There are obviously many other alternatives for microservice
development….
• Very interesting topic… if we had all day.

6
THE GO LANGUAGE

The Go Language

7
THE GO LANGUAGE

It has been stated that the reason the three authors


created Go was their…

8
THE GO LANGUAGE

”… shared dislike of C++'s complexity as a


primary motivation for designing a new language”

9
THE GO LANGUAGE

Go was designed …

10
THE GO LANGUAGE

”… to eliminate the slowness and clumsiness of


software development at Google”

Go official FAQ

11
WHAT WAS IMPROVED WITH GO?

• ~50x build time improvement over C++


• Internal C++ application builds taking 30-75 minutes.
• Better dependency management
• Cross-platform builds
• Language level concurrency
• Readable and maintainable code
• Even for non superstar developers

12
THE GO LANGUAGE

• Claims to be
• efficient, scalable and productive.
• Designed
• to improve the working environment for its designers and
their coworkers.
• Is not
• a research language.

13
THE GO LANGUAGE

• Go is
• compiled, statically typed, concurrent, garbage-collected
• Has
• structs, pointers, interfaces, closures
• But does not have
• classes, inheritance, generics, operator overloading, pointer
arithmetic

14
WHY GOLANG - DEVELOPING

What does actual developers think about Go?

15
”… a disservice to intelligent programmers”
Gary Willoughby - blogger

16
”… stuck in the 70’s”
Dan Given

17
”… psuedointellectual arrogance of Rob Pike
and everything he stands for”
Keith Wesolowski

18
THE GO LANGUAGE

But also

19
”I like a lot of the design decisions they made in the [Go] language.
Basically, I like all of them.”
Martin Odersky, creator of Scala

20
”Never used a language before that empowers you to solve problems
as quick as Go does”
Alexander Orlov @ Twitter

21
”Go isn’t a very good language in theory, but it’s a great language in
practice, and practice is all I care about”
anonymous hackernews poster

22
THE GO LANGUAGE

Some pros and cons

23
DEVELOPMENT IN GOLANG - PROS

• Easy to learn, readable, productive and pretty powerful.


• The built-in concurrency is awesome.
• Cross-platform.
• Rich standard APIs and vibrant open source community.
• Quick turnaround and decent IDE support (getting better!)
• Nice bundled tools.
• Built-in unit testing, profiling, coverage, benchmarking,
formatting, code quality…
• Strongly opinionated.
• Code formatting, compile errors on typical warnings.

24
DEVELOPING IN GOLANG - SOME CONS

• Missing generics
• Dependency versioning
• Verbose syntax
• Error checking, no autoboxing of primitive types etc.
• Unit testing and Mocking isn’t very intuitive.

25
WHO USES GOLANG

• Some well-known software built entirely in golang


• Docker
• Kubernetes
• etcd
• influxdb (time series database)
• cockroachdb (spanner-like database)

26
GOLANG - SYNTAX IN 2-5 MINUTES

Two code samples

27
SAMPLE CODE 1 - HELLO WORLD

28
SAMPLE CODE 2 - CONCURRENCY

29
Go microservices

30
GO MICROSERVICE IMPLEMENTATION - CONSIDERATIONS

• When implementing microservices, we need working, mature


and stable libraries for things such as:
• HTTP / REST / RPC APIs
• Data serializers / deserializers (json, xml etc.)
• Messaging APIs
• Persistence APIs
• Logging
• Testability

31
The demo application

32
Curl Legend
ARCHITECTURAL OVERVIEW • CB = Circuit Breaker (Go Hystrix)
• TA = Correlated tracing (Opentracing API / Zipkin)
HTTPS
Docker Swarm
cluster Edge server
OAuth token relay
(Netflix Zuul)
CB/TA

HTTP

OAuth Security API (Go) Monitor


OAuth Res
Authorization Dashboard
Server (Hystrix Dashboard)
(spring-security)
CB / TA

HTTP
Configuration Hystrix Stream
Server Account Composite (Go) aggregation
(spring-cloud- AMQP (Modified Netflix
config) TA Turbine)
CB / TA
VipService (Go)

AMQP Trace
HTTP
Messaging HTTP Analysis
(RabbitMQ) (Zipkin)

TA

Images (Go) Quotes-Service


(Spring Boot)
WHY GO - RUNTIME CHARACTERISTICS

• Low memory usage


• Typically executes at least as fast as Java
• Fast startup
• Highly concurrent
• Garbage Collector geared for very short GC pauses

34
GO MICROSERVICES - STATICALLY LINKED BINARIES

• Statically linked binary produces an executable without


external dependencies.
• No jar- or dll-hell
• No requirement on the OS having a JRE / CLR / NodeJS or
other libraries
• (except libc)
• Small executable size

35
DOCKER CONTAINERS & STATICALLY LINKED BINARIES

• In the context of Docker Containers, the statically linked binary


allows use of very bare parent images.
• I’m using iron/base which is ~6 mb, alpine is another popular
choice.

FROM iron/base

EXPOSE 6868
ADD vipservice-linux-amd64 /
ADD healthcheck-linux-amd64 /

HEALTHCHECK CMD [”./healthcheck-linux-amd64”, ”-port=6868”]

ENTRYPOINT [”./vipservice-linux-amd64", ”-profile=test”]

36
Demo 1
Footprint @ Docker Swarm Mode

37
”what is hard in Microservices is all the things
around them”
Jonas Bonér - author of Akka

38
Consider:

39
MICROSERVICE CONSIDERATIONS

• Centralized configuration
• Service Discovery
• Centralized Logging
• Distributed Tracing
• Circuit Breaking
• Load balancing
• Edge server / Reverse proxy
• Monitoring
• Security

40
Curl Legend
ARCHITECTURAL OVERVIEW • CB = Circuit Breaker (Go Hystrix)
• TA = Correlated tracing (Opentracing API / Zipkin)
HTTPS
Docker Swarm
cluster Edge server
OAuth token relay
(Netflix Zuul)
CB/TA

HTTP

OAuth Security API (Go) Monitor


OAuth Res
Authorization Dashboard
Server (Hystrix Dashboard)
(spring-security)
CB / TA

HTTP
Configuration Hystrix Stream
Server Account Composite (Go) aggregation
(spring-cloud- AMQP (Modified Netflix
config) TA Turbine)
CB / TA
VipService (Go)

AMQP Trace
HTTP
Messaging HTTP Analysis
(RabbitMQ) (Zipkin)

TA

Images (Go) Quotes-Service


(Spring Boot)
Things not really Go-related…

42
EDGE SERVER

• Our Go services doesn’t care about the EDGE / reverse-proxy


• Netflix Zuul, Nginx, HAProxy …
• Or use solution provided by container orchestrator
• Ingress Routing mesh (Docker Swarm mode)
• Ingress controller (K8S)
• Routes (OpenShift)
• Must forward HTTP headers.
• Security

43
SERVICE DISCOVERY AND LOAD BALANCING

• Load-balancing and Service Discovery is handled by the


orchestration engine.
• E.g. the Docker Swarm or K8S / OpenShift ”service”
abstraction.
• Eureka service discovery and Ribbon-like client-based load-
balancing can be implemented too.

44
Demo 2 -
Load balancing and fast scaling
@ Docker Swarm

45
Go Microservice
Anatomy

TA

46
HTTP / REST FRAMEWORK

HTTP/REST framework
(gorilla)

Configuration Trace
Server Configuration Distributed
Analysis
(spring-cloud- Client Tracing
(Zipkin)
config) (viper) (opentracing-go)

Logger
(logrus)

AMQP Hystrix Stream


Messaging AMQP
Circuit aggregation
client
(RabbitMQ) (steadway/
Breaker (Modified Netflix
(hystrix-go)
amqp) Turbine)

47
GO WITH OUT WITHOUT WEB FRAMEWORKS?

• Consider using the native http packages + a router package over


a full-blown web framework such as gin, echo, beego.

48
HTTP FRAMEWORK (GORILLA)

49
HTTP FRAMEWORK (GORILLA)

50
CENTRALIZED CONFIGURATION

HTTP/REST framework
(gorilla)

Configuration Trace
Server Configuration Distributed
Analysis
(spring-cloud- Client Tracing
(Zipkin)
config) (viper) (opentracing-go)

Logger
(logrus)

AMQP Hystrix Stream


Messaging AMQP
Circuit aggregation
client
(RabbitMQ) (steadway/
Breaker (Modified Netflix
(hystrix-go)
amqp) Turbine)

51
CENTRALIZED CONFIGURATION

• With possibly tens of microservices and hundreds of


instances, centralized and externalized configuration is a
must.
• Configuration providers:
• Config servers
• Spring Cloud Config, etcd …
• Container orchestrator mechanisms
• K8S and OpenShift has ”config maps” and ”secrets” in
order to mount configuration files, certificates etc. into
containers at startup.

52
CONFIGURATION USING SPRING CLOUD CONFIG AND VIPER

Docker Swarm
Microservices

Configuration HTTP VIPER


CB / TA
git HTTPS Server
repository (spring-cloud-config)
HTTP

VIPER
CB / TA

http://configserver:8888/imageservice-test/master

53
CONFIGURATION - VIPER

• Viper supports YAML, .properties, JSON and Env-vars


• With a few lines of code, we can load and inject config from
Spring Cloud Config into Viper

54
CONFIGURATION - VIPER USAGE

55
CONFIGURATION PUSH USING SPRING CLOUD CONFIG AND VIPER

<config change commit


pushed to repo>
Docker Swarm
Microservices

Configuration
git HTTP POST commit hook
Server
repository (spring-cloud-config) VIPER
CB / TA VIPER

<Refresh token> CB / TA

<Refresh token>
RabbitMQ

56
Demo 3 -
Configuration Push

57
CENTRALIZED LOGGING

HTTP/REST framework
(gorilla)

Configuration Trace
Server Configuration Distributed
Analysis
(spring-cloud- Client Tracing
(Zipkin)
config) (viper) (opentracing-go)

Logger
(logrus)

AMQP Hystrix Stream


Messaging AMQP
Circuit aggregation
client
(RabbitMQ) (steadway/
Breaker (Modified Netflix
(hystrix-go)
amqp) Turbine)

58
LOGGING - LOGRUS

• Applications needs structured logging


• slf4j, log4j, logback…
• Logrus is a similar API for Go
• Supports levels, fields, formatters, hooks

59
LOGRUS

60
CENTRALIZING LOGS

• In a Docker context, we configure a logging driver when


declaring our ”service”.
• The logging driver adds lots of nice container metadata.
• Logs are sent to an aggregation service (typically something
like logstash)
• The log aggregation service may perform some filtering,
transforming etc. before storing logs to a storage backend or
sending them to a LaaS provider.

61
LOGGING WITH CONTAINER METADATA (GELF)

{
"version":"1.1",
"host":"swarm-manager-0",
"short_message":{
”level”:"info",
"msg":"Successfully initialized service”,
”time":"2017-07-17T16:03:35+02:00"
},
"timestamp":1.487625824614e+09,
"level":6,
"_command":"./vipservice-linux-amd64 -profile=test",
”_container_id”:”894edfe2faed131d417eebf77306a0386b430….",
"_container_name":"vipservice.1.jgaludcy21iriskcu1fx9nx2p",
"_created":"2017-02-20T21:23:38.877748337Z",
”_image_id”:”sha256:1df84e91e0931ec14c6fb4e55…..”,
"_image_name":"someprefix/vipservice:latest",
"_tag":"894edfe2faed"
}
62
63
DISTRIBUTED TRACING

HTTP/REST framework
(gorilla)

Configuration Trace
Server Configuration Distributed
Analysis
(spring-cloud- Client Tracing
(Zipkin)
config) (viper) (opentracing-go)

Logger
(logrus)

AMQP Hystrix Stream


Messaging AMQP
Circuit aggregation
client
(RabbitMQ) (steadway/
Breaker (Modified Netflix
(hystrix-go)
amqp) Turbine)

64
DISTRIBUTED TRACING

• Track a request over multiple microservices


• Also trace within services and methods
• Invaluable for high-level profiling across the service stack.
• Facilitated by go-opentracing and zipkin

65
GO-OPENTRACING CODE SAMPLE

66
DISTRIBUTED TRACING

67
Demo 4 -
Distributed Tracing with Zipkin

68
CIRCUIT BREAKER

HTTP/REST framework
(gorilla)

Configuration Trace
Server Configuration Distributed
Analysis
(spring-cloud- Client Tracing
(Zipkin)
config) (viper) (opentracing-go)

Logger
(logrus)

AMQP Hystrix Stream


Messaging AMQP
Circuit aggregation
client
(RabbitMQ) (steadway/
Breaker (Modified Netflix
(hystrix-go)
amqp) Turbine)

69
CIRCUIT BREAKING - HYSTRIX

• Mechanism to make sure a single malfunctioning microservice


doesn’t halt the entire service or application.
• go-hystrix (circuit breaker)
• Netflix Turbine (aggregation)
• Netflix Hystrix Dashboard (GUI)

70
CIRCUIT BREAKING

• Programmatic hystrix configuration

71
CIRCUIT BREAKING

• Example go-hystrix usage, non-blocking.

72
CIRCUIT BREAKING

• Hystrix stream aggregation using customized Netflix Turbine


Go Services

:8181/hystrix.stream

CB / TA

Hystrix Stream Monitor


:8181/hystrix.stream aggregation :8282/turbine.stream Dashboard
(Modified Netflix (Hystrix Dashboard)
CB / TA Turbine)

:8181/hystrix.stream
CB / TA

Client Discovery token

Client Discovery token

RabbitMQ

73
Demo 5 -
Hystrix Dashboard

74
DISTRIBUTED TRACING

75
SUMMARY

• Go is an interesting option for microservices due to runtime


characteristics and rather pleasant developing.
• Although but not without it’s fair share of quirks especially
regarding the lack of traditional OO constructs and missing
generics.
• Microservice development in Go requires a bit of work
regarding integration with supporting services, but can be
mitigated by using integration libraries such as go-kit or our
own little toolkit.
• Don’t be afraid to pick your favorite libraries!

76
WANT TO LEARN MORE?

• Nic Jackson
• July 2017 from Packt
• Technical reviewers:
• Magnus Larsson
• Erik Lupander

77
DVIZZ - A DOCKER SWARM VISUALIZER

• https://github.com/eriklupander/dvizz
• Pull requests are more than welcome!

78
RESOURCES

• My 12-part blog series: http://callistaenterprise.se/blogg/


teknik/2017/02/17/go-blog-series-part1/
• Demo landscape source code: https://github.com/
callistaenterprise/goblog
• Branch ”nov2017”
• Spring Cloud Netflix: https://cloud.spring.io/spring-cloud-
netflix/
• go-kit: https://github.com/go-kit/kit
• dvizz: https://github.com/eriklupander/dvizz
• packt book: https://www.packtpub.com/application-
development/building-microservices-go

79
Questions?
80

You might also like