I'm currently working on a Spring Boot microservices project. I have created services and each service is running separately. With this, I need some services to communicate with other services. How can i achieve that?
I saw some blogs about this which use Netflix, Eureka cloud servers to achieve this. Is there any way I can achieve this in my local environment without using cloud servers?
One microservice can easily expose a REST endpoint for other microservices to call it. Integration via RESTful endpoints is typically implemented when a microservice needs to call another and receive an immediate (synchronous) response.
Spring Boot Microservices: Creating an Item Catalog ServiceUse Item-catalog-service for the artifact name and click on Next. Add the following dependencies: Actuator: features to help you monitor and manage your application. Eureka Discovery: for service registration.
Of course you can. Microservices are just REST-Services. You need to understand how REST-Services work. After that just write 2 Microservices (2 Rest-Services: producer-service and consumer-service) with Spring-boot, let them run under different server-ports, call the consumer-service from the other, and that's it: you have your Microservices. Now this is the primitive way to write Microservices.
To make them evolve, you need to add some "magic" (no rocket science), for example using Ribbon to distribute load between two instances of your "producer-service".
You may use a discovery service which is just a spring-boot application with the annotation @EnableEurekaServer (You need to add the appropriate dependency in your pom) Now add to your first (primitive) Microservices the annotation @EnableDiscoveryClient to the main classes and the defaultZone pointing to your eureka-service in the application.properties (or application.yml) of both, start your eureka-service (discovery service) and the 2 Microservices: those will register on the discovery-service. Of course now you don't need to hard-code the http address of the producer-service in the consumer-service.
Take a look at this tutorial
Edited on 21th of November 2018 at 12:41 GMT
Suppose that your first (trivial) microservice (a pure rest-service) is running on your PC under port 8091.
In the controller of your second (trivial) microservice you call your first service using the RestTemplate.getForEntity(url,responseType,uriVariables) like so for the example in the linked tutorial:
ResponseEntity<CurrencyConversionBean> responseEntity = new RestTemplate().getForEntity( "http://localhost:8091/currency-exchange/from/{from}/to/{to}", CurrencyConversionBean.class, uriVariables);
Where url: the url of your first (micro)(rest)service. responseType: the class/type of the object awaited as response. uriVariables: is a map containing variables for the URI template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With