Archive

Archive for August, 2017

Building Netflix style microservices in .NET – Getting Started

August 15, 2017 Leave a comment

This is the first post of what will be a series of blog posts on Netflix style microservices in c# with .NET Core. The series is not meant as the ultimate guide for developing microservices in c#, but it is rather the summary of my personal experiences. I will describe how to setup an environment based on the Netflix microservice stack, how to develop a .NET Core Web API integrating with this stack, how to setup an API gateway, etc.

Introduction on microservices

Microservices are an architectural pattern that is becoming more and more popular. The pattern originated in web born companies such as Netflix, Spotify, Uber, Google,…. Luckily for us these companies shared their knowledge and experiences at various conferences with the rest of the world; resulting in the current rapid adaption of the microservice pattern. If you want to know more about microservices you can find some interesting videos recorded at tech conferences on YouTube from people working for Netflix, Spotify, etc.

Microservices are not the silver bullet that are going to make the development of every application smoother. They are probably even going to make the development of smaller applications more complex. But for building complex applications or the development within larger organizations, they will make the different components more decoupled and will bring more autonomy to the different teams. Resulting in more agility and a better time to market for each of the independent teams. If your organization is in a situation where software releases are complex and require the coordination of lots of people and components, then I recommend you to have a look at the microservices architectural pattern.

Martin fowler has a nice high-level overview on microservices: https://www.youtube.com/watch?v=2yko4TbC8cI

What is important to understand about microservices, is that microservices is not about very small (REST) services, but rather about how to decouple a large application into smaller components, each responsible for a specific bounded context. The responsibility over these different bounded contexts is then given to different teams. Communication between the different components is going over formal interfaces allowing the different teams to work in full autonomy. You could describe the microservice pattern as SOA done right 😉

An important component of a microservice architecture is the service registry. This registry is used by the various microservices for looking up the address and port of a specific microservice they which to call. Every microservice instance registers itself to the service registry at startup and will periodically send heartbeats. A popular service registry is Eureka of Netflix, but other implementations such as Consul exist too.

In my personal opinion microservices are not a revolutionary new pattern, but is rather building further upon insights gathered from:

  • SOA
  • DevOps
  • DDD
  • Test Automation

Preparing the development environment

In order to setup a local development environment, you will need Windows 10 Pro x64 (for Hyper-V) and Visual Studio 2017.

Docker

The first component we need to install is Docker. Docker has a version for Windows that is fully compatible with all the Linux containers available online. Behind the scenes it is making use of a Linux kernel that is running on Hyper-V. You can find Docker for Windows at: https://www.docker.com/docker-windows

Eureka

Eureka is the service registry developed by Netflix (https://github.com/Netflix/eureka). For simplicity, I wanted to host this component within a fully self-contained Docker container. There exist a Docker build file for Eureka created by Netflix. But unfortunately this was a kind of garage project and the build file is outdated (is was using a very old Eureka version). So I decided to create my own Docker build file.

I have based it on Tomcat Alpine. You can find the build file and the necessary configuration files at my GitHub: https://github.com/pieterderycke/docker-eureka

FROM tomcat:8.5.15-jre8-alpine
MAINTAINER Pieter De Rycke <phdrycke@gmail.com>

RUN rm -fr /usr/local/tomcat/webapps/ROOT &&\
mkdir /usr/local/tomcat/webapps/ROOT && \
cd /usr/local/tomcat/webapps/ROOT/ && \
wget -q http://repo1.maven.org/maven2/com/netflix/eureka/eureka-server/1.7.0/eureka-server-1.7.0.war &&\
unzip eureka-server-1.7.0.war &&\
rm eureka-server-1.7.0.war

COPY config.properties /usr/local/tomcat/webapps/ROOT/config.properties
COPY eureka-client-test.properties /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/eureka-client-test.properties
COPY eureka-server-test.properties /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/eureka-server-test.properties

EXPOSE 8080

ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh"]

CMD ["run"]

If you do a check-out from my GitHub, you can build the image with the following command:

docker build --tag eureka .

Microservice Environment

During the rest of the blog series, I will make use of a Docker environment consisting of a dedicated Docker network and a number of containers running within this network.

The network is a simple Docker bridge network:

docker network create --driver bridge netflix-network

Next we create a Docker container from the Eureka image we just created:

docker run -p 8080:8080 --name=eureka --network="netflix-network" --network-alias="eureka" eureka

We check with a browser if Eureka started successfully by going to http://localhost:8080:

2017-08-18 00_07_29-Eureka

Wrap Up

This was the first blog in what will be a serie on microservice development with C# and .NET Core. This first article should already provide you a high level overview on microservices and you should now also have a development environment running locally for developing microservices. In the next article we are going to create a first Web API Core microservice running in a Docker container and connecting to Eureka.

Advertisements
Categories: C#, microservices