SpringBoot入門-2(兩種熱部署方式)

 

在編寫代碼的時候,你會發現咱們只是簡單把打印信息改變了,就須要從新部署,若是是這樣的編碼方式,那麼咱們估計一天下來就真的是打幾個Hello World就下班了。那麼如何解決熱部署的問題呢?那就是springloaded

 一、在1的基礎上,pom文件添加一個plugin節點java

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <dependencies>
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>springloaded</artifactId>
    <version>1.2.6.RELEASE</version>
   </dependency>
  </dependencies>
 </plugin>

  二、此次啓動不能直接運行 main 方法了,須要點擊項目右鍵>>Run As >>Maven build...>>輸入spring-boot:run啓動,以後修改java文件測試web

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @RestController 至關於@Controller和@RequestBody
 *
 */
@RestController
@EnableAutoConfiguration
public class HelloController {

    
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("22");
        return "Hello Word SpringBootabbb";
    }
    
    public static void main(String[] args) {
        SpringApplication.run(HelloController.class, args);
    }
}

  完成熱部署,可是這個關閉時,從新啓動會發現端口被佔用,端口未被關閉,還在運行着,須要在任務管理器中進行關閉java.exespring

java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind0(Native Method) ~[na:1.7.0_45]
    at sun.nio.ch.Net.bind(Net.java:444) ~[na:1.7.0_45]
    at sun.nio.ch.Net.bind(Net.java:436) ~[na:1.7.0_45]
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214) ~[na:1.7.0_45]
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.7.0_45]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:773) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:473) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:986) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:239) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:194) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:151) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at com.test.HelloController.main(HelloController.java:24) [classes/:na]

2017-08-12 15:55:39.128 ERROR 1136 --- [           main] o.apache.catalina.core.StandardService   : Failed to start connector [Connector[HTTP/1.1-8080]]

org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8080]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:239) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:194) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:151) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at com.test.HelloController.main(HelloController.java:24) [classes/:na]
Caused by: org.apache.catalina.LifecycleException: service.getName(): "Tomcat";  Protocol handler start failed
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:993) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    ... 13 common frames omitted
Caused by: java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind0(Native Method) ~[na:1.7.0_45]
    at sun.nio.ch.Net.bind(Net.java:444) ~[na:1.7.0_45]
    at sun.nio.ch.Net.bind(Net.java:436) ~[na:1.7.0_45]
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214) ~[na:1.7.0_45]
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.7.0_45]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:773) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:473) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:986) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    ... 14 common frames omitted

2017-08-12 15:55:39.165  INFO 1136 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-08-12 15:55:39.193 ERROR 1136 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat servlet container
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:165) ~[spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) ~[spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) ~[spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    at com.test.HelloController.main(HelloController.java:24) [classes/:na]
Caused by: java.lang.IllegalStateException: Tomcat connector in failed state
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:159) ~[spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE]
    ... 10 common frames omitted

或者採用另外一個運行方式,能夠解決此問題。
apache

使用run as – java application,tomcat

那麼還須要作一些處理。 把spring-loader-1.2.4.RELEASE.jar下載下來,放到項目的lib目錄中,app

而後把IDEA的run參數裏VM參數設置爲: -javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify maven

而後啓動就能夠了,這樣在run as的時候,也能進行熱部署 spring-boot

 

 

經過使用springloaded進行熱部署,可是些代碼修改了,並不會進行熱部署測試

  第二種熱部署方式之devtools

spring-boot-devtools 是一個爲開發者服務的一個模塊,其中最重要的功能就是自動應用代碼更改到最新的App上面去。ui

原理是在發現代碼有更改以後,從新啓動應用,可是速度比手動中止後再啓動還要更快,更快指的不是節省出來的手工操做的時間。

其深層原理是使用了兩個ClassLoader,一個Classloader加載那些不會改變的類(第三方Jar包),另外一個ClassLoader加載會更改的類,

稱爲 restart ClassLoader ,這樣在有代碼更改的時候,原來的restart ClassLoader 被丟棄,從新建立一個restart ClassLoader,因爲須要加載的類相比較少,

因此實現了較快的重啓時間(5秒之內)。

 

一、maven的pom文件配置

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
 <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork><!-- 若是沒有該項配置,肯呢個devtools不會起做用,即應用不會restart -->
                </configuration>
            </plugin>

而後就完成了

devtools會監聽classpath下的文件變更,而且會當即重啓應用(發生在保存時機),注意:由於其採用的虛擬機機制,該項重啓是很快的。

 ---- Run Application --- Java Application便可測試

 

公衆號

歡迎關注個人公衆號「碼上開發」,天天分享最新技術資訊、最優原創文章。關注獲取最新資源

相關文章
相關標籤/搜索