My first impression about ISTIO and Service Mesh

Today I will talk about ISTIO and Service Mesh topics. I’m on a learning path these concepts to apply in my job and help some other interesting people on a software architecture and development.

I’m so excited to learn it because I think this maybe change our way to develop microservices, specifically how we can use infrastructure to get more insights about our runtime applications a.k.a metrics, health and routing.

This post intends to be a simple explanation about some Istio features and some comparisons with other tools provided by amazing Netflix OSS components.

Intro

I’m a java developer and most of part of my job time I’ve been created solutions with the Java frameworks and libraries. The Spring Framework is an amazing framework to create web applications, also has a brilliant and vibrant ecosystem.

Also, the Spring Cloud Projects provides interesting solutions to helps us, developers, to build distributed applications.

On this post, we will use the Kubernetes as container orchestrator.

I had some experiences coding with Spring Cloud Netflix Projects which integrates the Spring ecosystem with Netflix OSS tools, like Netflix Ribbon, Netflix Hystrix, Netflix Eureka and these projects is really amazing, and makes our developer’s infrastructures tasks so easy, with a couple annotations and some configurations the projects have interesting features like Service Discovery, Client Side Load Balancer and Circuit Breaker.

This kind of application, which uses the Netflix OSS tools, usually handle the network layer inside the application layer. For instance, the circuit breaker feature provided by Hystrix needs to be configured in the application layer. We can see the retrieves calls are handled by our component (application) because we have the Hystrix inside. In the same way, the Ribbon works we need to have in our classpath.

It works generally well but imagines the following situation. Remember the Netflix OSS only works for Java stacks, it uses the Java ecosystem to achieve these features. But now we need to change the application because the application needs to handle more load with minimal resource usage. The Golang fits well in this case.

The microservices advent can help developers to create small and independents application.

We can’t lose these important features in the microservices architectural style.

Because of this situation, the Service Mesh Pattern has been gaining some notation in the development world. Using this pattern we can isolate the Network stack in another container (in the same POD), it is called sidecar container, it will be responsible to handle all network calls independently of the application language has been built.

In this context, Istio as an implementation of service-mesh gives for us some interesting features like Intelligent Routing, Circuit Breaker, and Fault Injection outside of our application, with any code more to achieve these features. It is amazing.

Installation

I tried different approaches to install Istio in my cluster, actually, I’m using the Google Kubernetes Engine (GKE) to learn and try Istio features.

I decided to use the Helm installation which one proved easier and fast way to install/delete Istio in my kubernetes cluster. I followed the Istio installation with Helm, you can find the instruction at istio installation guide.

The Helm installation disables the Mutual TLS authentication by default, of course, you can enable using the Helm command line flags.

If you using the Istio 0.8.0 version, the installation automatically spins up a pod which one provides automatic sidecar containers in our PODs, it can make our lives easier because we can mark a namespace to enable sidecar automatically. I used this feature and works very well.

I extremely recommend you to install “infra” services, the Istio refers to this as “add-ons” like Grafana, Prometheus, Service Graph and Jaeger. These add-ons help us to see the cluster metrics, which Istio collects and stores in some these services.

That is all to enable us to play with Istio!!

Experience

I’ve created a couple of applications, there are no complex business rules. The idea here is to try the service-to-service communications and get some metrics about these communications. And then I deployed these applications in the Kubernetes cluster, the important thing here is I’ve created the standard Deployments in Kubernetes.

These deployments have a Services which one’s exports the ports, nothing special here. The crucial thing is the Service needs to use the metadata section with the labels called “app”  if you forget this the Istio will not work as expected.

Look at the following example:

Pay attention to the metadata section.

To Istio work as expected we need to follow some requirements, these requirements can be found here.

After the deployments worked. I decided to try some requests for my services. And like a magic without any configuration, my Grafana instances started to gathering some services metrics. I’m got impressed at this moment because the how fast it is and with a simple yaml configuration I had an almost complete and interesting metrics.

Look at Grafana instance, at this moment I didn’t create any graphics or configuration and the graphics show my whole applications ecosystem.

 

Also there other interesting services like Jaeger which one allows us to collect tracing between service communications and measure the time of service calls.

Prometheus which one will store our metrics in the time series database, it is a kind of Backend for Grafana. The Prometheus basically will collect the service metrics and store it.

Service Graph which one show our services dependencies in real-time. It is an awesome app.

Conclusion

I’ve started to play a few weeks ago with Istio (Service Mesh) and I think these tools will helps developers and devops guys in different ways. To get insight into the infrastructure, maybe the applications metrics usages. Another important characteristics for me is how Istio collect the application metrics. It is not intrusive and it makes easier to development lifecycle.

Also, these tools can help the companies to innovate faster than ever because it offers awesome implementations like Canaries Deployments, the companies can try your deployments without downtime and with a reasonable safety.

I’m excited to study more about it, in the next weeks I will discover more features and I will share on this blog.

If you can help me, on the journey to learn service mesh please share interesting articles about the topic.

Thank you

References

 

