API · Microservices · REST · Uncategorized

Microservices with Docker and Spring Boot


This guide is for someone interesting in quickly building a micro-service using the Spring Boot framework. Works great if you have prior JAX-RS / Jersey experience

Step-by-step guide

Add the steps involved:

  1. Create a Maven Project
    1. Final Structure
    2. Pom ( Attached here )
  2. Include Spring Boot Dependencies in your pom
    1. Parent Project
      	<parent>
      
      		<groupId>org.springframework.boot</groupId>
      
      		<artifactId>spring-boot-starter-parent</artifactId>
      
      		<version>1.3.1.RELEASE</version>
      
      	</parent>
    2. Dependencies
      	<dependencies>
      
      		<dependency>
      
      			<groupId>junit</groupId>
      
      			<artifactId>junit</artifactId>
      
      			<scope>test</scope>
      
      		</dependency>
      
      		<dependency>
      
      			<groupId>org.springframework.boot</groupId>
      
      			<artifactId>spring-boot-starter-web</artifactId>
      
      		</dependency>
      
      		<dependency>
      
      			<groupId>org.springframework.boot</groupId>
      
      			<artifactId>spring-boot-starter-jersey</artifactId>
      
      		</dependency>
      
      	</dependencies>
  3. Create the Java main class – this is entry point with the @SpringBootApplication annotation
    1. App.java
      @SpringBootApplication
      public class App 
      {
          public static void main( String[] args )
          {
          	SpringApplication.run(App.class, args);
          }
      }
  4. Create the API Controller class
    1. Add Spring MVC methods for REST in the API controller class
      1. SimpleAPIController.java with SpringMVC only
        @Component
        @RestController
        @Path("/api")
        public class SimpleAPIController {
        	@RequestMapping(value = "/springmvc", produces = "application/json")
            public Map<String, Object> springMvc() {
        		Map<String, Object> data = new HashMap<String, Object>();
        		data.put("message", "Spring MVC Implementation of API is available");
        		return data;
            }
        }
    2. (Alternate/Additional) Add Jersery methods for REST in the API controller class
      1. SimpleAPIController.java with Jersey (Note: You need the spring boot jersey dependency in your pom)
        @Component
        @RestController
        @Path("/api")
        public class SimpleAPIController {
        	@GET
        	@Produces({ "application/json" })
        	public Response getMessage() {
        		Map<String, Object> data = new HashMap<String, Object>();
        		data.put("message", "Jersey Implementation of API is available");
        		Response response = Response.ok().entity(data).build();
        		return response;
        	}
        }
    3. (Alternate/Additional) Create a JerseyConfig class
      1. JerseryConfig.java – required for jersey rest implementation
        @Configuration
        public class JerseyConfig extends ResourceConfig {
            public JerseyConfig() {
                register(SimpleAPIController.class);
            }
        }
  5. Build the Maven project
    1. Add the Spring boot maven build plugin
      	<build>
      		<plugins>
      			<plugin>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-maven-plugin</artifactId>
      			</plugin>
      		</plugins>
      	</build>
  6. Execute the spring boot application
    1. Run in Eclipse as Java Application
    2. Run as a standalone Java Jar
      java -jar target/SimpleService-0.0.1-SNAPSHOT.jar
    3. Specify a different port and run as a standalone Java Jar

      java -Dserver.port=8090 -jar target/SimpleService-0.0.1-SNAPSHOT.jar
    4. Running in a docker container

      1. See: https://spring.io/guides/gs/spring-boot-docker/
      2. Docker file
        FROM java:8
        VOLUME /tmp
        ADD SimpleService-0.0.1-SNAPSHOT.jar app.jar
        RUN bash -c 'touch /app.jar'
        ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

 

The official how-to is here https://spring.io/guides/gs/rest-service/#use-maven and a great blog post is herehttp://blog.codeleak.pl/2015/01/getting-started-with-jersey-and-spring.html
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s