栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

基于spring-boot的hello-world web项目容器化

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于spring-boot的hello-world web项目容器化

hello world项目开发
  • 项目结构

  • pom.xml


    4.0.0

    sogou.liuming216448
    spring-boot-sample
    1.0.0-SNAPSHOT
    jar

    
 org.springframework.boot
 spring-boot-starter-parent
 1.5.6.RELEASE
 
    

    
 UTF-8
 UTF-8
 1.8
    

    
 
     org.springframework.boot
     spring-boot-starter-web
     
  
      org.springframework.boot
      spring-boot-starter-logging
  
     
 
 
     org.springframework.boot
     spring-boot-starter-log4j
     1.3.8.RELEASE
 

 
     org.springframework.boot
     spring-boot-starter-test
     test
 
    

    
 
     
  org.springframework.boot
  spring-boot-maven-plugin
     
 
    


  • SpringBootSampleApplication
package sogou.liuming216448.springboot.sample;

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


@SpringBootApplication
@RestController
public class SpringBootSampleApplication {
    @RequestMapping("/")
    public String home() {
 return "{"version":"1.0.1","message":"hello world!"}";
    }

    public static void main(String[] args) {
 SpringApplication.run(SpringBootSampleApplication.class, args);
    }
}
  • application.yaml
spring:
  application:
    name: spring-boot-sample
server:
  tomcat:
    accesslog:
      enabled: true
      directory: ""
    basedir: /opt/logs/${spring.application.name}
  • log4j.properties
system.logPath=/opt/logs/spring-boot-sample
log4j.rootLogger=INFO, console, system_ALL

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%p] %d [%t] %c{3} (%F:%L) -%m%n

log4j.appender.system_ALL=org.apache.log4j.DailyRollingFileAppender
log4j.appender.system_ALL.file=${system.logPath}/system_ALL.log
log4j.appender.system_ALL.encoding=UTF-8
log4j.appender.system_ALL.DatePattern='.'yyyy-MM-dd
log4j.appender.system_ALL.layout=org.apache.log4j.PatternLayout
log4j.appender.system_ALL.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH:mm:ss,SSS}][%c{3}:%L] - %m%n
  • 运行 SpringBootSampleApplication
  .   ____   _     __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot :: (v1.5.6.RELEASE)

