Spring Boot Microservices Communication with Retrofit
Hi all,
In my first article, I would like to talk about Retrofit in Sprint boot microservice communications. Before start, I have to highlight that it is a milestone version so this article will be preview of it :)
When you click the Square Retrofit page, the first thing you will see is A type-safe HTTP client for Android and Java. Personally, Retrofit is my first choice as Http Client Library in both Android and Backend.
However, when you work on microservice with Sprint Boot, there are lots of microservice communication libraries from top to bottom as OpenFeign, Resttemplate or maybe WebClient if reactive approach has been chosen. Each of them has advantages and disadvantages at themselves. According to dependencies, architecture or approach, one of them can be suitable for your project.
My first choice is still OpenFeign in microservice, but sometimes, I wished that what if Retrofit can take OpenFeign and other libraries place! The main reason why Retrofit is my first choice is power of interceptors and easy implementation and async calls.
Approximately 1 year ago, I encountered such a wonderful video on YouTube that Josh Long introduced Spring Cloud Square.
As Josh Long mentioned I love OpenFeign but it does not do great with asynchronous or reactive sort of use cases. Finally so I am so glad there is an alternative.
Spring Cloud Square is still in milestone version, I hope at the first quarter of the new year, stable version will be published. Till that day, let’s take a look at preview.
First, we include dependencies in gradle file.
implementation ‘org.springframework.cloud:spring-cloud-square-retrofit:0.4.1’implementation ‘org.springframework.cloud:spring-cloud-square-okhttp:0.4.1’
After loading libraries, configuration class was generated for retrofit clients in application.
In this demo, I used Retrofit with OkHttpClient and Spring Cloud LoadBalancer. But you can also use WebClient (https://github.com/spring-cloud-incubator/spring-cloud-square). If Retrofit is used for also external Rest API, it might be helpful declaring basePackages for clarity of microservice communication.
Like classic Retrofit implementation, Service Interface is created for API’s. At that point, you have to add @RetrofitClient annotation for your microservice and if you have specific configuration for target microservice, you can add configuration.
At custom configuration, multiple interceptor can be added. In this example, there is token interceptor for security layer between microservice, authenticator interceptor for refreshing accessToken by using OkHttp Authenticator and also user specific headers such as language, app version and etc. If interceptors have dependencies itself such as token service implementation, interceptors should have added as autowired. Otherwise, classic approach can be used.
After the development of API interface and configuration, client can be added to service. At this demo, error handling was done in abstract class but you can also add interceptor, it is totally depends on your service architecture.
With the help of Retrofit’s easy sync and async process ability, it will be wonderful alternative in microservice communication regarding to OpenFeign.
2022-12-13 13:00:19.307 INFO [retrofitdemo,010ce3b09e6c23ff,010ce3b09e6c23ff] 8884 --- [nio-8085-exec-1] okhttp3.OkHttpClient : --> GET http://welcome-service/v1/whoiam?name=Oguz
2022-12-13 13:00:19.424 INFO [retrofitdemo,010ce3b09e6c23ff,010ce3b09e6c23ff] 8884 --- [nio-8085-exec-1] okhttp3.OkHttpClient : <-- 200 http://127.0.0.1:8086/v1/whoiam?name=Oguz&constantQuery=fromInterceptor (116ms, unknown-length body)
I hope Spring Cloud Square stable version will be published soon. Please CLAP :)