Introduction
In this chapter, we will explore Spring Boot Embedded Servers, which are integral to creating stand-alone, production-grade Spring applications. Embedded servers allow you to run your Spring Boot applications without the need for an external server setup, simplifying the deployment and development processes.
What are Spring Boot Embedded Servers?
Spring Boot Embedded Servers are built-in servers that come bundled with your Spring Boot application. These servers run your application as a stand-alone service, eliminating the need to deploy your application on an external server. Common embedded servers include Tomcat, Jetty, and Undertow.
Key Features of Spring Boot Embedded Servers
- Simplicity: Eliminates the need for separate server installation and configuration.
- Portability: Makes your application self-contained and portable.
- Convenience: Eases the development process by allowing you to run your application directly from your IDE or command line.
Common Embedded Servers in Spring Boot
1. Tomcat
- Default Server: Tomcat is the default embedded server used by Spring Boot.
- Configuration: Automatically configured by Spring Boot when the
spring-boot-starter-webdependency is added.
2. Jetty
- Alternative Server: Jetty is another popular choice for an embedded server.
- Configuration: To use Jetty, you need to exclude Tomcat and include the
spring-boot-starter-jettydependency.
3. Undertow
- Lightweight Server: Undertow is a lightweight, high-performance server.
- Configuration: To use Undertow, you need to exclude Tomcat and include the
spring-boot-starter-undertowdependency.
How to Use Embedded Servers
Using the Default Tomcat Server
When you create a Spring Boot project with the spring-boot-starter-web dependency, Tomcat is included and configured by default.
Example pom.xml for Maven
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.company</groupId>
<artifactId>first-springboot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>first-springboot-api</name>
<description>First Spring Boot REST API</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Switching to Jetty
To use Jetty as the embedded server, you need to exclude Tomcat and include Jetty in your dependencies.
Example pom.xml for Maven
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.company</groupId>
<artifactId>first-springboot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>first-springboot-api</name>
<description>First Spring Boot REST API</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Switching to Undertow
To use Undertow as the embedded server, you need to exclude Tomcat and include Undertow in your dependencies.
Example pom.xml for Maven
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.company</groupId>
<artifactId>first-springboot-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>first-springboot-api</name>
<description>First Spring Boot REST API</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Configuring Embedded Servers
You can customize the configuration of the embedded server through application properties or YAML files.
Example Configuration in application.properties
server.port=8081
server.servlet.context-path=/api
server.tomcat.max-threads=200
Example Configuration in application.yml
server:
port: 8081
servlet:
context-path: /api
tomcat:
max-threads: 200
Benefits of Using Embedded Servers
1. Simplicity in Deployment
Embedded servers simplify the deployment process as you don’t need to install and configure an external server. You can package your application as a JAR file that includes the server and run it with a simple command.
2. Portability
With an embedded server, your application becomes self-contained and portable. You can easily move it between different environments without worrying about server configurations.
3. Ease of Development
Embedded servers allow you to run your application directly from your IDE or command line, making the development process more straightforward and efficient.
Conclusion
In this chapter, we have covered the concept of Spring Boot Embedded Servers, their key features, and how to use them in your Spring Boot projects. We also looked at configuring and customizing these embedded servers to suit your application’s needs. In the next chapter, we will explore more advanced configurations and features of Spring Boot.