How-tos

Creating, running and deploying Spring microservices in 5 minutes

Share this post:

Spring developers looking for a quick way to create a microservice which can be deployed on Bluemix should check out the latest version of the Bluemix Developer CLI plugin which now has the option to create a Spring microservice. Just to clarify things right at the start, this isn’t an alternative to Spring Initializer, it’s a way to create a basic microservice which can be optionally bound to Bluemix services and deployed using the CLI.

In this post I am going to show how the Bluemix CLI can be used to generate a Spring microservice, provision and bind services to it and finally deploy to Bluemix. We’ll also take a look at the code that has been generated and how that provides a great starting point for you to begin adding your own code.

Generating the code

If you haven’t already done so, then you’ll need to follow the instructions for installing the Bluemix Developer CLI plugin if you want to try things out for yourself. A Spring microservice can be generated using the CLI as shown below with the generated code being put into a folder under the current directory.

spring $bx dev create
? Select a pattern:                        
1. Web App
2. Mobile App
3. Backend for Frontend
4. Microservice
5. MFP
Enter a number> 4

? Select a starter:
1. Basic
Enter a number> 1

? Select a language:
1. Java - MicroProfile / Java EE
2. Node
3. Python
4. Java - Spring Framework
Enter a number> 4

? Enter a name for your project> springmsdemo                             
? Enter a hostname for your project> springmsdemo
? Do you want to add services to your project? [y/n]> y

? Select a service:
1. Cloudant NoSQL Database
2. Object Storage
Enter a number> 1

? Select a service plan:               
1. Lite
2. Standard
3. Dedicated Hardware
Enter a number> 1

Successfully added service to project.               

? Do you want to add another service? [y/n]> n
                                  
The project, springmsdemo, has been successfully saved into the current directory.
OK

Before carrying on, let’s look at what we’ve just selected and what has been generated. The first couple of selections establish that we want a microservice and it’s going to use Spring. Things get interesting when we’re presented with the service options. If you pick a service, in this case Cloudant, then not only is code generated to bind to that service but it is also provisioned ready for use.

Other files that are generated will be used when running your application locally in a docker container. The README is a good place to start as it will provide an overview on what has been generated as well as more information about the services that were selected.

Running the microservice

The CLI is then used to build and run the microservice locally. The build stage will create a docker container that provides the necessary tools to build the microservice, a second container is then used to run the built container.

springmsdemo $bx dev build
Deleting the container named 'bx-dev-springmsdemo-tools' ...
Creating image bx-dev-java-maven-tools based on Dockerfile-tools...
Image will have user added
OK                    
Creating a container named 'bx-dev-springmsdemo-tools' from that image...
OK
Starting the 'bx-dev-springmsdemo-tools' container...
OK
Building the project in the current directory started at Wed Aug 23 13:49:37 2017
OK                    
Stopping the 'springmsdemo' container...
The 'springmsdemo' container was not found
Stopping the 'bx-dev-springmsdemo-tools' container...
OK
springmsdemo $bx dev run
Stopping the 'springmsdemo' container...
The 'springmsdemo' container was not found
Creating image springmsdemo based on Dockerfile...
OK                    
Creating a container named 'springmsdemo' from that image...
OK
Starting the 'springmsdemo' container...
OK
Executing run command started at Wed Aug 23 13:50:17 2017

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)

2017-08-23 12:50:22.167  INFO 17 --- [           main] application.SBApplication                : Starting SBApplication v1.0-SNAPSHOT on d6e23df14534 with PID 17 (/project/springmsdemo-1.0-SNAPSHOT.jar started by root in /project)
... 
(output snipped)

You can now test your microservice locally by going to http://localhost:8080/v1/cloudant.

The really nice thing is that although the list of databases is empty, the list has been read from the Cloudant service instance that was provisioned earlier.

[code language=”java”]
@RestController
public class Example {

@Autowired @ServiceName(name="springmsdemo-cloudantNoSQLDB-4dce")
private CloudantClient client;

@RequestMapping("v1/")
public @ResponseBody ResponseEntity<String> example() {
List<String> list = new ArrayList<>();
//return a simple list of strings
list.add("Some data");
return new ResponseEntity<String>(list.toString(), HttpStatus.OK);
}

@RequestMapping("v1/cloudant")
public @ResponseBody ResponseEntity<String> cloudant(){
List<String> list = new ArrayList<>();
try {
list = client.getAllDbs();
} catch (NullPointerException e) {
return new ResponseEntity<String>("Server Error", HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<String>("Available databases : " + list.toString(), HttpStatus.OK);
}

}
[/code]

Deploying to bluemix

The final step is to deploy the microservice to Bluemix. Simply type ‘bx dev deploy‘ on the command line and that’s it. You can then view the microservice in the Bluemix console (along with the service that was created).

This post has shown how to use the Bluemix CLI Developer plug-in to create a Spring microservice bound to a Cloudant database, build and run it locally then finally deploy it Bluemix – all in under 5 minutes !

More How-tos stories
November 14, 2018

PostgreSQL Tips: Template Databases

In this PostgreSQL Tip, we demystify PostgreSQL template databases and how and when you might use them. Template databases are really useful when you use the same database objects every time you create a new database.

Continue reading

November 9, 2018

Build Messaging Solutions with Apache Kafka or Event Streams for IBM Cloud: Part 3

One key aspect of a robust architecture is that it is built to smoothly handle system failures, outages, and configuration changes without violating the data loss and consistency requirements of the use case. To proactively build such solutions needs an understanding of the possible exceptions and risky scenarios and preparedness to manage them efficiently.

Continue reading

November 9, 2018

How to Set Up a Concourse CI/CD Pipeline

To hit the ground running for any project based on cloud-based applications, it is very important to set up the associated infrastructure for the development of microservices. Quick and high-speed development can be achieved if the team has a way to do Continuous Integration (CI) and Continuous Delivery (CD) of the application.

Continue reading