Microservices, are trending right now, whether you like it or not. There are good reasons for that as it resolves many issues organizations are faced with. It also opens a Pandora box as new issues pop up every now and then… But that is a story for another day: in the end, microservices are here to stay.
In this series of articles, I’ll take a simple Spring Boot app ecosystem and turn it into microservices to deploy them into the Cloud. As an example, I’ll use an ecommerce shop, that requires different services such as:
- an account service
- a product service
- a cart service
- etc.
This week is dedicated to creating a sample REST application that lists products. It is based on Spring Boot because Boot makes developing such an application a breeze, as will see in the rest of this article.
Let’s use Maven. The relevant part of the POM is the following:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.M5</version>
</parent>
<groupId>ch.frankel.microservice</groupId>
<artifactId>microservice-sample</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Easy enough:
- Use Java 8
- Inherit from Spring Boot parent POM
- Add Spring Data JPA & Spring Data REST dependencies
- Add a database provider, h2 for now
The next step is the application entry point, it’s the standard Spring Boot main class:
@SpringBootApplication
public class ProductApplication {
public static void main(String... args) {
SpringApplication.run(ProductApplication.class);
}
}
It’s very straightforward, thanks to Spring Boot inferring which dependencies are on the classpath.
Besides that, it’s just adding a simple Product
entity class and a Spring Data repository interface:
@Entity
public class Product {
@Id
@GeneratedValue(strategy = AUTO)
private long id;
private String name;
// Constructors and getters
}
public interface ProductRepository extends JpaRepository<Product, Long> {}
At this point, we’re done. Spring Data REST will automatically provide a REST endpoint to the repository. After executing the main class of the application, browsing to http://localhost:8080/products will yield the list of available products in JSON format.
If you don’t believe how easy this, then you’re welcome to take a look at the Github repo and just launch the app with mvn spring-boot:run
.
There’s a script to populate initial data present.
Next week, I’ll try to upload the application in the cloud (probably among Cloud Foundry, Heroku and YaaS).