企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 使用Spring Boot Actuator构建RESTful Web服务 [Spring Boot Actuator Spring Boot](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready) 是 的子项目。 它为您的应用程序增加了几项生产级服务,而您却毫不费力。 在本指南中,您将构建一个应用程序,然后查看如何添加这些服务。 ## 你会建立什么 本指南将指导您使用Spring Boot Actuator创建一个“世界,您好”的RESTful Web服务。 您将构建一个接受以下HTTP GET请求的服务: ~~~ $ curl http://localhost:9000/hello-world ~~~ 它使用以下JSON进行响应: ~~~ {"id":1,"content":"Hello, World!"} ~~~ 您的应用程序中还添加了许多功能,用于在生产(或其他)环境中管理服务。 您构建的服务的业务功能与构建 [RESTful Web服务中的功能相同](https://spring.io/guides/gs/rest-service) 。 尽管比较结果可能会很有趣,但是您无需使用该指南即可利用这一指南。 ### 你需要什么 * 约15分钟 * 最喜欢的文本编辑器或IDE * [JDK 1.8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) 或更高版本 * [Gradle 4+](http://www.gradle.org/downloads) 或 [Maven 3.2+](https://maven.apache.org/download.cgi) * 您还可以将代码直接导入到IDE中: * [弹簧工具套件(STS)](https://spring.io/guides/gs/sts) * [IntelliJ IDEA](https://spring.io/guides/gs/intellij-idea/) ## 如何完成本指南 像大多数Spring 一样 [入门指南](https://spring.io/guides) ,您可以从头开始并完成每个步骤,也可以绕过您已经熟悉的基本设置步骤。 无论哪种方式,您最终都可以使用代码。 要 **从头开始** ,请继续进行“ [从Spring Initializr开始”](https://spring.io/guides/gs/actuator-service/#scratch) 。 要 **跳过基础知识** ,请执行以下操作: * [下载](https://github.com/spring-guides/gs-actuator-service/archive/master.zip) 并解压缩本指南的源存储库,或使用 对其进行克隆 [Git](https://spring.io/understanding/Git) : `git clone [https://github.com/spring-guides/gs-actuator-service.git](https://github.com/spring-guides/gs-actuator-service.git)` * 光盘进入 `gs-actuator-service/initial` * 继续 [创建代表类](https://spring.io/guides/gs/actuator-service/#initial) 。 **完成后** ,您可以根据中的代码检查结果 `gs-actuator-service/complete`. ## 从Spring Initializr开始 如果您使用Maven,请访问 [Spring Initializr](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=actuator-service&name=actuator-service&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.actuator-service&dependencies=web,actuator) 以生成具有所需依赖项的新项目(Spring Web和Spring Boot Actuator)。 以下清单显示了 `pom.xml` 选择Maven时创建的文件: ~~~ <?xml version="1.0" encoding="UTF-8"?> <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 https://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>2.4.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>actuator-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>actuator-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ~~~ 如果使用Gradle,请访问 [Spring Initializr](https://start.spring.io/#!type=gradle-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=actuator-service&name=actuator-service&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.actuator-service&dependencies=web,actuator) 以生成具有所需依赖项的新项目(Spring Web和Spring Boot Actuator)。 以下清单显示了 `build.gradle`选择Gradle时创建的文件: ~~~ plugins { id 'org.springframework.boot' version '2.4.3' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() } ~~~ ### 手动初始化(可选) 如果要手动初始化项目而不是使用前面显示的链接,请按照以下步骤操作: 1. 导航到 [https://start.spring.io](https://start.spring.io) 。 该服务提取应用程序所需的所有依赖关系,并为您完成大部分设置。 2. 选择Gradle或Maven以及您要使用的语言。 本指南假定您选择了Java。 3. 单击 **Dependencies,** 然后选择 **Spring Web** 和 **Spring Boot Actuator** 。 4. 点击 **生成** 。 5. 下载生成的ZIP文件,该文件是使用您的选择配置的Web应用程序的存档。 如果您的IDE集成了Spring Initializr,则可以从IDE中完成此过程。 ## 运行空服务 Spring Initializr创建一个空的应用程序,您可以使用它来入门。 以下示例(摘自 `src/main/java/com/example/actuatorservice/ActuatorServiceApplication` 在里面 `initial` 目录)显示了Spring Initializr创建的类: ~~~ package com.example.actuatorservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ActuatorServiceApplication { public static void main(String[] args) { SpringApplication.run(ActuatorServiceApplication.class, args); } } ~~~ 这 `@SpringBootApplication`注释根据类路径的内容和其他内容提供了默认值(如嵌入式servlet容器)。 它还会打开Spring MVC的 `@EnableWebMvc` 注释,用于激活Web端点。 在此应用程序中没有定义终结点,但是有足够的空间来启动事物并查看Actuator的某些功能。 这 `SpringApplication.run()`该命令知道如何启动Web应用程序。 您所需要做的就是运行以下命令: ~~~ $ ./gradlew clean build && java -jar build/libs/gs-actuator-service-0.1.0.jar ~~~ 您尚未编写任何代码,这是怎么回事? 要查看答案,请等待服务器启动,打开另一个终端,然后尝试以下命令(及其输出显示): ~~~ $ curl localhost:8080 {"timestamp":1384788106983,"error":"Not Found","status":404,"message":""} ~~~ 前面命令的输出表明服务器正在运行,但是您尚未定义任何业务端点。 您会看到执行器发出的通用JSON响应,而不是默认的容器生成的HTML错误响应 `/error`端点。 您可以在服务器启动的控制台日志中看到开箱即用提供的端点。 您可以尝试其中的一些端点,包括 `/health`端点。 以下示例显示了如何执行此操作: ~~~ $ curl localhost:8080/actuator/health {"status":"UP"} ~~~ 状态为 `UP`,因此执行器服务正在运行。 有关 请参见Spring Boot的 [Actuator Project](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot-actuator) 更多详细信息, 。 ## 创建一个表示形式类 首先,您需要考虑一下API的外观。 您要处理GET请求 `/hello-world`,可以选择使用名称查询参数。 为了响应这样的请求,您想发送回表示问候的JSON,该JSON类似于以下内容: ~~~ { "id": 1, "content": "Hello, World!" } ~~~ 这 `id` 字段是问候语的唯一标识符,并且 `content` 包含问候语的文字表示。 要建模问候表示,请创建一个表示类。 以下清单(来自 `src/main/java/com/example/actuatorservice/Greeting.java`)显示 `Greeting` 班级: ~~~ package com.example.actuatorservice; public class Greeting { private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } } ~~~ 现在,您需要创建将用于表示形式类的终结点控制器。 ## 创建一个资源控制器 在Spring中,REST端点是Spring MVC控制器。 下面的Spring MVC控制器(来自 `src/main/java/com/example/actuatorservice/HelloWorldController.java`)处理GET请求, `/hello-world` 端点并返回 `Greeting` 资源: ~~~ package com.example.actuatorservice; import java.util.concurrent.atomic.AtomicLong; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloWorldController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); @GetMapping("/hello-world") @ResponseBody public Greeting sayHello(@RequestParam(name="name", required=false, defaultValue="Stranger") String name) { return new Greeting(counter.incrementAndGet(), String.format(template, name)); } } ~~~ 面向人的控制器和REST端点控制器之间的主要区别在于响应的创建方式。 端点控制器不依赖于视图(例如JSP)以HTML形式呈现模型数据,而是将要直接写入响应正文的数据返回。 这 [`@ResponseBody`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html)批注告诉Spring MVC不要将模型呈现到视图中,而是将返回的对象写到响应主体中。 它是通过使用Spring的消息转换器之一来实现的。 因为Jackson 2在类路径中, [`MappingJackson2HttpMessageConverter`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html) 将处理 `Greeting` 如果请求的是JSON对象 `Accept` 标头指定应返回JSON。 您怎么知道杰克逊2正在上课? 运行`mvndependency:tree`或者 ./gradlew dependencies,您将获得包含Jackson 2.x的详细的依赖关系树。 您还可以看到它来自 / spring-boot-starter-json ,它本身是由 导入的 spring-boot-starter-web 。 ## 运行应用程序 您可以从自定义主类或直接从配置类之一运行应用程序。 对于这个简单的示例,您可以使用 `SpringApplication`帮手类。 请注意,这是Spring Initializr为您创建的应用程序类,您甚至无需对其进行修改即可使其适用于此简单应用程序。 以下清单(来自 `src/main/java/com/example/actuatorservice/HelloWorldApplication.java`)显示了应用程序类: ~~~ package com.example.actuatorservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } } ~~~ 在传统的Spring MVC应用程序中,您将添加 `@EnableWebMvc` 打开关键行为,包括配置 `DispatcherServlet`。 但是当Spring Boot 检测到 时,它会自动打开此注释 **spring-webmvc** 在您的类路径上 。 这使您可以在接下来的步骤中构建控制器。 这 `@SpringBootApplication` 注释也带来了 [`@ComponentScan`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html) 批注,告诉Spring扫描 `com.example.actuatorservice` 这些控制器的软件包(以及任何其他带注释的组件类)。 ## 建立可执行的JAR 您可以使用Gradle或Maven从命令行运行该应用程序。 您还可以构建一个包含所有必需的依赖项,类和资源的可执行JAR文件,然后运行该文件。 生成可执行jar使得在整个开发生命周期中,跨不同环境等等的情况下,都可以轻松地将服务作为应用程序进行发布,版本控制和部署。 如果您使用Gradle,则可以通过使用以下命令运行该应用程序 `./gradlew bootRun`。 或者,您可以通过使用以下命令构建JAR文件: `./gradlew build` 然后运行JAR文件,如下所示: ~~~ java -jar build/libs/gs-actuator-service-0.1.0.jar ~~~ 如果您使用Maven,则可以通过使用以下命令运行该应用程序 `./mvnw spring-boot:run`。 或者,您可以使用以下命令构建JAR文件: `./mvnw clean package` 然后运行JAR文件,如下所示: ~~~ java -jar target/gs-actuator-service-0.1.0.jar ~~~ 此处描述的步骤将创建可运行的JAR。 您还可以 构建经典的WAR文件 。 服务运行后(因为您已运行 `spring-boot:run` (在终端中),您可以通过在单独的终端中运行以下命令来对其进行测试: ~~~ $ curl localhost:8080/hello-world {"id":1,"content":"Hello, Stranger!"} ~~~ ## 切换到其他服务器端口 Spring Boot Actuator默认在端口8080上运行。 `application.properties`文件,您可以覆盖该设置。 以下清单(来自 `src/main/resources/application.properties`)显示了具有必要更改的文件: ~~~ server.port: 9000 management.server.port: 9001 management.server.address: 127.0.0.1 ~~~ 通过在终端中运行以下命令来再次运行服务器: ~~~ $ ./gradlew clean build && java -jar build/libs/gs-actuator-service-0.1.0.jar ~~~ 现在,该服务在端口9000上启动。 您可以通过在终端中运行以下命令来测试它是否在端口9000上运行: ~~~ $ curl localhost:8080/hello-world curl: (52) Empty reply from server $ curl localhost:9000/hello-world {"id":1,"content":"Hello, Stranger!"} $ curl localhost:9001/actuator/health {"status":"UP"} ~~~ ## 测试您的应用程序 要检查您的应用程序是否正常工作,您应该为应用程序编写单元测试和集成测试。 中的考试班 `src/test/java/com/example/actuatorservice/HelloWorldApplicationTests.java` 确保 * 您的控制器反应灵敏。 * 您的管理端点是响应性的。 请注意,测试会在随机端口上启动应用程序。 以下清单显示了测试类: ~~~ /* * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.actuatorservice; import java.util.Map; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.TestPropertySource; import static org.assertj.core.api.BDDAssertions.then; /** * Basic integration tests for service demo application. * * @author Dave Syer */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @TestPropertySource(properties = {"management.port=0"}) public class HelloWorldApplicationTests { @LocalServerPort private int port; @Value("${local.management.port}") private int mgt; @Autowired private TestRestTemplate testRestTemplate; @Test public void shouldReturn200WhenSendingRequestToController() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = this.testRestTemplate.getForEntity( "http://localhost:" + this.port + "/hello-world", Map.class); then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } @Test public void shouldReturn200WhenSendingRequestToManagementEndpoint() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = this.testRestTemplate.getForEntity( "http://localhost:" + this.mgt + "/actuator/info", Map.class); then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } } ~~~ ## 概括 恭喜你! 您刚刚使用Spring开发了一个简单的RESTful服务,并且使用Spring Boot Actuator添加了一些有用的内置服务。