SpringBoot入門の最終回

添加实力派功能

(本番是正式的,真正的意思,不过直译很奇怪.グレード是等级的意思.サービス是服务的意思,之前也一直这么翻译,但是一直觉得别扭,从这里开始翻译成功能.)

创建商业用的web网站的话,恐怕有必要添加若干个管理功能.SpringBoot使用执行器模块(actuator module),提供了若干个这样的功能(健康检查,监控,Bean等).

使用Gradle 的话,在build.gradle文件里添加以下依赖.

implementation 'org.springframework.boot:spring-boot-starter-actuator'

使用Maven的话,在pom.xml文件里添加以下依赖.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

接着,重启应用程序.使用Gradle的话,在终端窗口(complete 文件夹内)执行以下命令.

./gradlew bootRun

使用Maven的话,在终端窗口(complete 文件夹内)执行以下命令.

./mvnw spring-boot:run

RESTful endpoint的新装配知道在应用程序里添加.这些是SpringBoot提供的管理功能.下面显示的就是典型的输出代码.

management.endpoint.configprops-org.springframework.boot.actuate.autoconfigure.context.properties.ConfigurationPropertiesReportEndpointProperties
management.endpoint.env-org.springframework.boot.actuate.autoconfigure.env.EnvironmentEndpointProperties
management.endpoint.health-org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties
management.endpoint.logfile-org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointProperties
management.endpoints.jmx-org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointProperties
management.endpoints.web-org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties
management.endpoints.web.cors-org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties
management.health.status-org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorProperties
management.info-org.springframework.boot.actuate.autoconfigure.info.InfoContributorProperties
management.metrics-org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties
management.metrics.export.simple-org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleProperties
management.server-org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties
management.trace.http-org.springframework.boot.actuate.autoconfigure.trace.http.HttpTraceProperties

执行器公开如下.

  • 执行器/健康检查
  • 执行器/信息
  • 执行器
`/actuator/shutdown`也有endpoint,默认,显示只通过JMX.要使HTTP endpoint有效,在`application.properties`里添加`management.endpoint.shutdown.enabled=true`,在`management.endpoints.web.exposure.include=health,info,shutdown`公开.但是,请不要把被公开的应用程序的shutdown endpoint设置为有效.

执行以下命令,可以确认应用程序是否正常运作.

$ curl localhost:8080/actuator/health
{"status":"UP"}

另外,使用curl调用shutdown,没有在application.properties 上添加所需的行(请参考刚才的解释)的话,还可以确认会发生什么.

$ curl -X POST localhost:8080/actuator/shutdown
{"timestamp":1401820343710,"error":"Not Found","status":404,"message":"","path":"/actuator/shutdown"}

由于没有设置有效,被请求的endpoint不能使用(因为endpoint不存在).

关于这些各个REST endpoint的详细信息以及application.properties文件设置调整的方法请参考endpoint相关文档.

SpringBoot的初试牛刀

已经看了一部分SpringBoot的初始阶段内容了.这些在源码:GitHub全部都可以确认.

支持JAR也支持Groovy

最后献上一例,使用SpringBoot,展示一下如何连接到原本需要却有可能没有注意到的Bean的方法.另外,展示一下使便利的管理功能奏效的方法.

但是,SpringBoot远不止如此.不是只有向来的WAR包部署,有了SpringBoot的下载模块还可以集中可执行的JAR包.各种各样的解释展示了根据spring-boot-gradle-pluginspring-boot-maven-plugin的双重支持.

并且,SpringBoot还支持Groovy,仅凭一个文件就能创建SpringMVC Web应用程序.

创建一个app.groovy的新文件,配置如下代码.

@RestController
class ThisWillActuallyRun {

    @RequestMapping("/")
    String home() {
        return "Hello, World!"
    }

}
文件路径不重要.连在单一的推文内都可以get到小的应用程序.

接下来,安装SpringBoot CLI.

执行如下命令,再执行Groovy应用程序.

$ spring run app.groovy
为了避免端口冲突,请卸载之前的应用程序.

再开一个终端窗口,执行如下curl命令.(同时显示输出)

$ curl localhost:8080
Hello, World!

SpringBoot会把注解主动添加到代码里,使用Groovy Grape依赖管理器执行应用会拉下所需的library执行.

总结

恭喜你!使用SpringBoot创建了简单的Web应用程序,get到了开发基础的晋级方法.另外,方便的切实可用的功能也上轨了.这是使用SpringBoot所能做的其中很小的一个样例而已.更详细的介绍,请参考SpringBoot的官方文档.

本番グレードのサービスを追加する

ビジネス用の Web サイトを構築している場合、おそらくいくつかの管理サービスを追加する必要があります。Spring Boot は、アクチュエーターモジュールを使用して、そのようなサービス(ヘルス、監査、Bean など)をいくつか提供します。

