Micronaut vs. Spring Boot – Micronaut vs. Spring Boot

最后修改: 2021年 12月 20日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’re going to compare Micronaut and Spring Boot. Spring Boot is a part of the popular Spring framework used for getting Spring applications up and running quickly. Micronaut is a JVM-based framework created to address some of the weaknesses of Spring/Spring Boot.

在本教程中,我们将比较MicronautSpring BootSpring Boot是流行的Spring框架的一部分,用于使Spring应用程序快速启动和运行。Micronaut是一个基于JVM的框架,为解决Spring/Spring Boot的一些弱点而创建。

We’ll compare the two frameworks in several areas. First, we’ll compare the ease of creating a new application, language support, and other configuration options. Then we’ll look at two simple REST applications. Finally, we’ll compare the code and measure performance differences.

我们将在几个方面比较这两个框架。首先,我们将比较创建一个新的应用程序的难易程度、语言支持和其他配置选项。然后我们将看看两个简单的REST应用程序。最后,我们将比较代码并测量性能差异。

2. Features

2.特点

In the following sections, we’ll break down several features in the two frameworks.

在下面的章节中,我们将对这两个框架的几个特征进行分解。

2.1. Setup

2.1.设置

First, we’ll compare the ease of getting a new application up and running in the two frameworks.

首先,我们将比较在这两个框架中启动和运行一个新的应用程序的容易程度。

Both Micronaut and Spring Boot offer multiple convenient methods for creating new applications. For example, we can create a new application using either framework with a command-line interface. Alternatively, we could use the Spring Initializr for Spring Boot or a similar tool for Micronaut called Launch.

Micronaut和Spring Boot都提供了多种方便的方法来创建新的应用程序。例如,我们可以使用任何一个框架的命令行界面来创建一个新的应用程序。另外,我们可以使用Spring Boot的Spring Initializr或Micronaut的类似工具Launch

In terms of IDE support, we can use Spring Boot plugins for most popular IDEs, including its flavor of Eclipse, the Eclipse Spring Tools Suite. We have a Micronaut plugin available to us if we’re using IntelliJ.

在IDE支持方面,我们可以为大多数流行的IDE使用Spring Boot插件,包括其Eclipse的风味,即Eclipse Spring Tools Suite。如果我们使用IntelliJ,我们有一个Micronaut插件可用。

2.2. Language Support

2.2.语言支持

As we turn to language support, we’ll find that it’s nearly identical for Spring Boot and Micronaut. For both frameworks, we can choose between Java, Groovy, or Kotlin. If we choose Java, both frameworks support Java 8, 11, and 17. Additionally, we can use either Gradle or Maven with both frameworks.

当我们转向语言支持时,我们会发现Spring Boot和Micronaut的语言支持几乎是相同的。对于这两个框架,我们可以选择Java、Groovy,或Kotlin。如果我们选择Java,这两个框架都支持Java 8、11和17。此外,我们可以在这两个框架中使用Gradle或Maven。

2.3. Servlet Container

2.3 Servlet容器

Using Spring Boot, our application will use Tomcat by default. However, we can configure Spring Boot to use either Jetty or Undertow as well.

使用Spring Boot,我们的应用程序将默认使用Tomcat。但是,我们也可以将Spring Boot配置为使用Jetty或Undertow

Our Micronaut applications will, by default, run on a Netty-based HTTP Server. However, we can choose to switch our application to run on Tomcat, Jetty, or Undertow.

我们的Micronaut应用程序将默认运行在一个基于Netty的HTTP服务器上。然而,我们可以选择将我们的应用程序切换到Tomcat、Jetty或Undertow上运行。

2.4. Properties Configuration

2.4.属性配置

For Spring Boot, we can define our properties in application.properties or application.yml. We can use the application-{env}.properties convention to provide different properties for different environments. Additionally, we can override these applications file provided properties using system properties, environment variables, or JNDI attributes.

对于Spring Boot,我们可以在application.propertiesapplication.yml中定义我们的properties。我们可以使用application-{env}.properties约定,为不同的环境提供不同的属性。此外,我们可以使用系统属性、环境变量或JNDI属性覆盖这些应用程序文件提供的属性。

We can use application.properties, application.yml, and application.json for our properties files in Micronaut. We can also use the same convention for supplying environment-specific properties files. If we need to override any properties, we can use system properties or environment variables.

我们可以使用application.properties, application.yml, 和application.json作为Micronaut中的属性文件。我们也可以使用同样的约定来提供特定环境的属性文件。如果我们需要覆盖任何属性,我们可以使用系统属性或环境变量。

2.5. Messaging Support

2.5.信息传递支持

If we’re using messaging with Spring Boot, we have Active MQ, Artemis, Rabbit MQ, and Apache Kafka available to us.

如果我们使用Spring Boot的消息传递,我们有Active MQ、Artemis、Rabbit MQApache Kafka可供我们选择。

On the Micronaut side, we have Apache Kafka, Rabbit MQ, and Nats.io as options.

在Micronaut方面,我们有Apache Kafka、Rabbit MQ和Nats.io作为选项。

