Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The Microservices Way Part 1

DZone's Guide to

The Microservices Way Part 1

Microservices are really hot right now. Here's a neat example using Spring Boot to create microservices for Cloud deployment.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

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 serie 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:

  1. Use Java 8
  2. Inherit from Spring Boot parent POM
  3. Add Spring Data JPA & Spring Data REST dependencies
  4. 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 FoundryHeroku and YaaS).

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
microservices ,spring boot

Published at DZone with permission of Nicolas Frankel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}