美文网首页
Springboot【2.x】+ Maven远程部署到Linux

Springboot【2.x】+ Maven远程部署到Linux

作者: 山姆极客 | 来源:发表于2019-10-12 13:47 被阅读0次

序言

随着阿里中台概念的提出,大大小小的互联网公司都对原有项目技术架构进行改造、优化、升级,其中使用最多的莫过于微服务、容器以及云服务。我司综合考虑未来业务发展决定项目逐步云化、容器化,下面就基于springboot+maven项目远程部署到docker中进行分享下,希望能给大伙儿有些帮助。

系统及软件环境

  • Linux操作系统CentOS7(建议)
  • Docker(社区版)---官方地址

Docker RemoteApi

远程部署前提需要开启docker远程访问功能,下面介绍常用两个操作系统的配置

  • CentOS 6
    修改/etc/sysconfig/docker文件,重启后生效(service docker restart)
DOCKER_OPTS="-H=unix:///var/run/docker.sock -H=0.0.0.0:2375"
  • CentOS 7
    修改/usr/lib/systemd/system/docker.service文件,在ExecStart后面添加一行
ExecStart=/usr/bin/dockerd  -H tcp://0.0.0.0:2375  -H unix:///var/run/docker.sock

效果截图

dockerConfig

重启服务

  systemctl daemon-reload    
  systemctl restart docker.service 
dockerInfo

项目创建与访问

打开intelliJ -> New Project -> Spring Assistant(Spring Initializr Server) -> 工程名(mvhello) -> 点击完成,项目结构如下

[图片上传失败...(image-c1dd47-1570859119714)]

在src/main/kotlin 相应目录下新建HelloController

@RestController
class HelloController {
    @GetMapping("hello")
    fun helloWorld() = "hello world"
}

在src/main/resource的application.properties里面添加自定义端口号

server.port=8888

启动项目,浏览器访问

~$ mvn spring-boot:run

[图片上传失败...(image-35df75-1570859119714)]

配置DockerFile

确保项目能正常访问后,在src/main/docker文件夹下面,新建dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD mvhello-0.0.1.jar app.jar
# -Djava.security.egd=file:/dev/./urandom 可解决tomcat可能启动慢的问题
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8888

配置Maven中docker依赖

在pom.xml中添加下面代码块

<plugins>
    <!-- springboot 打包 -->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <!-- mvn clean package docker:build -Dmaven.test.skip=true -->
    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>1.0.0</version>
        <configuration>
            <!-- 镜像名称  -->
            <imageName>maven-hello</imageName>
            <imageTags>0.0.1</imageTags>
            <!-- docker远程服务器地址 -->
            <dockerHost>http://192.168.80.200:2375</dockerHost>
            <!-- Dockerfile文件存放目录 -->
            <dockerDirectory>src/main/docker</dockerDirectory>
            <resources>
                <resource>
                    <directory>${project.build.directory}</directory>
                    <include>${project.build.finalName}.jar</include>
                </resource>
            </resources>
        </configuration>
    </plugin>
</plugins>

保存后,执行以下命令

~$ mvn clean package docker:build -Dmaven.test.skip=true

[图片上传失败...(image-82ca2c-1570859119714)]

看到执行成功后,远程登录80.200的服务,看下docker镜像

[图片上传失败...(image-626551-1570859119714)]

文献参考

【demo地址】(https://github.com/lenvonsam/mvhello.git)
【docker官网】(https://docs.docker.com)
【docker介绍】(https://www.cnblogs.com/boshen-hzb/p/6400272.html)
【kotlin介绍】(https://www.runoob.com/kotlin/kotlin-tutorial.html)

相关文章

网友评论

      本文标题:Springboot【2.x】+ Maven远程部署到Linux

      本文链接:https://www.haomeiwen.com/subject/oqsqmctx.html