2.6. Security

2.6.安全问题

Spring Boot offers five authorization strategies: basic, form login, JWT, SAML, and LDAP. If we’re using Micronaut, we have the same options minus the SAML.

Spring Boot提供了五种授权策略:基本、表单登录、JWT、SAML和LDAP。如果我们使用Micronaut,我们也有同样的选项,但不包括SAML。

Both frameworks provide us with OAuth2 support.

这两个框架都为我们提供了OAuth2>支持。

In terms of actually applying the security, both frameworks allow us to use annotations to secure methods.

在实际应用安全方面,两个框架都允许我们使用注解来保护方法。

2.7. Management and Monitoring

2.7.管理和监测

Both frameworks provide us with the ability to monitor various metrics and statistics in our applications. We can define custom endpoints in both frameworks. We also can configure endpoint security in both frameworks.

这两个框架都为我们提供了监测应用程序中各种指标和统计数据的能力。我们可以在这两个框架中定义自定义端点。我们还可以在这两个框架中配置端点的安全性。

However, the Spring Boot actuator provides several more built-in endpoints than Micronaut.

然而, Spring Boot执行器比Micronaut提供了更多的内置端点。

2.8. Template Languages

2.8.模板语言

We can create complete full-stack applications with both frameworks, using provided template languages to render the front-end.

我们可以用这两个框架创建完整的全栈应用程序,使用提供的模板语言来渲染前端。

For Spring Boot, our choices are Thymeleaf, Apache Freemarker, Mustache, and Groovy. We can also use JSP, although the practice is discouraged.

对于Spring Boot,我们的选择是ThymeleafApache FreemarkerMustache、以及Groovy。我们还可以使用JSP,尽管不鼓励这种做法。

We have a few more options available in Micronaut: Thymeleaf, Handlebars, Apache Velocity, Apache Freemarker, Rocker, Soy/Closure, and Pebbles.

我们在Micronaut中还有一些选择。Thymeleaf、Handlebars、Apache Velocity、Apache Freemarker、Rocker、Soy/Closure和Pebbles。

2.9. Cloud Support

2.9.云支持

Spring Boot applications rely on third-party libraries for many cloud-specific features.

Spring Boot 应用程序依靠第三方库来实现许多云的特定功能。

Micronaut is natively designed for cloud microservices. Cloud concepts that Micronaut will natively handle for us include distributed configuration, service discovery, client-side load balancing, distributed tracing, and serverless functions.

Micronaut是为云微服务设计的。Micronaut将为我们处理的云概念包括分布式配置、服务发现、客户端负载平衡、分布式跟踪和无服务器功能。

3. The Code

3.守则

Now that we’ve compared some basic features in the two frameworks, let’s create and compare two applications. To keep things simple, we’ll create a simple REST API that solves basic arithmetic problems. Our service layer will consist of a class that actually does the math for us. Our controller class will contain an endpoint for addition, subtraction, multiplication, and division.

现在我们已经比较了这两个框架的一些基本功能,让我们创建并比较两个应用程序。为了保持简单,我们将创建一个简单的REST API,解决基本的算术问题。我们的服务层将由一个实际为我们做数学运算的类组成。我们的控制器类将包含一个加法、减法、乘法和除法的端点。

Before we dig into the code, let’s consider a significant difference between Spring Boot and Micronaut. Although the two frameworks provide dependency injection, they go about it differently. Our Spring Boot application handles dependency injection at runtime using reflection and proxies. In contrast, our Micronaut application builds dependency injection data when it’s compiled.

在我们深入研究代码之前,让我们考虑一下Spring Boot和Micronaut之间的一个重大区别。尽管这两个框架都提供了依赖性注入,但它们的做法是不同的。我们的Spring Boot应用程序在运行时使用反射和代理来处理依赖性注入。相比之下,我们的Micronaut应用程序在编译时建立了依赖性注入数据。

3.1. The Spring Boot Application

3.1.Spring Boot应用程序

First, let’s define a class in our Spring Boot application called ArithmeticService:

首先,让我们在Spring Boot应用程序中定义一个名为ArithmeticService的类。

@Service
public class ArithmeticService {
    public float add(float number1, float number2) {
        return number1 + number2;
    }
    
    public float subtract(float number1, float number2) {
        return number1 - number2;
    }
    
    public float multiply(float number1, float number2) {
        return number1 * number2;
    }
    
    public float divide(float number1, float number2) {
        if (number2 == 0) {
            throw new IllegalArgumentException("'number2' cannot be zero");
        }
        return number1 / number2;
    }
}

Next, let’s create our REST controller:

接下来,让我们创建我们的REST控制器。

@RestController
@RequestMapping("/math")
public class ArithmeticController {
    @Autowired
    private ArithmeticService arithmeticService;
    
    @GetMapping("/sum/{number1}/{number2}")
    public float getSum(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
    	return arithmeticService.add(number1, number2);
    }
    
    @GetMapping("/subtract/{number1}/{number2}")
    public float getDifference(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
    	return arithmeticService.subtract(number1, number2);
    }
    
