In this post we will learn how to integrate applications developed in Spring Boot with Netflix Eureka.
First step is to create two Spring Boot application services
- customer-service
- order-service
Go to https://start.spring.io and create order-service app using below config details.
Mainly for our use case, adding Spring Web, Eureka Server dependencies are the required ones
Likewise, create customer-service app with same config details used for order-service by adding Spring Web, Eureka Server dependencies.
Next, create eureka-server app with below config . Adding Eureka Server is main important config for our use case
Next, Import the projects created from the above into Eclipse IDE.
Add @EnableEurekaServer on EurekaServerApplication class as shown Below
package com.venkat; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
Add below properties to the application.properties
server.port=8090 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
Next, Add @EnableEurekaClient on CustomerServiceApplication class in customer-service app as shown below
@SpringBootApplication @EnableEurekaClient public class CustomerServiceApplication { public static void main(String[] args) throws RestClientException, IOException { SpringApplication.run(CustomerServiceApplication.class, args); } @Bean public ConsumerControllerClient consumerControllerClient() { return new ConsumerControllerClient(); } }
Likewise, Add @EnableEurekaClient on OrderServiceApplication class in order-service app as shown below
@SpringBootApplication @EnableEurekaClient public class OrderSerivceApplication { public static void main(String[] args) { SpringApplication.run(OrderSerivceApplication.class, args); } }
Next, Add below configuration in customer-service and order-service
application.properties files
customer-service
spring.application.name=customer-service server.port=8091 eureka.client.serviceUrl.defaultZone=http://localhost:8090/eureka
order-service
spring.application.name=order-service server.port=8080 eureka.client.serviceUrl.defaultZone=http://localhost:8090/eureka
Next, Create Order.java in model package as shown below with orderId, itemName, itemType, Cost attributes.
package com.venkat.model; public class Order { private String orderId; private String itemName; private String itemType; private double cost; public Order() { } public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public String getItemType() { return itemType; } public void setItemType(String itemType) { this.itemType = itemType; } public double getCost() { return cost; } public void setCost(double cost) { this.cost = cost; } }
Create OrderController with a REST GET mapping for /customerorder to return Order object
package com.venkat.controllers; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.venkat.model.Order; @RestController public class OrderController { @RequestMapping(value = "/customerorder", method = RequestMethod.GET) public Order customerOrder() { Order order = new Order(); order.setOrderId("TIF567"); order.setItemName("Dosa"); order.setItemType("Tiffin"); order.setCost(40.00); return order; } }
Create a ConsumerControllerClient.java class as shown below
package com.venkat.controllers; import java.io.IOException; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @RestController public class ConsumerControllerClient { @Autowired private DiscoveryClient discoveryClient; @GetMapping(path="/customerorderinfo") public String getCustomerOrder() throws RestClientException, IOException { List<ServiceInstance> instances = discoveryClient.getInstances("ORDER-SERVICE"); // Creating URL for calling order-service ServiceInstance serviceInstance = instances.get(0); String baseURL = serviceInstance.getUri().toString(); String completeURL = baseURL + "/customerorder"; // Calling order-service RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = null; try { response = restTemplate.exchange(completeURL, HttpMethod.GET, getHeaders(), String.class); } catch (Exception ex) { System.out.println(ex); } System.out.println(response.getBody()); return response.getBody(); } private static HttpEntity<?> getHeaders() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); return new HttpEntity<>(headers); } }
Final step is to Run EurekaServer, order-service, customer-service apps
Open http://localhost:8090 (port on which eureka server is running). Here you will see
order-service,customer-service app instances registered with it.
Output on hitting REST Endpoint http://localhost:8091/customerorderinfo is given below
Conclusion
In the above post we have seen how to register two service applications with Netflix Eureka
Server and communicate between the services via Eureka server registry without knowing the
host, port info of the service to which we are communicating with.
2 thoughts on “Spring Boot and Netflix Eureka Integration”