Spring Boot 2 Meets Kotlin

Hello Guys.

Today we will talk about the new feature added in Spring 5.0 and Spring Boot 2. We will understand the Kotlin support for Spring Boot Applications.

Kotlin is a new language created by JetBrains Team. The language is JVM language. It means the language creates a bytecode to run on Java Virtual Machine.

As we can see, the primary inspiration is Scala language. There are many constructions similar in both languages, the data classes concepts for instance.

There are some interesting advantages when we adopt Kotlin to code. The most exciting is reduce boilerplate code and make our code more concise, it brings more maintainability and transforms our code more readable.

We will understand these topics on next examples, then is Time to Code!!!

Create the Tasks Project with Spring Initializr

We will create a simple project to manage Tasks. The main idea here is to explain some Kotlin interesting points on this project.

Let’s go to Spring Initiliazr page. The project should be created with these configurations below:

The interesting points here are:

  • Maven Project
  • Kotlin Language
  • Spring Boot 2
  • Dependencies: Reactive Web, Actuator and Reactive MongoDB

Creating our TaskEntity

We need to create our main entity. The entity is pretty simple and so easy to implement the Task class should be like this:

As we can see, there are some Kotlin interesting points here. There is a data class keyword it means the class has the purpose of holding data. Kotlin will add some important behaviors automatically like:

  • equals() and hashCode()
  • toString()
  • copy()

there are some restrictions the data classes cannot be abstract, open, sealed or inner.

The full data classes documentation can be found here.

Creating the Reactive Task Repository

Now, we will use the Spring Data MongoDB Reactive implementation. The behaviors are similar in the blocking version, but it will not blocking because is the reactive version. The way to thinking is similar there is DSL to use objects properties to create queries automatically.

The TaskRepository should be like this:

The keyword interface is the same in Java language, but the way to extends is Kotlin is slightly different, in Kotlin we will use “:” instead of extends.

Creating the TaskService

Let’s create our TaskService class it will invoke the repository implementations. The code of TaskService should be like this:

There are a couple of interesting things here. Let’s start by injection, there is no necessity to use @Autowired in class constructor since Spring Framework 4 version, as we can see it will work as expected here as well. We use val in favor immutability.

Let’s understand the tasks() function. The function has no body because of the implementation has one line only. In this case, the function return can be omitted as well. It makes the code more concise and easy to understand.

We have used the same features in our other functions.

The full documentation about functions can be found here.

The REST Layer

Our REST Layer should be reactive. Then we need to return Flux or Mono in our methods. We will use the one line function and we can omit these declarations, keep in mind to achieve reactive functionalities we need Flux or Mono in our methods.

The TaskResource class should be like this:

As we can see we are using the same as we did before, we have not used return methods declaration, the compiler can infer it for us, also it prevents developers errors as well.

We are using the @GetMapping and @PostMapping instead of @RequestMapping annotations, it makes our code more readable.

Configuring the MongoDB connections

We will use the yaml file, it makes our configuration file more readable and introduces semantics in our file. The configuration file should be like this:

There is nothing special here, a couple of configurations for mongoDB and tomcat server.

The Final Project Structure

Let’s analyze the final project structure, you can put in your preferred structure. I suggest the following one:

 

Excellent, now we can run it.

Run it and try your pretty new API using Kotlin Language!!!

Awesome Job, well done!!!

The full source code can be found here.

Tip

I recommend docker to run a mongoDB instance it makes your life extremely easy.

Book

You can find more detailed implementations and different Use Case in my book ( Spring 5.o By Example), published by Packt.

 

Thank you, for next Post, I will write about Spring Cloud Gateway and how it can help developers to work with Routes.

BYE.

Continuous Query With Spring Data Reactive MongoDB

Continuous Query With Spring Data Reactive MongoDB

On this blog post, we will take a look how to implement Continuous Queries in MongoDB. Also, we will have a dash of Spring WebFlux in Action.

Let’s do it, right now!!!

What is Continuous Query???

Is a kind of active query, when the data were arriving in the database, if this piece of data matches accordingly with our query an event will be emitted to our application, with the piece of data. We can think a kind of Event-Driven Programming using the database as an event trigger.

It is a powerful feature and enable us to add interactive behaviors in our application.

Capped Collections and Tailable Cursors

In MongoDB, there is a feature called Capped Collections. This kind of collection has a fixed size and support high-throughput operations and retrieve the documents based on insert order. We will use this kind of collection to store our data to simulate the “continuous query.”

Also, MongoDB has an exciting feature called Tailable Cursors which we are able to use on Capped Collections. This cursor is similar to

in Unix command system, which means we will retrieve the documents in natural order.

Spring Data Reactive MongoDB

Now we know how Capped Collections and Tailable Cursor works. Then is time to use Spring Data Reactive Mongo DB stuff.

Use Case

We will simulate a kind of data collector for IoT components. The main feature of our project is collect components data and provides an API to show max and min temperatures when the temperature reaches the limits configured for a specific device. Then the Frontend Team can build a fantastic reactive page to display the device information.

