{"id":2245,"date":"2018-08-20T23:38:21","date_gmt":"2018-08-20T21:38:21","guid":{"rendered":"https:\/\/craftcoders.app\/?p=538"},"modified":"2024-08-14T14:27:54","modified_gmt":"2024-08-14T12:27:54","slug":"spring-cloud-netflix-sidecar-tutorial","status":"publish","type":"post","link":"https:\/\/craftcoders.app\/spring-cloud-netflix-sidecar-tutorial\/","title":{"rendered":"Spring Cloud Netflix Sidecar Tutorial"},"content":{"rendered":"\r\n
Hey guys, If you don’t know what Microservices are, please read the excellent blog post<\/a> from Martin Fowler regarding Microservices. Basically every book, article or scientific paper (and my bachelor thesis) about this architectural style is based on this blog post, so yeah, it’s a pretty good read. I am currently in the process of writing my bachelor thesis and therefore implemented a prototypical application using the Microservice architectural style. Because I’m a Java guy and know the Spring framework, I decided to implement the application using the Spring Cloud ecosystem. I use an Eureka server as a service registry. Furthermore, I implemented several Spring Boot services and was able to register them to Eureka with the use of an annotation and a little bit of configuration. It turned out that I had to implement one of the services making up my application with PHP (yikes!) because a library I had to use is not available in Java. Because I only had two weeks for the implementation of my prototype I certainly wouldn’t have been able to write a Java implementation of the library. Therefore I decided to create a PHP microservice with the help of\u00a0Lumen<\/a>.\u00a0 Furthermore I didn’t want to miss out on the fancy features of my service registry like client-side load-balancing and the decoupling of my service providers from my consumers. After a bit of research I found the documentation of the Eureka HTTP API<\/a>. I got discouraged at the sight of the XSD I had to implement in my PHP service to register it with Eureka. I really did not want to implement the various REST operations manually into my service because my PHP knowledge is very limited and I have never used Lumen before. I was on the verge of giving up when I found Spring Cloud Netflix Sidecar. It promised to let me register my service written in PHP with Eureka using one annotation and a little configuration, just like in my other services written with Spring boot. Spring Cloud Netflix Sidecar is a subproject of Spring Cloud Netflix<\/a>\u00a0and is inspired by the Netflix Prana<\/a> project. A sidecar service is basically yet another Spring boot application that runs on the same host as your non-JVM service. It registers itself to your service registry with a defined application name and frequently checks the health of your non-JVM service via a REST call. The sidecar is also able to forward calls from other services of your application. By using a sidecar application you only have to implement minimal changes to your non-JVM application, so it’s also great for legacy projects. For you to get the hang of Spring Cloud Netflix Sidecar I created a very minimalistic project consisting of a Eureka server, a Lumen service capable of doing nothing and the corresponding sidecar application. You have to have docker and docker-compose installed to run this example. In order to run the example application, clone the project from our GitHub repository<\/a>. After that change into its directory and type Sadly, sidecar isn’t available in Spring Initializr, so you have to manually add following maven dependency to your Spring Boot application:<\/p>\r\n\r\n\r\n\r\n After adding the dependency you can annotate your main class with the We have to tell the sidecar application on which port to run and what it’s name is. Note that the The important configuration are the<\/p>\r\n\r\n\r\n\r\n The configured health check REST endpoint should return a JSON document looking like this:<\/p>\r\n\r\n\r\n\r\n And that’s all you have to change in your non-JVM application to get all the advantages of the Spring Cloud ecosystem. In this post I showed you how to include a non-JVM application in your Spring Cloud ecosystem. This can be done by creating a Spring boot application with the sidecar dependency and some configuration and adding a simple health check to your service. I hope I was able to help some people with this post! Best regards Leon<\/p>\r\n","protected":false},"excerpt":{"rendered":" Introduction Hey guys,this week’s post is about Microservices created with the various Spring Cloud frameworks and how to include services written in non-JVM programming languages into the Spring Cloud ecosystem using Spring Cloud Netflix Sidecar. Please be aware that this tutorial is specifically written for people who know the architectural style of Microservices and are creating applications using the various … Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":2346,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[108,147,149,109],"tags":[],"acf":[],"yoast_head":"\n
this week’s post is about Microservices created with the various Spring Cloud<\/a> frameworks and how to include services written in non-JVM programming languages into the Spring Cloud ecosystem using Spring Cloud Netflix Sidecar<\/a>. Please be aware that this tutorial is specifically written for people who know the architectural style of Microservices and are creating applications using the various Spring Cloud frameworks or plan to do so.
<\/p>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\nThe problem I faced<\/h2>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\nSpring Cloud Netflix Sidecar<\/h2>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\nA working example<\/h2>\r\n\r\n\r\n\r\n
docker-compose up -d<\/code> into your console. This command pulls all necessary images from our DockerHub registries and starts the containers. After everything has started, you can access http:\/\/localhost:8761\/, which is the Eureka dashboard, and see the lumen service registered.
You can stop the container containing the lumen service by typing docker stop *lumen-service*<\/code> and the status of the application on your Eureka dashboard should change to DOWN a few seconds later. That is because the sidecar application’s heartbeats are not answered by your lumen service, obviously.
<\/p>\r\n\r\n\r\n\r\nHow to set this up<\/h2>\r\n\r\n\r\n\r\n
<dependency>\r\n\u00a0 \u00a0 <groupId>org.springframework.cloud<\/groupId>\r\n\u00a0 \u00a0 <artifactId>spring-cloud-netflix-sidecar<\/artifactId>\r\n<\/dependency><\/code><\/pre>\r\n\r\n\r\n\r\n
@EnableSidecar<\/code>annotation.
It would not be a proper Spring application if you wouldn’t have to create an application.yml and add all the necessary configuration, so let’s do this.<\/p>\r\n\r\n\r\n\r\nserver:\r\n port: 5678\r\n\r\nspring:\r\n application:\r\n name: lumen-service\r\n\r\neureka:\r\n client:\r\n serviceUrl:\r\n defaultZone: ${EUREKA_URI:http:\/\/localhost:8761\/eureka}\r\n instance:\r\n preferIpAddress: true\r\n\r\nsidecar:\r\n port: ${SIDECAR_PORT:8000}\r\n health-uri: ${SIDECAR_HEALTH_URI:http:\/\/localhost:8000\/health}<\/code><\/pre>\r\n\r\n\r\n\r\n
spring.application.name<\/code> property is the one getting displayed on your Eureka dashboard. Furthermore we have to tell the application where the registry server is located.
<\/p>\r\n\r\n\r\n\r\n\r\n
{\r\n \"status\": \"UP\"\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n
Implementing such a simple health check in Lumen is pretty easy, just add the following code snippet in your web.php located in the routes folder of your project:<\/p>\r\n\r\n\r\n\r\n$router->group(['prefix' => 'health'], function () use ($router) {\r\n\r\n $router->get('', function () {\r\n return response()->json(['status' => 'UP'])\r\n });\r\n\r\n});<\/code><\/pre>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\nRoundup<\/h2>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\n
<\/p>\r\n\r\n\r\n\r\n