Gradle を使用する場合は、build.gradle ファイルに次の依存関係を追加します。

implementation 'org.springframework.boot:spring-boot-starter-actuator'COPY

Maven を使用する場合は、pom.xml ファイルに次の依存関係を追加します。

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>COPY

次に、アプリケーションを再起動します。Gradle を使用する場合は、ターミナルウィンドウ(complete ディレクトリ内)で次のコマンドを実行します。

./gradlew bootRun

Maven を使用する場合は、ターミナルウィンドウ(complete ディレクトリ内)で次のコマンドを実行します。

./mvnw spring-boot:run

RESTful エンドポイントの新しいセットがアプリケーションに追加されていることがわかります。これらは、Spring Boot が提供する管理サービスです。次のリストは、典型的な出力を示しています。

management.endpoint.configprops-org.springframework.boot.actuate.autoconfigure.context.properties.ConfigurationPropertiesReportEndpointProperties
management.endpoint.env-org.springframework.boot.actuate.autoconfigure.env.EnvironmentEndpointProperties
management.endpoint.health-org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties
management.endpoint.logfile-org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointProperties
management.endpoints.jmx-org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointProperties
management.endpoints.web-org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties
management.endpoints.web.cors-org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties
management.health.status-org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorProperties
management.info-org.springframework.boot.actuate.autoconfigure.info.InfoContributorProperties
management.metrics-org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties
management.metrics.export.simple-org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleProperties
management.server-org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties
management.trace.http-org.springframework.boot.actuate.autoconfigure.trace.http.HttpTracePropertiesCOPY

アクチュエーターは次を公開します。

/actuator/shutdown エンドポイントもありますが、デフォルトでは、JMX を介してのみ表示されます。HTTP エンドポイントとして有効にするには、 management.endpoint.shutdown.enabled=trueapplication.properties ファイルに追加し、management.endpoints.web.exposure.include=health,info,shutdown で公開します。ただし、公開されているアプリケーションのシャットダウンエンドポイントを有効にしないでください。

次のコマンドを実行して、アプリケーションの正常性を確認できます。

$ curl localhost:8080/actuator/health
{"status":"UP"}COPY

また、curl を使用してシャットダウンを呼び出して、application.properties に必要な行(前の注を参照)を追加していない場合に何が起こるかを確認することもできます。

$ curl -X POST localhost:8080/actuator/shutdown
{"timestamp":1401820343710,"error":"Not Found","status":404,"message":"","path":"/actuator/shutdown"}COPY

有効にしなかったため、リクエストされたエンドポイントは使用できません(エンドポイントが存在しないため)。

これらの各 REST エンドポイントの詳細および application.properties ファイル(src/main/resources 内)で設定を調整する方法については、エンドポイントに関するドキュメントを参照してください。

Spring Boot のスターターを見る

Spring Boot の「スターター」の一部を見てきました。これらはすべてソースコード: GitHub (英語) で確認できます。

JAR サポートおよび Groovy サポート

最後の例では、Spring Boot を使用して、必要なことに気付かない可能性のある Bean をワイヤリングする方法を示しました。また、便利な管理サービスを有効にする方法も示しました。

ただし、Spring Boot はそれ以上のことを行います。従来の WAR ファイルデプロイだけでなく、Spring Boot のローダーモジュールのおかげで実行可能 JAR をまとめることもできます。さまざまなガイドが、spring-boot-gradle-plugin および spring-boot-maven-plugin によるこの二重サポートを示しています。

その上、Spring Boot は Groovy もサポートしており、わずか 1 つのファイルで Spring MVC Web アプリケーションを構築できます。

app.groovy という新しいファイルを作成し、次のコードをその中に配置します。

@RestController
class ThisWillActuallyRun {

    @RequestMapping("/")
    String home() {
        return "Hello, World!"
    }

}COPY
ファイルの場所は関係ありません。単一のツイート (英語) 内に小さなアプリケーションを収めることさえできます!

次に、Spring Boot の CLI をインストールします

次のコマンドを実行して、Groovy アプリケーションを実行します。

$ spring run app.groovyCOPY
ポートの衝突を避けるために、以前のアプリケーションをシャットダウンします。

別のターミナルウィンドウから、次の curl コマンドを実行します(出力とともに表示)。

$ curl localhost:8080
Hello, World!COPY

Spring Boot は、キーアノテーションをコードに動的に追加し、Groovy グレープ (英語) を使用してアプリの実行に必要なライブラリをプルダウンすることでこれを行います。

要約

おめでとう! Spring Boot を使用して簡単な Web アプリケーションを構築し、開発ペースを向上させる方法を学びました。また、便利な本番サービスもオンにしました。これは、Spring Boot でできることのほんの小さなサンプリングです。詳細については、Spring Boot のオンラインドキュメントを参照してください。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值