Our API will have five principal operations:

  • Create Device
  • Input Temperature
  • Device Temperature Stream
  • Device Min Temperature Stream
  • Device Max Temperature Stream

Let’s start with our primary Entity Temperature, the temperature class should be:

As we can see there is nothing special, we have used the Lombok Annotations to remove boilerplate code.

We have the Temperature class created, then we can create our TemperatureRepository, the interface can be declared like this:

Awesome, there is some interesting stuff here. The first one is the @Tailable annotation, which instructs the Spring Frameworks to await the MongoDB Tailable Cursors. When the data arrives in MongoDb with de data matches with a specific query one event will be emitted. In this case, we will watch events from a specific device. Take a look at our return type we always should use the Flux<T> to make application reactive.

Time to create our service class, the implementation should be like this:

On our method init(), we have declared a capped collection. First, we have removed the collection created and then reactively we have created and configured the temperatures collection. Remember this method will call one time. The others methods are so simple the main idea here is to call the Repository Layer to retrieve the documents. The important part here is the return types of methods, always Flux<T> when we expect the events.

In our RestController we will use the Server-Sent Event (SSE), to add more interactive behaviors for our API. The main idea here is to push data to the client using the Half-Duplex HTTP connection.

Our DeviceResource should be like this:

As we can see take a look at the methods declarations we have used the MediaType.APPLICATION_STREAM_JSON_VALUE. It is much important to add Stream Behavior for our application.

Simple like that we have created a couple of classes and lines, to add a powerful feature like “continuous query” in our application. Spring Data Reactive reactive brings reactive characteristics for our application and make them more resilient and cost-effective.

The Full source code can be found at GitHub. There is a client to add some data and make the test easier, the client will send data to interval pre-configured.

The Spring 5.0 By Example Book

Recently I launched the Spring 5.0 By Example Book. This book contains a lot of Spring Concepts and Examples, using the Microservices Architecture and much codes using the Spring Boot 2.

The key features of the book are:

  • Learn reactive programming by implementing a reactive application with Spring Webflux
  • Create a robust and scalable messaging application with Spring messaging support
  • Apply your knowledge to build three real-world projects in Spring

You can find the book on Amazon.

Requirements:

MongoDb instance up and running. I recommend Docker to spin up a MongoDB container.

The MongoDb should be listening at “localhost”, otherwise you can configure using the application.yaml or application.properties.

References:

https://docs.mongodb.com/manual/core/capped-collections/

https://docs.mongodb.com/manual/core/tailable-cursors/

https://en.wikipedia.org/wiki/Server-sent_events

 

See you. BYE.

Introduce the Spring WebFlux – A Practical Guide

Hello guys,

In my first blog post, I mean on this new address I choose the hot topic in java programming, the Spring 5, recently Pivotal had launched the new version of the framework and promoted this version to GA, which means this version is production ready, you can find the full list of features here.

The highlights for me are the full compatibility with Java 9, the kotlin support and finally the most commented feature, the new module called Spring WebFlux which one was built on a reactive foundation.

For this post, I will explain about the Spring WebFlux, which one can help us to write a fully non-blocking application based on event loop concepts.

Before coding, I would like to explain how event-loop works and the difference between this model and a traditional large thread pool with thread-per-request execution model…let’s go.

Event-loop Model

Popularized by nodejs, this style allows us to scale with a small number of threads instead of the thread-pool, the concept is quite simple the request arrives at event loop then it blocking on resource-emitting events and dispatches them to corresponding handlers and callbacks.

Keep in mind one important thing, you should never block the execution otherwise the application will be blocked.

This figure can help us to understand the concept

 

Some considerations before code

The Spring Framework Team chosen the Reactor as a Reactive Streams implementation is a quite similar to RxJava

The most important classes are Mono, which one represents 0 | 1 sequence and Flux which represents 0 to N elements.

Let’s do something amazing…

My choice as a datastore was Cassandra, is a popular noSQL database because Spring has a Reactive Repository for this database. Let’s do the reactive Repository

This is a simple repository, pay attention in one thing you should extend the Reactive version of Cassandra repository.

Let’s go to the service layer, this layer is like we did in before versions of Spring Framework, the only difference is, you should return Mono or Flux in your methods.

The interesting thing here is the @Autowired annotation isn’ t necessary anymore, Spring detects it.

And finally, we will create a REST layer for our applications, this is a quite similar to the last version before 5. Remember we are using the Reactor Netty as a web server this makes our application reactive.

@RequestMapping, @GetMapping, @PostMapping is unchanged, please pay attention to the return types, this needs to be changed to Mono Or Flux.

Conclusions

Spring renews your portfolio one more time, bringing concepts of reactive programming to your projects, you can use your previous knowledge about Spring to create amazing Reactive Applications.

Advice: Keep in mind one thing, the concept of reactive programming uses the declarative programming and this is totally different from imperative programming.

You can find the full code on my GitHub

On the next post, I will explain the Kotlin support on Spring 5, see you there. BYE.