SpringBoot基礎教程(十六)——與docker的結合
最近一段時間,容器化成為了一種趨勢。一臺伺服器可以虛擬成多個容器,同時提供服務,共享硬體資源,節約成本,容器化的翹楚就是Docker,我司的所有微服務的釋出都已經容器化。spring boot 也緊跟潮流,加入了Docker的maven外掛,可以通過執行命令來製作映象。
本節的主要內容不是講程式碼,而是講這個Docker外掛。廢話不多說,上pom
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正則[a-z0-9-_.]的,否則構建不會成功 -->
<!-- 詳見:https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed-->
<imageName>spring-boot-docker-start</imageName>
<!--相當於from java,本地有使用本地的映象,沒有的話從遠端倉庫拉取-->
<baseImage>java</baseImage>
<exposes>
<!--暴露容器內的8080埠-->
<expose>8080</expose>
</exposes>
<!--進入點,執行的命令-->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
imageName就是映象的名稱。baseImage是基礎映象,本地有使用本地的映象,沒有的話從遠端倉庫拉取,暴露容器內的8080埠,執行java -jar 命令,啟動微服務。我們知道使用Docker需要制定Dockerfile檔案,裡面的元素完全通過maven外掛的標籤來體現了。還是有前提的,你得先安裝好Docker。講解到這裡,我們開始執行
第一步:執行mvn clean package docker:build
建立生成映象。
第二步:啟動映象docker run -it -P spring-boot-docker-start
,看下容器內的日誌
➜ spring-boot-docker-start git:(master) docker run -it -P spring-boot-docker-start
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.5.RELEASE)
2018-03-25 08:41:56.274 INFO 1 --- [ main] com.shuqi.ApplicationMain : Starting ApplicationMain on 075543f8f5b6 with PID 1 (/spring-boot-docker-start.jar started by root in /)
2018-03-25 08:41:56.287 INFO 1 --- [ main] com.shuqi.ApplicationMain : No active profile set, falling back to default profiles: default
2018-03-25 08:41:56.406 INFO 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@126d28d3: startup date [Sun Mar 25 08:41:56 UTC 2018]; root of context hierarchy
2018-03-25 08:41:58.356 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-03-25 08:41:58.382 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2018-03-25 08:41:58.384 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.33
2018-03-25 08:41:58.512 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-03-25 08:41:58.512 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2113 ms
2018-03-25 08:41:58.920 INFO 1 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-03-25 08:41:58.928 INFO 1 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-03-25 08:41:58.937 INFO 1 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-03-25 08:41:58.937 INFO 1 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-03-25 08:41:58.938 INFO 1 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-03-25 08:41:59.406 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@126d28d3: startup date [Sun Mar 25 08:41:56 UTC 2018]; root of context hierarchy
2018-03-25 08:41:59.516 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.shuqi.controller.HelloController.hello()
2018-03-25 08:41:59.523 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-03-25 08:41:59.524 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-03-25 08:41:59.584 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-25 08:41:59.585 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-25 08:41:59.645 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-25 08:41:59.754 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-03-25 08:41:59.834 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-03-25 08:41:59.838 INFO 1 --- [ main] com.shuqi.ApplicationMain : Started ApplicationMain in 4.084 seconds (JVM running for 5.012)
[2018-03-25 08:41:59] server started!
啟動成功。
第三步:輸入docker ps
看看容器內的8080埠被對映到了本機的哪個埠
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
075543f8f5b6 spring-boot-docker-start "java -jar /spring..." About a minute ago Up About a minute 0.0.0.0:32768->8080/tcp trusting_noether
確定是32768埠。
第四步:瀏覽器中輸入http://localhost:32768/hello
,看到結果
說明我們訪問容器內的程式成功了!
下節將的內容是:SpringBoot基礎教程(十七)——與日誌的結合
相關文章
- SpringBoot與mongodb的結合Spring BootMongoDB
- SpringBoot:結合 SpringBoot 與 Grails 3Spring BootAI
- shell基礎教程 十六: Shell until迴圈
- Redux 基礎教程以及結合 React 使用方式ReduxReact
- Python基礎(五)——閉包與lambda的結合Python
- Redis 結合 Docker 搭建叢集,並整合SpringBootRedisDockerSpring Boot
- SpringBoot與單元測試JUnit的結合Spring Boot
- Redis 結合 Docker 搭建哨兵+主從,並整合SpringBootRedisDockerSpring Boot
- SpringBoot2.x系列教程(三十六)SpringBoot之Tomcat配置Spring BootTomcat
- Docker基礎與基本概念Docker
- Mockito與PowerMock的使用基礎教程Mockito
- SpringBoot之:SpringBoot的HATEOAS基礎Spring Boot
- 【matplotlib基礎】--結合地圖地圖
- [適合小白的Linu基礎入門教程一
- 寫給前端工程師看的Docker教程-基礎篇前端工程師Docker
- flutter+Springboot的結合FlutterSpring Boot
- SpringBoot(十六)_springboot整合JasperReSpring Boot
- 【Docker】Docker基礎-埠對映與容器互聯Docker
- SpringBoot基礎實戰系列(二)springboot解析json與HttpMessageConverterSpring BootJSONHTTP
- 【Docker】Docker基礎操作Docker
- docker基礎Docker
- Docker 基礎Docker
- Docker:基礎Docker
- springboot2.x基礎教程:自動裝配原理與條件註解Spring Boot
- docker使用經驗總結(三、Docker compose基礎應用)Docker
- Docker(十六)-Docker的daemon.json的作用DockerJSON
- 【JavaWeb】SpringBoot基礎JavaWebSpring Boot
- Spring MVC與Bootstrap基礎教程 - josdemSpringMVCboot
- 企業級 SpringBoot 教程 (二十四)springboot整合dockerSpring BootDocker
- Docker|基礎篇Docker
- Docker--基礎Docker
- Docker基礎原理Docker
- Docker 基礎 - 1Docker
- Docker 基礎 - 2Docker
- Docker 基礎 - 3Docker
- 組合遊戲與博弈論基礎遊戲
- 通俗易懂的Redis資料結構基礎教程Redis資料結構
- WebAssembly 基礎以及結合其他程式語言Web