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整合docker部署实现两种构建Docker镜像方式
- Springboot项目打war包docker包找不到resource下静态资源的解决方案
- 使用dockercompose搭建springboot-mysql-nginx应用
- Mac下部署springBoot项目到Docker中(demo)
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数示例代码
- 详解springboot项目docker部署实践
- 解决idea中javaweb的mysql8.0.15配置问题
- 基于idea 的 Java中的get/set方法之优雅的写法
- IDEA新建javaWeb以及Servlet简单实现小结
- java通过Idea远程一键部署springboot到Docker详解
相关文章
SpringBoot集成thymeleaf渲染html模板的步骤详解
这篇文章主要给大家详细介绍了SpringBoot集成thymeleaf如何使实现html模板的渲染,文中有详细的代码示例,具有一定的参考价值,需要的朋友可以参考下2023-06-06
最新评论