为了账号安全,请及时绑定邮箱和手机立即绑定

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

hello world项目开发

  • 项目结构

图片描述

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sogou.liuming216448</groupId>
    <artifactId>spring-boot-sample</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 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;

/**
 * @author liuming216448
 * @date 2019/10/15 9:47 AM
 * @since 1.0.0
 */
@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: [/*]
[INFO] 2019-10-16 19:57:06,211 [localhost-startStop-1] web.servlet.FilterRegistrationBean (AbstractFilterRegistrationBean.java:258) -Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
[INFO] 2019-10-16 19:57:06,212 [localhost-startStop-1] web.servlet.FilterRegistrationBean (AbstractFilterRegistrationBean.java:258) -Mapping filter: 'httpPutFormContentFilter' to: [/*]
[INFO] 2019-10-16 19:57:06,212 [localhost-startStop-1] web.servlet.FilterRegistrationBean (AbstractFilterRegistrationBean.java:258) -Mapping filter: 'requestContextFilter' to: [/*]
[INFO] 2019-10-16 19:57:06,424 [main] method.annotation.RequestMappingHandlerAdapter (RequestMappingHandlerAdapter.java:534) -Looking for @ControllerAdvice: 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:06,465 [main] method.annotation.RequestMappingHandlerMapping (AbstractHandlerMethodMapping.java:543) -Mapped "{[/]}" onto public java.lang.String sogou.liuming216448.springboot.sample.SpringBootSampleApplication.home()
[INFO] 2019-10-16 19:57:06,469 [main] method.annotation.RequestMappingHandlerMapping (AbstractHandlerMethodMapping.java:543) -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)
[INFO] 2019-10-16 19:57:06,469 [main] method.annotation.RequestMappingHandlerMapping (AbstractHandlerMethodMapping.java:543) -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)
[INFO] 2019-10-16 19:57:06,486 [main] servlet.handler.SimpleUrlHandlerMapping (AbstractUrlHandlerMapping.java:362) -Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[INFO] 2019-10-16 19:57:06,486 [main] servlet.handler.SimpleUrlHandlerMapping (AbstractUrlHandlerMapping.java:362) -Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[INFO] 2019-10-16 19:57:06,509 [main] servlet.handler.SimpleUrlHandlerMapping (AbstractUrlHandlerMapping.java:362) -Mapped URL path [/**/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            CREATED              SIZE
spring-boot-sample                                          1.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                                                       latest              ab56bba91343        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-apiserver                                   v1.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-scheduler                                   v1.14.6             d27987bc993e        8 weeks ago          81.6MB
kubernetesdashboarddev/dashboard-metrics-sidecar            latest              c11c56931781        2 months ago         40.1MB
kubernetesui/metrics-scraper                                v1.0.1              709901356c11        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-apiserver                                   v1.14.3             9946f563237c        4 months ago         210MB
k8s.gcr.io/kube-scheduler                                   v1.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/coredns                                          1.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.10              2c4adeb21b4f        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 ,响应如下:
图片描述

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

点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消