This is the Part – II post of the Spring Microservices with Netflix stack series. In this post, let us create a RESTful Service and register it with the Eureka Server. This will be my Eureka Client. To learn how to create Eureka Server which is a service registry, move to Part-I.
Create a spring boot application
Create a spring boot application using spring starter project in eclipse or from http://start.spring.io/. I am giving the artifact name as hotel-service. Add Web, Eureka Discovery as dependencies. The application is created and gets downloaded. Now, import this as a maven project in eclipse.To know more on how to create a RESTful application in detail using Spring Boot in detail, click here.
Now, lets get started
pom.xml
The dependencies in pom.xml
1 2 3 4 5 6 7 8 9 10 11 |
<!-- for Eureka Client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
application.yml
Just give the application name and server port number in application.yml
1 2 3 4 5 6 7 |
server: port: 8081 spring: application: name: hotel-service |
Model Class
Create a class Hotel with instance variables and getter and setter methods.
1 2 3 4 5 6 7 8 9 |
public class Hotel { private String name; private String city; private String cuisine; //add constructor and getter, setter methods } |
Controller Class
Create a class and annotate with @RestController. Add two methods and annotate with @RequestMapping.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@RestController public class HotelController { @RequestMapping("/greet/{name}") public String welcomeMessage(@PathVariable("name")String name) { return "Welcome "+name; } @RequestMapping("/all-hotels") public List<Hotel> viewHotels() { return getAllHotels(); } private List<Hotel> getAllHotels(){ List<Hotel> hotelList= new ArrayList<>(); hotelList.add(new Hotel("Keys","Bangalore","Continental")); hotelList.add(new Hotel("Sangeeta","Bangalore","Indian")); hotelList.add(new Hotel("Ruffles","Bangalore","Continental")); hotelList.add(new Hotel("A2B","Bangalore","Indian")); hotelList.add(new Hotel("The Dhaba","Bangalore","Indian")); return hotelList; } } |
Main Class – Eureka Client
Annotate the main class with @EnableEurekaClient. This annotation helps to register the RESTful service in the service registry(Eureka).
1 2 3 4 5 6 7 8 9 10 |
@EnableEurekaClient @SpringBootApplication public class HotelServiceApplication { public static void main(String[] args) { SpringApplication.run(HotelServiceApplication.class, args); } } |
Run the application as Spring Boot App. Check your REST service that is running up in
http://localhost:8081/greet/Jack
http://localhost:8081/greet/all-hotels
Eureka Console
Now, lets also check whether this service is registered with the service registry. Open the eureka console at http://localhost:8761. You can see that the hotel service application is registered
Test the Eureka Client
Click on the RESTful Service in the Eureka Console. It gets opened in the browser. Give the proper path (/all-hotels) and check for the expected output. You can see the output now in the browser as given below.
Thats it. In the next post Part-III, let us create a Feign Client, and access this rest service using the feign client.