[INFO] 2019-10-16 19:57:05,077 [main] springboot.sample.SpringBootSampleApplication (StartupInfoLogger.java:48) -Starting SpringBootSampleApplication on liuming216448.local with PID 33677 (/Users/apple/Workspace/spring-boot-sample/target/classes started by apple in /Users/apple/Workspace/spring-boot-sample)
[INFO] 2019-10-16 19:57:05,080 [main] springboot.sample.SpringBootSampleApplication (SpringApplication.java:593) -No active profile set, falling back to default profiles: default
[INFO] 2019-10-16 19:57:05,141 [main] context.embedded.AnnotationConfigEmbeddedWebApplicationContext (AbstractApplicationContext.java:583) -Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@ca263c2: startup date [Wed Oct 16 19:57:05 CST 2019]; root of context hierarchy
[INFO] 2019-10-16 19:57:05,274 [background-preinit] internal.util.Version (Version.java:30) -HV000001: Hibernate Validator 5.3.5.Final
[INFO] 2019-10-16 19:57:06,032 [main] embedded.tomcat.TomcatEmbeddedServletContainer (TomcatEmbeddedServletContainer.java:89) -Tomcat initialized with port(s): 8080 (http)
十月 16, 2019 7:57:06 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service [Tomcat]
十月 16, 2019 7:57:06 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/8.5.16
[INFO] 2019-10-16 19:57:06,104 [localhost-startStop-1] web.context.ContextLoader (EmbeddedWebApplicationContext.java:276) -Root WebApplicationContext: initialization completed in 967 ms
十月 16, 2019 7:57:06 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring embedded WebApplicationContext
[INFO] 2019-10-16 19:57:06,204 [localhost-startStop-1] web.servlet.ServletRegistrationBean (ServletRegistrationBean.java:190) -Mapping servlet: 'dispatcherServlet' to [/]
[INFO] 2019-10-16 19:57:06,211 [localhost-startStop-1] web.servlet.FilterRegistrationBean (AbstractFilterRegistrationBean.java:258) -Mapping filter: 'characterEncodingFilter' to: [favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[INFO] 2019-10-16 19:57:06,599 [main] export.annotation.AnnotationMBeanExporter (MBeanExporter.java:431) -Registering beans for JMX exposure on startup
[INFO] 2019-10-16 19:57:06,651 [main] embedded.tomcat.TomcatEmbeddedServletContainer (TomcatEmbeddedServletContainer.java:201) -Tomcat started on port(s): 8080 (http)
[INFO] 2019-10-16 19:57:06,655 [main] springboot.sample.SpringBootSampleApplication (StartupInfoLogger.java:57) -Started SpringBootSampleApplication in 1.906 seconds (JVM running for 2.936)

服务正常启动,访问端口是8080;浏览器中访问 http://127.0.0.1:8080,得到如下结果:

到此,基于spring-boot的hello world web项目开发完成。

项目容器化
  • Dockerfile
# 指定基础镜像,在此基础上制作自己的镜像
FROM openjdk:8-jre-alpine

# 声明维护者信息
MAINTAINER liuming216448

# 环境变量
Env APP_NAME spring-boot-sample
Env APP_VERSION 1.0.0-SNAPSHOT

# 创建目录
RUN mkdir -p /opt/app/${APP_NAME}
RUN mkdir -p /opt/logs/${APP_NAME}

# 将项目中已经打包好的jart包添加到镜像中
ADD target/spring-boot-sample-*.jar /opt/app/spring-boot-sample/app.jar

# 镜像的启动入口
ENTRYPOINT ["java","-jar","/opt/app/spring-boot-sample/app.jar"]
  • 制作镜像
    1. mvn打jar包
      在项目的根目录下执行mvn clean package,会在项目根目录下的target目录下生成spring-boot-sample-1.0.0-SNAPSHOT.jar
    2. 制作镜像
      在项目的根目录下执行docker build -t spring-boot-sample:1.0.1 .,开始制作镜像 ,过程如下:
Sending build context to Docker daemon   14.5MB
Step 1/8 : FROM openjdk:8-jre-alpine
 ---> f7a292bbb70c
Step 2/8 : MAINTAINER liuming216448
 ---> Using cache
 ---> 534c6e19352c
Step 3/8 : Env APP_NAME spring-boot-sample
 ---> Using cache
 ---> 54c8aee28eb6
Step 4/8 : Env APP_VERSION 1.0.0-SNAPSHOT
 ---> Using cache
 ---> 77ad4217c0d8
Step 5/8 : RUN mkdir -p /opt/app/${APP_NAME}
 ---> Using cache
 ---> fb29729f0ee7
Step 6/8 : RUN mkdir -p /opt/logs/${APP_NAME}
 ---> Using cache
 ---> 1141d8242f9f
Step 7/8 : ADD target/spring-boot-sample-*.jar /opt/app/spring-boot-sample/app.jar
 ---> 5b0f6f5b9e00
Step 8/8 : ENTRYPOINT ["java","-jar","/opt/app/spring-boot-sample/app.jar"]
 ---> Running in f3bb3929244b
Removing intermediate container f3bb3929244b
 ---> f0c8db8e175a
Successfully built f0c8db8e175a
Successfully tagged spring-boot-sample:1.0.1
  1. docker images查看已有镜像
~ » docker images apple@liuming216448
REPOSITORY TAG   IMAGE ID     CREATEDSIZE
spring-boot-sample1.0.1 f0c8db8e175a about a minute ago   99.1MB
docker-reg.sogou-inc.com/liuming216448/spring-boot-sample   1.0.1 c989bc35ec93 24 hours ago  99.1MB
nginx      latestab56bba91343 4 weeks ago   126MB
kubernetesui/dashboard   v2.0.0-beta4 6802d83967b9 6 weeks ago   84MB
k8s.gcr.io/kube-proxy    v1.14.6      ed8adf767eeb 8 weeks ago   82.1MB
k8s.gcr.io/kube-apiserverv1.14.6      0e422c9884cf 8 weeks ago   209MB
k8s.gcr.io/kube-controller-manager     v1.14.6      4bb274b1f2c3 8 weeks ago   157MB
k8s.gcr.io/kube-schedulerv1.14.6      d27987bc993e 8 weeks ago   81.6MB
kubernetesdashboarddev/dashboard-metrics-sidecar     latestc11c56931781 2 months ago  40.1MB
kubernetesui/metrics-scraper    v1.0.1709901356c11 3 months ago  40.1MB
k8s.gcr.io/kube-proxy    v1.14.3      004666307c5b 4 months ago  82.1MB
k8s.gcr.io/kube-controller-manager     v1.14.3      ac2ce44462bc 4 months ago  158MB
k8s.gcr.io/kube-apiserverv1.14.3      9946f563237c 4 months ago  210MB
k8s.gcr.io/kube-schedulerv1.14.3      953364a3ae7a 4 months ago  81.6MB
docker/kube-compose-controller  v0.4.23      a8c3d87a58e7 4 months ago  35.3MB
docker/kube-compose-api-server  v0.4.23      f3591b2cb223 4 months ago  49.9MB
openjdk    8-jre-alpine f7a292bbb70c 5 months ago  84.9MB
k8s.gcr.io/coredns1.3.1 eb516548c180 9 months ago  40.3MB
k8s.gcr.io/kubernetes-dashboard-amd64  v1.10.1      f9aed6605b81 10 months ago 122MB
k8s.gcr.io/etcd   3.3.102c4adeb21b4f 10 months ago 258MB
k8s.gcr.io/pause  3.1   da86e6ba6ca1 22 months ago 742kB

可以发现已经存在我们刚才制作的那个镜像了。

  1. 启动容器
~ » docker run -d -p 8888:8080 spring-boot-sample:1.0.1 --network=host    apple@liuming216448
2a397b37e3f10cca863e9bfa111df78aa7b9bc66006fefba46cf0bbeabd4e1d0

浏览器中访问 http://127.0.0.1:8888 ,响应如下:

到此,我们成功完成了项目的容器化。

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号