文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡(jiǎn)書
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.
將應(yīng)用打包成jar并使用內(nèi)嵌HTTP服務(wù)器的一個(gè)最大優(yōu)勢(shì)是你可以在任何地方運(yùn)行你的程序。調(diào)試Spring Boot應(yīng)用也很容易;你不必指定任何特定的IDE插件或擴(kuò)展。
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.
?
這一節(jié)只包含基于jar的打包,如果你想選擇將你的應(yīng)用打包成war文件,你應(yīng)該參考你的服務(wù)器和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中運(yùn)行一個(gè)Spring Boot應(yīng)用將像運(yùn)行一個(gè)簡(jiǎn)單的Java應(yīng)用一樣,然而,首先你需要導(dǎo)入你的工程。導(dǎo)入步驟根據(jù)你的IDE和構(gòu)建系統(tǒng)會(huì)有所變化。大多數(shù)IDE可以直接導(dǎo)入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.
如果你不能直接將工程導(dǎo)入你的IDE中,你可以使用構(gòu)建插件生成一個(gè)IDE元數(shù)據(jù)。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
Relaunchbutton rather thanRunto ensure that any existing instance is closed.
?
如果你偶然的運(yùn)行一個(gè)web應(yīng)用兩次,你會(huì)看到一個(gè)
Port already in use錯(cuò)誤。為了確保任何現(xiàn)有的實(shí)例被關(guān)閉,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插件來(lái)創(chuàng)建一個(gè)可執(zhí)行的jar包,你可以通過使用java -jar來(lái)運(yùn)行你的應(yīng)用。例如:
$ 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:
它也支持以遠(yuǎn)程調(diào)試模式運(yùn)行一個(gè)打包的應(yīng)用。這允許你在打包的應(yīng)用中添加一個(gè)調(diào)試器:
$ 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插件包含一個(gè)run目標(biāo),可以用來(lái)快速編譯并運(yùn)行你的應(yīng)用。應(yīng)用以exploded形式運(yùn)行,就像在IDE中運(yùn)行一樣。
$ mvn spring-boot:run
You might also want to use the useful operating system environment variable:
你也可以使用有用的操作系統(tǒng)環(huán)境變量:
$ 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插件也包含一個(gè)bootRun任務(wù),它可以以exploded方式運(yùn)行你的應(yīng)用。當(dāng)你導(dǎo)入spring-boot-gradle-plugin時(shí),可以添加bootRun任務(wù):
$ gradle bootRun
You might also want to use this useful operating system environment variable:
你也可以使用有用的操作系統(tǒng)環(huán)境變量:
$ 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應(yīng)用只是普通的Java應(yīng)用,JVM熱交換應(yīng)該是開箱即用的。JVM熱交換可以替換的字節(jié)碼有限制,一個(gè)更全面的解決方案是JRebel或Spring Loaded工程。spring-boot-devtools模塊也支持快速的應(yīng)用重啟。
See the Chapter 20, Developer tools section below and the Hot swapping “How-to” for details.
更多細(xì)節(jié)請(qǐng)看20章,下面的開發(fā)者工具部分和怎樣熱交換部分。
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包含額外的工具集合,可以使應(yīng)用開發(fā)的過程更方便一點(diǎn)。spring-boot-devtools模塊可以包含進(jìn)任何工程,用來(lái)提供額外的程序調(diào)試特性。為了添加工具支持,簡(jiǎn)單的添加模塊依賴到你的構(gòu)建系統(tǒng)中:
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 -jaror if it’s started using a special classloader, then it is considered a “production application”. Flagging the dependency asoptionalis a best practice that prevents devtools from being transitively applied to other modules using your project. Gradle does not supportoptionaldependencies out-of-the-box so you may want to have a look to the propdeps-plugin in the meantime.
?
當(dāng)運(yùn)行一個(gè)完整的打包應(yīng)用時(shí),開發(fā)者工具會(huì)自動(dòng)失效。如果你的應(yīng)用是通過
java -jar啟動(dòng)的,或者是通過一個(gè)特定的類加載器啟動(dòng)的,那它會(huì)被當(dāng)做一個(gè)『產(chǎn)品級(jí)應(yīng)用』。將依賴標(biāo)記為optional是阻止開發(fā)工具間接應(yīng)用到使用你應(yīng)用的其它模塊的最佳實(shí)踐。Gradle不支持optional依賴開箱即用,同時(shí)你可能想看看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
excludeDevtoolsbuild property to include it. The property is supported with both the Maven and Gradle plugins.
?
再打包文件默認(rèn)不包含開發(fā)者工具。如果你想使用某些遠(yuǎn)程開發(fā)者工具特性,你需要讓
excludeDevtools構(gòu)建屬性包含它。Maven和Gradle插件都支持這個(gè)屬性。
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.
當(dāng)classpath中的文件修改時(shí),使用spring-boot-devtools的應(yīng)用會(huì)自動(dòng)重啟。當(dāng)使用IDE開發(fā)時(shí),這是一個(gè)很有用的功能,因?yàn)榇a改變時(shí)它能快速的進(jìn)行反饋。默認(rèn)情況下,會(huì)監(jiān)控classpath指向的文件夾中任何條目的變化。注意某些資源例如靜態(tài)資源和視圖模板不需要重啟應(yīng)用。
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.
?
觸發(fā)重啟
作為
DevTools監(jiān)視器classpath中的資源,觸發(fā)重啟的唯一方式是更新classpath。引起classpath更新的方式取決于你使用的IDE。在Eclipse中,保存一個(gè)修改的文件將引起classpath更新并觸發(fā)重啟事件。在IntelliJ IDEA中,構(gòu)建工程(Build → Make Project)將會(huì)有同樣的效果。
?
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.
?
只要啟用了分支,你也可以通過支持的構(gòu)建系統(tǒng)啟動(dòng)你的應(yīng)用(例如,Maven和Gradle),因?yàn)?code>DevTools需要一個(gè)獨(dú)立的應(yīng)用類加載器來(lái)進(jìn)行正確操作。當(dāng)Gradle和Maven在
classpath中檢測(cè)到DevTools時(shí),它們默認(rèn)的進(jìn)行了那個(gè)工作。
?
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 asLiveReloadand property overrides) can still be used.
?
當(dāng)使用
LiveReload,自動(dòng)重啟能很好的工作。更多細(xì)節(jié)請(qǐng)看下面。如果你使用JRebel進(jìn)行自動(dòng)重啟,將不支持動(dòng)態(tài)的類重加載。其它的開發(fā)者工具功能(例如LiveReload和屬性覆寫)仍能繼續(xù)使用。
?
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依賴應(yīng)用上下文關(guān)閉鉤子來(lái)進(jìn)行重啟期間的關(guān)閉。如果你禁用了關(guān)閉鉤子它將不能正確工作(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.
?
當(dāng)決定
classpath中輸入引起的改變是否應(yīng)該觸發(fā)重啟時(shí),DevTools會(huì)自動(dòng)忽略命名為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提供的重啟技術(shù)是通過兩個(gè)類加載器進(jìn)行工作的。加載進(jìn)基類加載器的類不能改變(例如,那些第三方j(luò)ar包)。那些你正在開發(fā)的類加載進(jìn)重啟類加載器中。當(dāng)應(yīng)用重啟時(shí),丟棄舊的重啟類加載器并創(chuàng)建一個(gè)新的。這種方法意味著應(yīng)用重啟時(shí)比『冷啟動(dòng)』更快,因?yàn)榛惣虞d器已經(jīng)存在并可用。
如果你發(fā)現(xiàn)你的應(yīng)用重啟不夠快,或者你碰到了類加載問題,你可以考慮重載技術(shù)例如ZeroTurnaround的JRebel。這些工作通過加載時(shí)類的重寫使它們更適合重載。Spring加載提供了另一種選擇,但許多框架都不支持,也不支持商業(yè)化。
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:
某些資源當(dāng)它們改變時(shí)不一定需要觸發(fā)重啟。例如,Thymeleaf模板可以就地編輯。默認(rèn)情況下,/META-INF/maven,/META-INF/resources,/resources,/static,/public或/templates中資源的改變不會(huì)觸發(fā)重啟,但會(huì)觸發(fā)實(shí)時(shí)重載。如果你想定制這些排除項(xiàng),你可以使用spring.devtools.restart.exclude屬性。例如,僅排除/static和/public,設(shè)置如下:
spring.devtools.restart.exclude=static/**,public/**
If you want to keep those defaults and add additional exclusions, use the
spring.devtools.restart.additional-excludeproperty instead.
?
如果你想保持這些默認(rèn),添加額外的排除項(xiàng),可以使用
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.
當(dāng)你修改不在classpath中的文件時(shí),你可能也想重啟或重載你的應(yīng)用。為了這樣做,可以使用spring.devtools.restart.additional-paths屬性來(lái)監(jiān)控其它路徑上的變化。你可以使用前面描述的spring.devtools.restart.exclude屬性來(lái)控制其它路徑上的變化是否會(huì)觸發(fā)重啟或僅觸發(fā)實(shí)時(shí)重載。
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屬性禁用它。大多數(shù)情況下你可以在你的application.properties中設(shè)置它(這仍會(huì)初始化重啟類加載器但它不會(huì)監(jiān)控文件的變化)。
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:
如果你需要完整的禁用重啟支持,例如,因?yàn)樗荒芘c一個(gè)特定的庫(kù)一起工作,你需要在調(diào)用SpringApplication.run(…?)之前設(shè)置一個(gè)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可以持續(xù)編譯變化的文件,你可以只在特定的時(shí)間觸發(fā)重啟。為了實(shí)現(xiàn)這個(gè)功能你可以使用『觸發(fā)器文件』,當(dāng)你需要真正觸發(fā)重啟檢查時(shí),它是一個(gè)你必須要修改文件。修改這個(gè)文件只觸發(fā)重啟檢查,只有Devtools檢測(cè)到它必須要做些事情時(shí),重啟才會(huì)發(fā)生。觸發(fā)器文件應(yīng)該進(jìn)行手動(dòng)更新,或通過IDE插件更新。
To use a trigger file use the spring.devtools.restart.trigger-file property.
為了使用觸發(fā)器文件需要使用spring.devtools.restart.trigger-file屬性。
You might want to set
spring.devtools.restart.trigger-fileas a global setting so that all your projects behave in the same way.
?
為了使你所有的工程都表現(xiàn)一樣,你需要將
spring.devtools.restart.trigger-file設(shè)置為全局設(shè)置。
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部分所講的那樣,重啟功能是通過兩個(gè)類加載器實(shí)現(xiàn)的。對(duì)于大多數(shù)應(yīng)用來(lái)說這個(gè)方法能很好的工作,但是有時(shí)候它也會(huì)引起一些類加載問題。
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.
默認(rèn)情況下,IDE中的任何開放的工程都會(huì)使用“restart”類加載器進(jìn)行加載,任何規(guī)范的.jar文件都會(huì)使用“base”類加載器進(jìn)行加載。如果你在一個(gè)多模塊工程中工作,不是每一個(gè)模塊都導(dǎo)入到你的IDE中,你可能需要定制一些東西。為了實(shí)現(xiàn)這個(gè)功能你可以創(chuàng)建一個(gè)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元素應(yīng)該拉進(jìn)“restart”類加載器中,exclude元素應(yīng)該推入到“base”類加載器中。屬性值是應(yīng)用到classpath中的一個(gè)正則表達(dá)式。
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作為一個(gè)屬性啟動(dòng),它就被應(yīng)用。
All
META-INF/spring-devtools.propertiesfrom 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都會(huì)被加載。你可以將文件打包進(jìn)你的工程中,或這個(gè)工程使用的庫(kù)中。
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().
當(dāng)存在使用標(biāo)準(zhǔn)ObjectInputStream進(jìn)行反序列化的對(duì)象時(shí),重啟功能不能很好的工作。如果你需要反序列化數(shù)據(jù),你可能需要使用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.
遺憾的是,一些第三方庫(kù)反序列化時(shí)沒有考慮上下文類加載器。如果你發(fā)現(xiàn)了這樣的問題,你需要請(qǐng)求原作者進(jìn)行修正。
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模塊包含一個(gè)內(nèi)嵌的實(shí)時(shí)重載服務(wù)器,當(dāng)資源改變時(shí)可以用來(lái)觸發(fā)瀏覽器重新刷新。實(shí)時(shí)重載瀏覽器擴(kuò)展對(duì)于livereload.com中的Chrome,F(xiàn)irefox和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.
當(dāng)你的應(yīng)用運(yùn)行時(shí),如果你不想啟動(dòng)實(shí)時(shí)重載服務(wù)器,你可以將spring.devtools.livereload.enabled屬性設(shè)為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.
?
一次你只可以運(yùn)行一個(gè)實(shí)時(shí)重載服務(wù)器。在啟動(dòng)你的應(yīng)用之前,確保沒有其它的實(shí)時(shí)重載服務(wù)器在運(yùn)行。如果你從你的IDE中啟動(dòng)多個(gè)應(yīng)用,只有第一個(gè)應(yīng)用有實(shí)時(shí)重載服務(wù)器支持。
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:
通過添加一個(gè)名為.spring-boot-devtools.properties的文件到你的$HOME文件夾中(注意文件名以.開頭),你可以配置全局開發(fā)者工具設(shè)置。任何添加到這個(gè)文件的屬性都將應(yīng)用到你機(jī)器上所有使用開發(fā)者工具的Spring Boot應(yīng)用中。例如,為了配置重啟時(shí)總是使用一個(gè)觸發(fā)器文件,你需要添加如下內(nèi)容:
~/.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開發(fā)者工具是不受本地環(huán)境限制的,在運(yùn)行遠(yuǎn)程應(yīng)用時(shí)你也可以使用一些功能。遠(yuǎn)程支持是選擇性加入的,為了使它起作用你需要確保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:
然后你需要設(shè)置spring.devtools.remote.secret屬性,例如:
spring.devtools.remote.secret=mysecret
Enabling
spring-boot-devtoolson a remote application is a security risk. You should never enable support on a production deployment.
?
使
spring-boot-devtools在遠(yuǎn)程應(yīng)用上起作用是有安全風(fēng)險(xiǎn)的。在產(chǎn)品部署時(shí),你不應(yīng)該讓它支持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.
遠(yuǎn)程devtools需要兩部分提供支持:一個(gè)接收連接的服務(wù)器端,一個(gè)運(yùn)行在IDE中的客戶端應(yīng)用。當(dāng)設(shè)置spring.devtools.remote.secret屬性時(shí),服務(wù)器組件會(huì)自動(dòng)起作用??蛻舳私M件必須手動(dòng)啟動(dòng)。
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.RemoteSpringApplicationusing 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.
遠(yuǎn)程客戶端應(yīng)用設(shè)計(jì)要在你的IDE中運(yùn)行。你需要運(yùn)行org.springframework.boot.devtools.RemoteSpringApplication,與要連接的遠(yuǎn)程工程要使用同樣的classpath。傳給應(yīng)用的非可選參數(shù)應(yīng)該是你要連接的遠(yuǎn)程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 theRunmenu.Create a new
Java Application“l(fā)aunch configuration”.Browse for the
my-appproject.Use
org.springframework.boot.devtools.RemoteSpringApplicationas the main class.Add
https://myapp.cfapps.ioto theProgram arguments(or whatever your remote URL is).
例如,如果你在使用Eclipse或STS,你有一個(gè)名為my-app的工程,它已經(jīng)部署到了云計(jì)算中,你需要按照下面的步驟去做:
從
Run菜單中選擇Run Configurations…?。創(chuàng)建一個(gè)新的
Java Application“l(fā)aunch configuration”。瀏覽
my-app工程。使用
org.springframework.boot.devtools.RemoteSpringApplication作為主類。添加
https://myapp.cfapps.io到Program arguments(或無(wú)論你的遠(yuǎn)程URL是什么)。
A running remote client will look like this:
一個(gè)運(yùn)行的遠(yuǎn)程客戶端如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \
\\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) )
' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / /
=========|_|==============|___/===================================/_/_/_/
:: 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.secretproperty is read and passed to the server for authentication.
?
因?yàn)檫h(yuǎn)程客戶端與真實(shí)的應(yīng)用使用的
classpath相同,因此它可以直接讀取應(yīng)用屬性。這就是spring.devtools.remote.secret屬性是怎樣讀取并傳遞給服務(wù)器進(jìn)行授權(quán)的。
?
It’s always advisable to use
https://as the connection protocol so that traffic is encrypted and passwords cannot be intercepted.
?
為了傳輸可以加密,密碼不能被解析,建議總是使用
https://作為連接協(xié)議。
?
If you need to use a proxy to access the remote application, configure the
spring.devtools.remote.proxy.hostandspring.devtools.remote.proxy.portproperties.
?
如果你需要使用代理訪問遠(yuǎn)程應(yīng)用,配置
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.
遠(yuǎn)程客戶端會(huì)像本地重啟那樣監(jiān)控你應(yīng)用的classpath的變化。任何資源的更新都會(huì)推送到遠(yuǎn)程應(yīng)用并(如果需要的話)觸發(fā)重啟。如果你在迭代一個(gè)本地沒有的使用云服務(wù)的功能,它是非常有幫助的。通常更新和重啟比整個(gè)重新構(gòu)建部署更快。
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.
?
當(dāng)遠(yuǎn)程客戶端運(yùn)行時(shí)只監(jiān)控文件。如果在啟動(dòng)遠(yuǎn)程客戶端之前你修改了文件,它將不會(huì)推送到遠(yuǎn)程服務(wù)器。
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.
當(dāng)在遠(yuǎn)程應(yīng)用上分析問題時(shí),Java遠(yuǎn)程調(diào)試是非常有用的。遺憾的是,當(dāng)你的應(yīng)用部署離開你的數(shù)據(jù)中心時(shí),遠(yuǎn)程調(diào)試并不總是能用的。如果你正在使用一個(gè)基于容器的技術(shù)例如Docker,要設(shè)置遠(yuǎn)程調(diào)試也是非常棘手的。
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協(xié)議上的遠(yuǎn)程調(diào)試通道。遠(yuǎn)程客戶端提供一個(gè)端口為8000的本地服務(wù)器,你可以在這上面添加一個(gè)遠(yuǎn)程調(diào)試器。一旦建立了連接,調(diào)試通道將會(huì)在HTTP上發(fā)送到遠(yuǎn)程應(yīng)用上。如果你想使用一個(gè)不同的端口,你可以使用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:
你需要確保你的遠(yuǎn)程應(yīng)用啟動(dòng)了并且遠(yuǎn)程調(diào)試可用。這經(jīng)??梢酝ㄟ^配置JAVA_OPTS來(lái)完成。例如,在云計(jì)算平臺(tái)你可以添加下面的內(nèi)容到你的manifest.yml中:
---
env:
JAVA_OPTS: "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n"
Notice that you don’t need to pass an
address=NNNNoption to-Xrunjdwp. If omitted Java will simply pick a random free port.
?
注意你不必向
-Xrunjdwp傳遞address=NNNN選擇。如果被忽略Java將會(huì)簡(jiǎn)單的選擇一個(gè)隨機(jī)自由的端口。
?
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 → DebugfromPreferences…?and change theDebugger timeout (ms)to a more suitable value (60000works well in most situations).
?
在網(wǎng)上調(diào)試一個(gè)遠(yuǎn)程服務(wù)可能是非常慢的,你可能需要在你的IDE中添加
timeouts。例如,在Eclipse中你可以從Preferences…?選擇Java → Debug,并將Debugger timeout (ms)改成更合適的值(60000在大多數(shù)情況下都能很好工作)。
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.
可執(zhí)行jars可以用來(lái)進(jìn)行產(chǎn)品部署。當(dāng)它們是自包含時(shí),理想情況下它們也是適合云部署的。
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.
對(duì)于其它的“production ready”功能,例如健康,審計(jì)和度量REST或JMX端點(diǎn);考慮添加spring-boot-actuator。更多細(xì)節(jié)請(qǐng)看第五部分,“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.
現(xiàn)在你應(yīng)該對(duì)怎么使用Spring Boot以及應(yīng)該循序的一些最佳實(shí)踐有了很好的理解。你可以繼續(xù)學(xué)習(xí)關(guān)于Spring Boot的一些深度功能,或者你可以跳過前面,直接閱讀Spring Boot “production ready”方面的內(nèi)容。