文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
19. Running your application
One of the biggest advantages of packaging your application as jar and using an embedded HTTP server is that you can run your application as you would any other. Debugging Spring Boot applications is also easy; you don’t need any special IDE plugins or extensions.
将应用打包成jar并使用内嵌HTTP服务器的一个最大优势是你可以在任何地方运行你的程序。调试Spring Boot应用也很容易;你不必指定任何特定的IDE插件或扩展。
This section only covers jar based packaging, If you choose to package your application as a war file you should refer to your server and IDE documentation.
这一节只包含基于jar的打包,如果你想选择将你的应用打包成war文件,你应该参考你的服务器和IDE文档。
19.1 Running from an IDE
You can run a Spring Boot application from your IDE as a simple Java application, however, first you will need to import your project. Import steps will vary depending on your IDE and build system. Most IDEs can import Maven projects directly, for example Eclipse users can select Import…
→ Existing Maven Projects
from the File
menu.
你可以在你的IDE中运行一个Spring Boot应用将像运行一个简单的Java应用一样,然而,首先你需要导入你的工程。导入步骤根据你的IDE和构建系统会有所变化。大多数IDE可以直接导入Maven工程,例如Eclipse用户可以选择从File
菜单选择Import…
→ Existing Maven Projects
。
If you can’t directly import your project into your IDE, you may be able to generate IDE metadata using a build plugin. Maven includes plugins for Eclipse and IDEA; Gradle offers plugins for various IDEs.
如果你不能直接将工程导入你的IDE中,你可以使用构建插件生成一个IDE元数据。Maven中包含Eclipse和IDEA的插件;Gradle有各种IDEs的插件。
If you accidentally run a web application twice you will see a “Port already in use” error. STS users can use the
Relaunch
button rather thanRun
to ensure that any existing instance is closed.
如果你偶然的运行一个web应用两次,你会看到一个
Port already in use
错误。为了确保任何现有的实例被关闭,STS用户可以使用Relaunch
按钮而不是Run
按钮。
19.2 Running as a packaged application
If you use the Spring Boot Maven or Gradle plugins to create an executable jar you can run your application using java -jar
. For example:
如果你使用Spring Boot的Maven或Gradle插件来创建一个可执行的jar包,你可以通过使用java -jar
来运行你的应用。例如:
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar
It is also possible to run a packaged application with remote debugging support enabled. This allows you to attach a debugger to your packaged application:
它也支持以远程调试模式运行一个打包的应用。这允许你在打包的应用中添加一个调试器:
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar target/myproject-0.0.1-SNAPSHOT.jar
19.3 Using the Maven plugin
The Spring Boot Maven plugin includes a run
goal which can be used to quickly compile and run your application. Applications run in an exploded form just like in your IDE.
Spring Boot Maven插件包含一个run
目标,可以用来快速编译并运行你的应用。应用以exploded形式运行,就像在IDE中运行一样。
$ mvn spring-boot:run
You might also want to use the useful operating system environment variable:
你也可以使用有用的操作系统环境变量:
$ export MAVEN_OPTS=-Xmx1024m -XX:MaxPermSize=128M
19.4 Using the Gradle plugin
The Spring Boot Gradle plugin also includes a bootRun
task which can be used to run your application in an exploded form. The bootRun
task is added whenever you import the spring-boot-gradle-plugin
:
Spring Boot Gradle插件也包含一个bootRun
任务,它可以以exploded方式运行你的应用。当你导入spring-boot-gradle-plugin
时,可以添加bootRun
任务:
$ gradle bootRun
You might also want to use this useful operating system environment variable:
你也可以使用有用的操作系统环境变量:
$ export JAVA_OPTS=-Xmx1024m -XX:MaxPermSize=128M
19.5 Hot swapping
Since Spring Boot applications are just plain Java applications, JVM hot-swapping should work out of the box. JVM hot swapping is somewhat limited with the bytecode that it can replace, for a more complete solution JRebel or the Spring Loaded project can be used. The spring-boot-devtools
module also includes support for quick application restarts.
由于Spring Boot应用只是普通的Java应用,JVM热交换应该是开箱即用的。JVM热交换可以替换的字节码有限制,一个更全面的解决方案是JRebel或Spring Loaded工程。spring-boot-devtools
模块也支持快速的应用重启。
See the Chapter 20, Developer tools section below and the Hot swapping “How-to” for details.
更多细节请看20章,下面的开发者工具部分和怎样热交换部分。
20. Developer tools
Spring Boot includes an additional set of tools that can make the application development experience a little more pleasant. The spring-boot-devtools
module can be included in any project to provide additional development-time features. To include devtools support, simply add the module dependency to your build:
Spring Boot包含额外的工具集合,可以使应用开发的过程更方便一点。spring-boot-devtools
模块可以包含进任何工程,用来提供额外的程序调试特性。为了添加工具支持,简单的添加模块依赖到你的构建系统中:
Maven.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
Gradle.
dependencies {
compile("org.springframework.boot:spring-boot-devtools")
}
Developer tools are automatically disabled when running a fully packaged application. If your application is launched using
java -jar
or if it’s started using a special classloader, then it is considered a “production application”. Flagging the dependency asoptional
is a best practice that prevents devtools from being transitively applied to other modules using your project. Gradle does not supportoptional
dependencies out-of-the-box so you may want to have a look to the propdeps-plugin in the meantime.
当运行一个完整的打包应用时,开发者工具会自动失效。如果你的应用是通过
java -jar
启动的,或者是通过一个特定的类加载器启动的,那它会被当做一个『产品级应用』。将依赖标记为optional
是阻止开发工具间接应用到使用你应用的其它模块的最佳实践。Gradle不支持optional
依赖开箱即用,同时你可能想看看propdeps-plugin。
repackaged archives do not contain devtools by default. If you want to use certain remote devtools feature, you’ll need to enable the
excludeDevtools
build property to include it. The property is supported with both the Maven and Gradle plugins.
再打包文件默认不包含开发者工具。如果你想使用某些远程开发者工具特性,你需要让
excludeDevtools
构建属性包含它。Maven和Gradle插件都支持这个属性。
20.2 Automatic restart
Applications that use spring-boot-devtools
will automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a folder will be monitored for changes. Note that certain resources such as static assets and view templates do not need to restart the application.
当classpath
中的文件修改时,使用spring-boot-devtools
的应用会自动重启。当使用IDE开发时,这是一个很有用的功能,因为代码改变时它能快速的进行反馈。默认情况下,会监控classpath
指向的文件夹中任何条目的变化。注意某些资源例如静态资源和视图模板不需要重启应用。
Triggering a restart
As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using. In Eclipse, saving a modified file will cause the classpath to be updated and trigger a restart. In IntelliJ IDEA, building the project (
Build → Make Project
) will have the same effect.
触发重启
作为
DevTools
监视器classpath
中的资源,触发重启的唯一方式是更新classpath
。引起classpath
更新的方式取决于你使用的IDE。在Eclipse中,保存一个修改的文件将引起classpath
更新并触发重启事件。在IntelliJ IDEA中,构建工程(Build → Make Project
)将会有同样的效果。
You can also start your application via the supported build plugins (i.e. Maven and Gradle) as long as forking is enabled since DevTools need an isolated application classloader to operate properly. Gradle and Maven do that by default when they detect DevTools on the classpath.
只要启用了分支,你也可以通过支持的构建系统启动你的应用(例如,Maven和Gradle),因为
DevTools
需要一个独立的应用类加载器来进行正确操作。当Gradle和Maven在classpath
中检测到DevTools
时,它们默认的进行了那个工作。
Automatic restart works very well when used with
LiveReload
. See below for details. If you use JRebel automatic restarts will be disabled in favor of dynamic class reloading. Other devtools features (such asLiveReload
and property overrides) can still be used.
当使用
LiveReload
,自动重启能很好的工作。更多细节请看下面。如果你使用JRebel
进行自动重启,将不支持动态的类重加载。其它的开发者工具功能(例如LiveReload
和属性覆写)仍能继续使用。
DevTools relies on the application context’s shutdown hook to close it during a restart. It will not work correctly if you have disabled the shutdown hook (
SpringApplication.setRegisterShutdownHook(false)
).
DevTools
依赖应用上下文关闭钩子来进行重启期间的关闭。如果你禁用了关闭钩子它将不能正确工作(SpringApplication.setRegisterShutdownHook(false)
)。
When deciding if an entry on the classpath should trigger a restart when it changes, DevTools automatically ignores projects named
spring-boot
,spring-boot-devtools
,spring-boot-autoconfigure
,spring-boot-actuator
, andspring-boot-starter
.
当决定
classpath
中输入引起的改变是否应该触发重启时,DevTools
会自动忽略命名为spring-boot
,spring-boot-devtools
,spring-boot-autoconfigure
,spring-boot-actuator
和spring-boot-starter
的工程。
Restart vs Reload
The restart technology provided by Spring Boot works by using two classloaders. Classes that don’t change (for example, those from third-party jars) are loaded into a base classloader. Classes that you’re actively developing are loaded into a restart classloader. When the application is restarted, the restart classloader is thrown away and a new one is created. This approach means that application restarts are typically much faster than “cold starts” since the base classloader is already available and populated.
If you find that restarts aren’t quick enough for your applications, or you encounter classloading issues, you could consider reloading technologies such as JRebel from ZeroTurnaround. These work by rewriting classes as they are loaded to make them more amenable to reloading. Spring Loaded provides another option, however it doesn’t support as many frameworks and it isn’t commercially supported.
重启与重载(重新加载)
Spring Boot提供的重启技术是通过两个类加载器进行工作的。加载进基类加载器的类不能改变(例如,那些第三方jar包)。那些你正在开发的类加载进重启类加载器中。当应用重启时,丢弃旧的重启类加载器并创建一个新的。这种方法意味着应用重启时比『冷启动』更快,因为基类加载器已经存在并可用。
如果你发现你的应用重启不够快,或者你碰到了类加载问题,你可以考虑重载技术例如ZeroTurnaround的JRebel。这些工作通过加载时类的重写使它们更适合重载。Spring加载提供了另一种选择,但许多框架都不支持,也不支持商业化。
20.2.1 Excluding resources
Certain resources don’t necessarily need to trigger a restart when they are changed. For example, Thymeleaf templates can just be edited in-place. By default changing resources in /META-INF/maven
, /META-INF/resources
, /resources
, /static
, /public
or /templates
will not trigger a restart but will trigger a live reload. If you want to customize these exclusions you can use the spring.devtools.restart.exclude
property. For example, to exclude only /static
and /public
you would set the following:
某些资源当它们改变时不一定需要触发重启。例如,Thymeleaf模板可以就地编辑。默认情况下,/META-INF/maven
,/META-INF/resources
,/resources
,/static
,/public
或/templates
中资源的改变不会触发重启,但会触发实时重载。如果你想定制这些排除项,你可以使用spring.devtools.restart.exclude
属性。例如,仅排除/static
和/public
,设置如下:
spring.devtools.restart.exclude=static/**,public/**
If you want to keep those defaults and add additional exclusions, use the
spring.devtools.restart.additional-exclude
property instead.
如果你想保持这些默认,添加额外的排除项,可以使用
spring.devtools.restart.additional-exclude
属性。
20.2.2 Watching additional paths
You may want your application to be restarted or reloaded when you make changes to files that are not on the classpath. To do so, use the spring.devtools.restart.additional-paths
property to configure additional paths to watch for changes. You can use the spring.devtools.restart.exclude
property described above to control whether changes beneath the additional paths will trigger a full restart or just a live reload.
当你修改不在classpath
中的文件时,你可能也想重启或重载你的应用。为了这样做,可以使用spring.devtools.restart.additional-paths
属性来监控其它路径上的变化。你可以使用前面描述的spring.devtools.restart.exclude
属性来控制其它路径上的变化是否会触发重启或仅触发实时重载。
20.2.3 Disabling restart
If you don’t want to use the restart feature you can disable it using the spring.devtools.restart.enabled
property. In most cases you can set this in your application.properties
(this will still initialize the restart classloader but it won’t watch for file changes).
如果你想使用重启功能你可以通过spring.devtools.restart.enabled
属性禁用它。大多数情况下你可以在你的application.properties
中设置它(这仍会初始化重启类加载器但它不会监控文件的变化)。
If you need to completely disable restart support, for example, because it doesn’t work with a specific library, you need to set a System
property before calling SpringApplication.run(…)
. For example:
如果你需要完整的禁用重启支持,例如,因为它不能与一个特定的库一起工作,你需要在调用SpringApplication.run(…)
之前设置一个System
属性。例如:
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(MyApp.class, args);
}
20.2.4 Using a trigger file
If you work with an IDE that continuously compiles changed files, you might prefer to trigger restarts only at specific times. To do this you can use a “trigger file”, which is a special file that must be modified when you want to actually trigger a restart check. Changing the file only triggers the check and the restart will only occur if Devtools has detected it has to do something. The trigger file could be updated manually, or via an IDE plugin.
如果你的IDE可以持续编译变化的文件,你可以只在特定的时间触发重启。为了实现这个功能你可以使用『触发器文件』,当你需要真正触发重启检查时,它是一个你必须要修改文件。修改这个文件只触发重启检查,只有Devtools
检测到它必须要做些事情时,重启才会发生。触发器文件应该进行手动更新,或通过IDE插件更新。
To use a trigger file use the spring.devtools.restart.trigger-file
property.
为了使用触发器文件需要使用spring.devtools.restart.trigger-file
属性。
You might want to set
spring.devtools.restart.trigger-file
as a global setting so that all your projects behave in the same way.
为了使你所有的工程都表现一样,你需要将
spring.devtools.restart.trigger-file
设置为全局设置。
20.2.5 Customizing the restart classloader
As described in the Restart vs Reload section above, restart functionality is implemented by using two classloaders. For most applications this approach works well, however, sometimes it can cause classloading issues.
像前面的的Restart vs Reload
部分所讲的那样,重启功能是通过两个类加载器实现的。对于大多数应用来说这个方法能很好的工作,但是有时候它也会引起一些类加载问题。
By default, any open project in your IDE will be loaded using the “restart” classloader, and any regular .jar
file will be loaded using the “base” classloader. If you work on a multi-module project, and not each module is imported into your IDE, you may need to customize things. To do this you can create a META-INF/spring-devtools.properties
file.
默认情况下,IDE中的任何开放的工程都会使用“restart”类加载器进行加载,任何规范的.jar
文件都会使用“base”类加载器进行加载。如果你在一个多模块工程中工作,不是每一个模块都导入到你的IDE中,你可能需要定制一些东西。为了实现这个功能你可以创建一个META-INF/spring-devtools.properties
文件。
The spring-devtools.properties
file can contain restart.exclude.
and restart.include.
prefixed properties. The include
elements are items that should be pulled up into the “restart” classloader, and the exclude
elements are items that should be pushed down into the “base” classloader. The value of the property is a regex pattern that will be applied to the classpath.
spring-devtools.properties
可以包含restart.exclude.
和restart.include.
前缀属性。include
元素应该拉进“restart”类加载器中,exclude
元素应该推入到“base”类加载器中。属性值是应用到classpath
中的一个正则表达式。
For example:
例如:
restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar
All property keys must be unique. As long as a property starts with
restart.include.
orrestart.exclude
. it will be considered.
所有属性的键必须是唯一的。只要
restart.include.
或restart.exclude
作为一个属性启动,它就被应用。
All
META-INF/spring-devtools.properties
from the classpath will be loaded. You can package files inside your project, or in the libraries that the project consumes.
classpath
中的所有META-INF/spring-devtools.properties
都会被加载。你可以将文件打包进你的工程中,或这个工程使用的库中。
20.2.6 Known limitations
Restart functionality does not work well with objects that are deserialized using a standard ObjectInputStream
. If you need to deserialize data, you may need to use Spring’s ConfigurableObjectInputStream
in combination with Thread.currentThread().getContextClassLoader()
.
当存在使用标准ObjectInputStream
进行反序列化的对象时,重启功能不能很好的工作。如果你需要反序列化数据,你可能需要使用Spring的ConfigurableObjectInputStream
和Thread.currentThread().getContextClassLoader()
。
Unfortunately, several third-party libraries deserialize without considering the context classloader. If you find such a problem, you will need to request a fix with the original authors.
遗憾的是,一些第三方库反序列化时没有考虑上下文类加载器。如果你发现了这样的问题,你需要请求原作者进行修正。
20.3 LiveReload
The spring-boot-devtools
module includes an embedded LiveReload server that can be used to trigger a browser refresh when a resource is changed. LiveReload browser extensions are freely available for Chrome, Firefox and Safari from livereload.com.
spring-boot-devtools
模块包含一个内嵌的实时重载服务器,当资源改变时可以用来触发浏览器重新刷新。实时重载浏览器扩展对于livereload.com中的Chrome,Firefox和Safari而言是可用的。
If you don’t want to start the LiveReload server when your application runs you can set the spring.devtools.livereload.enabled
property to false
.
当你的应用运行时,如果你不想启动实时重载服务器,你可以将spring.devtools.livereload.enabled
属性设为false
。
You can only run one LiveReload server at a time. Before starting your application, ensure that no other LiveReload servers are running. If you start multiple applications from your IDE, only the first will have LiveReload support.
一次你只可以运行一个实时重载服务器。在启动你的应用之前,确保没有其它的实时重载服务器在运行。如果你从你的IDE中启动多个应用,只有第一个应用有实时重载服务器支持。
20.4 Global settings
You can configure global devtools settings by adding a file named .spring-boot-devtools.properties
to your $HOME
folder (note that the filename starts with “.”). Any properties added to this file will apply to all Spring Boot applications on your machine that use devtools. For example, to configure restart to always use a trigger file, you would add the following:
通过添加一个名为.spring-boot-devtools.properties
的文件到你的$HOME
文件夹中(注意文件名以.
开头),你可以配置全局开发者工具设置。任何添加到这个文件的属性都将应用到你机器上所有使用开发者工具的Spring Boot应用中。例如,为了配置重启时总是使用一个触发器文件,你需要添加如下内容:
~/.spring-boot-devtools.properties.
spring.devtools.reload.trigger-file=.reloadtrigger
20.5 Remote applications
The Spring Boot developer tools are not just limited to local development. You can also use several features when running applications remotely. Remote support is opt-in, to enable it you need to make sure that devtools
is included in the repackaged archive:
Spring Boot开发者工具是不受本地环境限制的,在运行远程应用时你也可以使用一些功能。远程支持是选择性加入的,为了使它起作用你需要确保devtools
包含在再打包的文件中:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>
Then you need to set a spring.devtools.remote.secret
property, for example:
然后你需要设置spring.devtools.remote.secret
属性,例如:
spring.devtools.remote.secret=mysecret
Enabling
spring-boot-devtools
on a remote application is a security risk. You should never enable support on a production deployment.
使
spring-boot-devtools
在远程应用上起作用是有安全风险的。在产品部署时,你不应该让它支持spring-boot-devtools
。
Remote devtools support is provided in two parts; there is a server side endpoint that accepts connections, and a client application that you run in your IDE. The server component is automatically enabled when the spring.devtools.remote.secret
property is set. The client component must be launched manually.
远程devtools
需要两部分提供支持:一个接收连接的服务器端,一个运行在IDE中的客户端应用。当设置spring.devtools.remote.secret
属性时,服务器组件会自动起作用。客户端组件必须手动启动。
20.5.1 Running the remote client application
The remote client application is designed to be run from within you IDE. You need to run org.springframework.boot.devtools.RemoteSpringApplication
using the same classpath as the remote project that you’re connecting to. The non-option argument passed to the application should be the remote URL that you are connecting to.
远程客户端应用设计要在你的IDE中运行。你需要运行org.springframework.boot.devtools.RemoteSpringApplication
,与要连接的远程工程要使用同样的classpath。传给应用的非可选参数应该是你要连接的远程URL。
For example, if you are using Eclipse or STS, and you have a project named my-app
that you’ve deployed to Cloud Foundry, you would do the following:
Select
Run Configurations…
from theRun
menu.Create a new
Java Application
“launch configuration”.Browse for the
my-app
project.Use
org.springframework.boot.devtools.RemoteSpringApplication
as the main class.Add
https://myapp.cfapps.io
to theProgram arguments
(or whatever your remote URL is).
例如,如果你在使用Eclipse或STS,你有一个名为my-app
的工程,它已经部署到了云计算中,你需要按照下面的步骤去做:
从
Run
菜单中选择Run Configurations…
。创建一个新的
Java Application
“launch configuration”。浏览
my-app
工程。使用
org.springframework.boot.devtools.RemoteSpringApplication
作为主类。添加
https://myapp.cfapps.io
到Program arguments
(或无论你的远程URL是什么)。
A running remote client will look like this:
一个运行的远程客户端如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \
\\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) )
' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / /
=========|_|==============|___/===================================/_/_/_/
:: Spring Boot Remote :: 2.0.0.BUILD-SNAPSHOT
2015-06-10 18:25:06.632 INFO 14938 --- [ main] o.s.b.devtools.RemoteSpringApplication : Starting RemoteSpringApplication on pwmbp with PID 14938 (/Users/pwebb/projects/spring-boot/code/spring-boot-devtools/target/classes started by pwebb in /Users/pwebb/projects/spring-boot/code/spring-boot-samples/spring-boot-sample-devtools)
2015-06-10 18:25:06.671 INFO 14938 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a17b7b6: startup date [Wed Jun 10 18:25:06 PDT 2015]; root of context hierarchy
2015-06-10 18:25:07.043 WARN 14938 --- [ main] o.s.b.d.r.c.RemoteClientConfiguration : The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2015-06-10 18:25:07.074 INFO 14938 --- [ main] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2015-06-10 18:25:07.130 INFO 14938 --- [ main] o.s.b.devtools.RemoteSpringApplication : Started RemoteSpringApplication in 0.74 seconds (JVM running for 1.105)
Because the remote client is using the same classpath as the real application it can directly read application properties. This is how the
spring.devtools.remote.secret
property is read and passed to the server for authentication.
因为远程客户端与真实的应用使用的
classpath
相同,因此它可以直接读取应用属性。这就是spring.devtools.remote.secret
属性是怎样读取并传递给服务器进行授权的。
It’s always advisable to use
https://
as the connection protocol so that traffic is encrypted and passwords cannot be intercepted.
为了传输可以加密,密码不能被解析,建议总是使用
https://
作为连接协议。
If you need to use a proxy to access the remote application, configure the
spring.devtools.remote.proxy.host
andspring.devtools.remote.proxy.port
properties.
如果你需要使用代理访问远程应用,配置
spring.devtools.remote.proxy.host
和spring.devtools.remote.proxy.port
属性。
20.5.2 Remote update
The remote client will monitor your application classpath for changes in the same way as the local restart. Any updated resource will be pushed to the remote application and (if required) trigger a restart. This can be quite helpful if you are iterating on a feature that uses a cloud service that you don’t have locally. Generally remote updates and restarts are much quicker than a full rebuild and deploy cycle.
远程客户端会像本地重启那样监控你应用的classpath
的变化。任何资源的更新都会推送到远程应用并(如果需要的话)触发重启。如果你在迭代一个本地没有的使用云服务的功能,它是非常有帮助的。通常更新和重启比整个重新构建部署更快。
Files are only monitored when the remote client is running. If you change a file before starting the remote client, it won’t be pushed to the remote server.
当远程客户端运行时只监控文件。如果在启动远程客户端之前你修改了文件,它将不会推送到远程服务器。
20.5.3 Remote debug tunnel
Java remote debugging is useful when diagnosing issues on a remote application. Unfortunately, it’s not always possible to enable remote debugging when your application is deployed outside of your data center. Remote debugging can also be tricky to setup if you are using a container based technology such as Docker.
当在远程应用上分析问题时,Java远程调试是非常有用的。遗憾的是,当你的应用部署离开你的数据中心时,远程调试并不总是能用的。如果你正在使用一个基于容器的技术例如Docker,要设置远程调试也是非常棘手的。
To help work around these limitations, devtools supports tunneling of remote debug traffic over HTTP. The remote client provides a local server on port 8000
that you can attach a remote debugger to. Once a connection is established, debug traffic is sent over HTTP to the remote application. You can use the spring.devtools.remote.debug.local-port
property if you want to use a different port.
为了帮助解决这些限制,devtools
支持在HTTP协议上的远程调试通道。远程客户端提供一个端口为8000
的本地服务器,你可以在这上面添加一个远程调试器。一旦建立了连接,调试通道将会在HTTP上发送到远程应用上。如果你想使用一个不同的端口,你可以使用spring.devtools.remote.debug.local-port
属性。
You’ll need to ensure that your remote application is started with remote debugging enabled. Often this can be achieved by configuring JAVA_OPTS
. For example, with Cloud Foundry you can add the following to your manifest.yml
:
你需要确保你的远程应用启动了并且远程调试可用。这经常可以通过配置JAVA_OPTS
来完成。例如,在云计算平台你可以添加下面的内容到你的manifest.yml
中:
---
env:
JAVA_OPTS: "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n"
Notice that you don’t need to pass an
address=NNNN
option to-Xrunjdwp
. If omitted Java will simply pick a random free port.
注意你不必向
-Xrunjdwp
传递address=NNNN
选择。如果被忽略Java将会简单的选择一个随机自由的端口。
Debugging a remote service over the Internet can be slow and you might need to increase timeouts in your IDE. For example, in Eclipse you can select
Java → Debug
fromPreferences…
and change theDebugger timeout (ms)
to a more suitable value (60000
works well in most situations).
在网上调试一个远程服务可能是非常慢的,你可能需要在你的IDE中添加
timeouts
。例如,在Eclipse中你可以从Preferences…
选择Java → Debug
,并将Debugger timeout (ms)
改成更合适的值(60000
在大多数情况下都能很好工作)。
21. Packaging your application for production
Executable jars can be used for production deployment. As they are self-contained, they are also ideally suited for cloud-based deployment.
可执行jars
可以用来进行产品部署。当它们是自包含时,理想情况下它们也是适合云部署的。
For additional “production ready” features, such as health, auditing and metric REST or JMX end-points; consider adding spring-boot-actuator
. See Part V, “Spring Boot Actuator: Production-ready features” for details.
对于其它的“production ready”功能,例如健康,审计和度量REST或JMX端点;考虑添加spring-boot-actuator
。更多细节请看第五部分,“Spring Boot Actuator: Production-ready features”。
22. What to read next
You should now have good understanding of how you can use Spring Boot along with some best practices that you should follow. You can now go on to learn about specific Spring Boot features in depth, or you could skip ahead and read about the “production ready” aspects of Spring Boot.
现在你应该对怎么使用Spring Boot以及应该循序的一些最佳实践有了很好的理解。你可以继续学习关于Spring Boot的一些深度功能,或者你可以跳过前面,直接阅读Spring Boot “production ready”方面的内容。