提升10倍生产力:IDEA远程一键部署SpringBoot到Docker

共 3093字,需浏览 7分钟

 ·

2020-10-11 03:11

粉丝福利1:我写了10 万字 Spring Boot 详细学习笔记(带完整目录)+源码,分享给大家

粉丝福利2:教妹子手撸了50个项目实战后,我超神了……

IDEA是Java开发利器,springboot是Java生态中最流行的微服务框架,docker是时下最火的容器技术,那么它们结合在一起会产生什么化学反应呢?

一、开发前准备

1.Docker安装

可以参考:

https://docs.docker.com/install/

2.配置docker远程连接端口

  vi /usr/lib/systemd/system/docker.service

找到 ExecStart,在最后面添加 -H tcp://0.0.0.0:2375,如下图所示

c260fe40c71be0fa061a50dc1804a698.webp

3.重启docker

 systemctl daemon-reload
 systemctl start docker

4.开放端口

firewall-cmd --zone=public --add-port=2375/tcp --permanent  

5.Idea安装插件,重启

8ee3eb1513601c4628a0656f61604aa0.webp

6.连接远程docker

1、编辑配置

47f4012a005a0ded709fa90d42231a1a.webp

2、填远程docker地址

8729ad4cbcf24d3d429c51f13fa52953.webp

3、连接成功,会列出远程docker容器和镜像

166b7ada268f906081b4fc8fb9d2adf3.webp

二、新建项目

1.创建springboot项目

项目结构图

d16e04b4ee62fd5b05f3d4fe190b8082.webp

1、配置pom文件


<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.0modelVersion>

    <groupId>docker-demogroupId>
    <artifactId>com.demoartifactId>
    <version>1.0-SNAPSHOTversion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.2.RELEASEversion>
        <relativePath />
    parent>

    <properties>
         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
         <docker.image.prefix>com.demodocker.image.prefix>
         <java.version>1.8java.version>
    properties>
    <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
          plugin>
        <plugin>
           <groupId>com.spotifygroupId>
           <artifactId>docker-maven-pluginartifactId>
           <version>1.0.0version>
           <configuration>
              <dockerDirectory>src/main/dockerdockerDirectory>
              <resources>
                <resource>
                    <targetPath>/targetPath>
                    <directory>${project.build.directory}directory>
                    <include>${project.build.finalName}.jarinclude>
                resource>
              resources>
           configuration>
        plugin>
        <plugin>
            <artifactId>maven-antrun-pluginartifactId>
            <executions>
                 <execution>
                     <phase>packagephase>
                    <configuration>
                        <tasks>
                            <copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}">copy>
                        tasks>
                     configuration>
                    <goals>
                        <goal>rungoal>
                    goals>
                    execution>
            executions>
        plugin>

       plugins>
    build>
<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
  <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
    <dependency>
        <groupId>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>1.2.17version>
    dependency>
dependencies>
project>

2、在src/main目录下创建docker目录,并创建Dockerfile文件

FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

3、在resource目录下创建application.properties文件

logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990

4、创建DockerApplication文件

@SpringBootApplication
public class DockerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DockerApplication.class, args);
    }
}

5、创建DockerController文件

@RestController
public class DockerController {
    static Log log = LogFactory.getLog(DockerController.class);

    @RequestMapping("/")
    public String index() {
        log.info("Hello Docker!");
        return "Hello Docker!";
    }
}

6、增加配置

8b0f3e4e71378b197c79f04550b10aa3.webp

144c069635c96ada451035cc3075e00e.webp

dd0affb16d1cee6171fb1ee360d0dc8e.webp

命令解释:

  • Image tag : 指定镜像名称和tag,镜像名称为 docker-demo,tag为1.1

  • Bind ports : 绑定宿主机端口到容器内部端口。格式为[宿主机端口]:[容器内部端口]

  • Bind mounts : 将宿主机目录挂到到容器内部目录中。
    格式为[宿主机目录]:[容器内部目录]。这个springboot项目会将日志打印在容器 /home/developer/app/logs/ 目录下,将宿主机目录挂载到容器内部目录后,那么日志就会持久化容器外部的宿主机目录中。

7、Maven打包

b8e49e8b8980661de1090d0d2e1f75c8.webp

8、运行

33bcff6b11e18e556e918779513d1edb.webp

8af4c1ef8ac8eabdc61b3364b04393b1.webp

先pull基础镜像,然后再打包镜像,并将镜像部署到远程docker运行

14931ebdce4c4c90312a2b941a474f62.webp

这里我们可以看到镜像名称为docker-demo:1.1,docker容器为docker-server

9、运行成功

5487c2c128fd800f81266c9a034defa6.webp

10、浏览器访问

5729ce3b3805a69c8f83e9a54d383b99.webp

11、日志查看

d6abb06b2a192ee573a4fbf870f19769.webp

自此,通过IDEA 部署springboot项目到docker成功!难以想象,部署一个Javaweb项目竟然如此简单方便!

来源:juejin.im/post/5d026212f265da1b8608828b

最后免费给大家分享50个Java项目实战资料,涵盖入门、进阶各个阶段学习内容,可以说非常全面了。大部分视频还附带源码,学起来还不费劲!


附上截图。(下面有下载方式)。

fcfb5724e2468b897ae3ef7063195a63.webp

7a6ce413ac6b36a78bbcc2fc585559d6.webp

fecd6f0d6b32cd08bb0ca4c3d3ae28b2.webp

a9ea3a9adad27b99fbbe93bc8adf8c97.webp

50cba60d95cfb0e7eed3f510a89eb88d.webp


项目领取方式:

扫描下方公众号回复:50

可获取下载链接

???

?长按上方二维码 2 秒回复「50」即可获取资料

点赞是最大的支持 71b865750e0b94a92d8ae9424cebe87c.webp

浏览 31
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报