SpringBoot与docker的结合的示例
程序员文章站
2022-06-19 23:41:57
最近一段时间,容器化成为了一种趋势。一台服务器可以虚拟成多个容器,同时提供服务,共享硬件资源,节约成本,容器化的翘楚就是docker,我司的所有微服务的发布都已经容器化。s...
最近一段时间,容器化成为了一种趋势。一台服务器可以虚拟成多个容器,同时提供服务,共享硬件资源,节约成本,容器化的翘楚就是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,看到结果
说明我们访问容器内的程序成功了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。