Creating Java Spring Boot microservices
Learn about creating Spring Boot microservices using Spring Initializr, JHipster, or through custom templating.
The Spring framework is incredibly popular for creating Java microservices. Both Spring Boot and Spring Cloud were designed specifically for building microservices, making them easy to work with whether your organization is transitioning from a monolithic architecture or already has a well-established microservice architecture.
If you’ve already adopted this open source framework, you have a few straightforward methods for creating a Spring Boot microservice.
If you’re just starting to create Spring Boot applications, then you’ll want to begin with the Spring Initializr. This initializer operates on the Spring framework and is the standard way to spin up a Spring Boot service.
To start, navigate to start.spring.io — you’ll immediately see Initializr’s wide range of out-of-the-box configurations, making it easy to develop your project from the start. You can choose to build a Maven or Gradle project, program in Java, Kotlin, or Groovy, and work with any of the eight different provided versions of Spring Boot. Initializr gives you the opportunity to include annotations/metadata at this stage, select your package, and the Java version you want.
Initializr makes it easy to add dependencies and Spring Boot starters. Before doing so, it’s critical that you consider the kind of microservice you’re building. Will it make HTTP calls to other services or expose an API? Will it run a scheduled job every few hours or handle events from other services? Being clear on what you’re building and the application properties will lead you to the right dependencies.
The wide range of starters and pre-configurations available is why Initializr is so popular among those building Spring Boot applications. Initializr has over 100 dependencies and, luckily, a search feature, so you can find exactly what you need. Here are some of the dependencies that you may find helpful:
- Spring Boot Dev Tools: Provides fast application restarts, LiveReload, and configurations for enhanced development resources.
- Eureka Discovery Client: REST-based, Spring Cloud service discovery for the purpose of load balancing and failover of middle-tier servers.
- Eureka Server: Spring-cloud-netflix Eureka server.
- Spring Web: Build RESTful web applications using Spring MVC. Uses Apache Tomcat as the default embedded container.
- Spring Security: Highly customizable authentication and access-control framework for Spring applications.
- Spring Data JPA: Persist data in SQL stores with Java Persistence API using Spring Data and Hibernate.
- Jersey: Framework for developing REST APIs and web services in Java.
- JDBC API: Database connectivity API that defines how a client may connect and query a database.
We especially recommend adding the Spring Boot Dev Tools dependency — this will ensure a faster experience when you’re ready to build your service. Once you select any dependency, it will automatically appear in a list so you can easily see everything you’ve added.
Once you click Generate, Initializr will lay all of the groundwork for you — it’ll create a build file with all of your dependencies. You can use Explore to view the generated source code and confirm that the configurations are correct before downloading and running the code.
You can also create a link to your configured code and share it with the rest of your engineering team to standardize how new Spring Boot projects are spun up on your team. You can’t create a truly customized template through Initializr, but the pre-configured build file is a good starting place.
Working with the Spring framework doesn’t mean you have to build from Initializr. JHipster is another popular, open source option — it’s been around for almost 10 years and has almost 20,000 stars on GitHub — and uses both the Angular and Spring frameworks. JHipster is fully loaded with features, making it easy to quickly generate web applications and microservices, as well as lots of modules for added functionality. Because of its following, you’ll also find a number of tutorials for any web service you need to build.
The tradeoff with JHipster is that it’s pretty heavyweight and opinionated. It’s likely that if you’re trying to integrate with one of JHipster’s options, like Kubernetes or Docker, you already have an internal process that developers should be following. In this case, JHipster’s functionality makes it an excellent place to start, but may not be how you generate every service.
Neither JHipster nor Initializr will allow you to build a truly customized template, but both are valid starting places when building a microservice. You can use Initializr to generate baseline source code and templatize it using Cookiecutter. This extra step allows you to include specific files, tweak the build script, and build custom dependencies that aren’t included by default. This approach will make more sense for organizations with established processes and operating procedures.
No matter how you’re creating your microservice, whether you’re using Java or not, the most important part is ensuring that developers are complying to your organization’s standards. Make sure to set up an observability stack, whether that’s through an APM or logging, and confirm that your configurations for deployments, whether through Kubernetes or Docker, are the same. This kind of standardization can be hard to achieve, which is why using Cookiecutter tends to be an easier solution at this stage. Instead of spinning up with Initializr or JHipster, you may find they’re better tools for testing, debugging, or just playing around with.
You can read more about Cookiecutter, Spring, and how Cortex can help you achieve standardization and accountability by subscribing to our blog.