    @GetMapping("/multiply/{number1}/{number2}")
    public float getMultiplication(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
    	return arithmeticService.multiply(number1, number2);
    }
    
    @GetMapping("/divide/{number1}/{number2}")
    public float getDivision(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
    	return arithmeticService.divide(number1, number2);
    }
}

Our controller has an endpoint for each of the four arithmetic functions.

我们的控制器为四个算术函数中的每一个都有一个端点。

3.2. The Micronaut Application

3.2.Micronaut应用程序

Now, let’s create the service layer of our Micronaut application:

现在,让我们来创建Micronaut应用程序的服务层。

@Singleton 
public class ArithmeticService {
    // implementation identical to the Spring Boot service layer
}

Next, we’ll write our REST controller with the same four endpoints as the Spring Boot applications:

接下来,我们将用与Spring Boot应用程序相同的四个端点来编写我们的REST控制器。

@Controller("/math")
public class ArithmeticController {
    @Inject
    private ArithmeticService arithmeticService;
    
    @Get("/sum/{number1}/{number2}")
    public float getSum(float number1, float number2) {
    	return arithmeticService.add(number1, number2);
    }
    
    @Get("/subtract/{number1}/{number2}")
    public float getDifference(float number1, float number2) {
    	return arithmeticService.subtract(number1, number2);
    }
    
    @Get("/multiply/{number1}/{number2}")
    public float getMultiplication(float number1, float number2) {
    	return arithmeticService.multiply(number1, number2);
    }
    
    @Get("/divide/{number1}/{number2}")
    public float getDivision(float number1, float number2) {
    	return arithmeticService.divide(number1, number2);
    }
}

We can see a lot of similarities between our very simple example applications. In terms of differences, we see that Micronaut takes advantage of Java’s annotations for injection whereas Spring Boot has its own.  Additionally, our Micronaut REST endpoints don’t require any special annotations on the path variables passed into the methods.

我们可以看到我们非常简单的示例应用程序之间有很多相似之处。在差异方面,我们看到Micronaut利用了Java的注解进行注入,而Spring Boot有自己的注解。 此外,我们的Micronaut REST端点不需要对传递到方法中的路径变量进行任何特殊注释。

3.3. Basic Performance Comparison

3.3.基本性能比较

Micronaut advertises fast start-up times, so let’s compare our two applications.

Micronaut宣传的是快速启动时间,所以让我们比较一下我们的两个应用程序。

First, let’s fire up the Spring Boot application and see how long it takes:

首先,让我们启动Spring Boot应用程序,看看它需要多长时间。

[main] INFO  c.b.m.v.s.CompareApplication - Started CompareApplication in 3.179 seconds (JVM running for 4.164)

Next, let’s see how quickly our Micronaut application starts:

接下来,让我们看看我们的Micronaut应用程序的启动速度。

21:22:49.267 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 1278ms. Server Running: http://localhost:57535

We can see that our Spring Boot application starts up in just over three seconds and a little over one second in Micronaut.

我们可以看到,我们的Spring Boot应用在三秒多一点就启动了,而在Micronaut中则是一秒多一点。

Now that we’ve looked at start-up time, let’s exercise our APIs a bit and then check some basic memory statistics. We’ll use the default memory settings when we start our applications.

现在我们已经看了启动时间,让我们锻炼一下我们的API,然后检查一些基本的内存统计。当我们启动我们的应用程序时,我们将使用默认的内存设置。

We’ll start with the Spring Boot application. First, let’s call each of the four arithmetic endpoints and then pull our memory endpoint:

我们将从Spring Boot应用程序开始。首先,让我们分别调用四个算术端点,然后拉动我们的内存端点。

Initial: 0.25 GB 
Used: 0.02 GB 
Max: 4.00 GB 
Committed: 0.06 GB 

Next, let’s run through the same exercise with our Micronaut application:

接下来,让我们用我们的Micronaut应用程序进行同样的练习。

Initial: 0.25 GB 
Used: 0.01 GB 
Max: 4.00 GB 
Committed: 0.03 GB

In this limited example, both our applications use little memory, but the Micronaut uses about half as much as the Spring Boot application.

在这个有限的例子中,我们的两个应用程序使用的内存都很少,但Micronaut使用的内存大约是Spring Boot应用程序的一半。

4. Conclusion

4.总结

In this article, we compared Spring Boot with Micronaut. First, we started with an overview of what the two frameworks are. Then, we went through several features and compared the options. Finally, we pitted two simple example applications against each other. We took a look at the code for both applications and then looked at start-up and memory performance.

在这篇文章中,我们比较了Spring Boot和Micronaut。首先,我们先概述了这两个框架是什么。然后,我们浏览了几个功能并比较了各种选项。最后,我们将两个简单的示例应用程序进行了对比。我们看了一下两个应用的代码,然后看了一下启动和内存性能。

As always, the example code is available over on GitHub for both the Spring Boot and the Micronaut application.

一如既往,GitHub上提供了Spring BootMicronaut应用程序的示例代码。