美文网首页Docker 基础
DockerCompose 项目练习-java 微服务

DockerCompose 项目练习-java 微服务

作者: yjtuuige | 来源:发表于2021-07-17 17:51 被阅读0次

    docker-compose 测试 java 微服务

    一、编写项目微服务

    • HelloController.java 文件
    package com.example.demo;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        @RequestMapping("/hello")
        public String hello() {
            return "docker Spring Boot!--微服务测试";
        }
    }
    
    • application.properties 文件
    server.port = 8080
    spring.redis.host = redis        # 用服务名称代替IP地址,避免IP变化,不能连接
    

    二、创建 dockerfile 文件,构建镜像

    FROM openjdk:17-ea-jdk-oraclelinux8        # 基本环境,jdk版本不能低于编译环境
    COPY *.jar /app.jar        # 将当前目录的jar包拷贝到容器 /app.jar 中
    CMD ["--server.port=8080"]        # 指定端口  启动
    EXPOSE 8080        # 暴露的端口
    ENTRYPOINT ["java","-jar","/app.jar"]        # 运行 java 程序
    

    三、创建 docker-compose.yml 文件

    version: '3.9'
    services:
      webapp:        # 自定义服务名称
        build: .        # 构建镜像 
        image: webapp        # 自定义镜像名称
        depends_on:        # 启动顺序
          - redis
        ports:
          - "8080:8080"
      redis:        # redis 服务(名称可自定义),上文中的 spring.redis.host = redis 做连接
        image: redis:alpine
    

    四、在服务器启动微服务

    1、项目打包(.jar

    2、在 linux 中创建一个文件夹

    [root@VM-0-6-centos home]# mkdir docker_java
    [root@VM-0-6-centos home]# cd docker_java
    [root@VM-0-6-centos docker_java]# ll
    total 0
    [root@VM-0-6-centos docker_java]# pwd
    /home/docker_java
    

    3、上传文件至服务器:.jarDockerfiledocker-compose.yml

    4、启动 docker-compose up -d

    [root@VM-0-6-centos docker_java]# docker-compose up -d
    Creating network "docker_java_default" with the default driver
    Pulling redis (redis:alpine)...
    alpine: Pulling from library/redis
    5843afab3874: Pull complete
    9db2305878ef: Pull complete
    3558750a1d54: Pull complete
    240d71d1acc7: Pull complete
    2a888d25337f: Pull complete
    22e6fbce362a: Pull complete
    Digest: sha256:442fbfdeccf203c277827cfd8e7e727ce411611e1a6caeda9cca8115ed17b9cc
    Status: Downloaded newer image for redis:alpine
    Building webapp
    Sending build context to Docker daemon  17.33MB
    Step 1/5 : FROM openjdk:17-ea-jdk-oraclelinux8
    17-ea-jdk-oraclelinux8: Pulling from library/openjdk
    c828c776e142: Pull complete 
    8846dac42cae: Pull complete 
    f431879d4af7: Pull complete 
    Digest: sha256:cd8d4a275b8a524215da59c0f5b0ff373999cad236e24cde30f842d74eea66a9
    Status: Downloaded newer image for openjdk:17-ea-jdk-oraclelinux8
     ---> 9901faf47b8c
    Step 2/5 : COPY *.jar /app.jar
     ---> f88861ab682f
    Step 3/5 : CMD ["--server.port=8080"]
     ---> Running in 9827a9270b4b
    Removing intermediate container 9827a9270b4b
     ---> 16556550966c
    Step 4/5 : EXPOSE 8080
     ---> Running in 7229bfba148b
    Removing intermediate container 7229bfba148b
     ---> 82e0b5e56013
    Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
     ---> Running in e4d32bf080f8
    Removing intermediate container e4d32bf080f8
     ---> ca41a6e6bb60
    Successfully built ca41a6e6bb60
    Successfully tagged docker_java_webapp:latest
    WARNING: Image for service webapp was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
    # 告警,提示需要重新打包 `docker-compose build` or `docker-compose up --build`
    Creating docker_java_redis_1 ... done
    Creating docker_java_webapp_1 ... done
    

    5、项目重新部署打包

    • 停止运行的项目 docker-compose down
    [root@VM-0-6-centos docker_java]# docker-compose down
    Stopping docker_java_webapp_1 ... done
    Stopping docker_java_redis_1  ... done
    
    • 重新构建并启动
      docker-compose build 重新构建
      docker-compose up --build 重新构建,并启动!
    [root@VM-0-6-centos docker_java]# docker-compose up --build -d
    Building webapp
    Sending build context to Docker daemon  17.33MB
    Step 1/5 : FROM openjdk:17-ea-jdk-oraclelinux8
     ---> 9901faf47b8c
    Step 2/5 : COPY *.jar /app.jar
     ---> Using cache
     ---> f88861ab682f
    Step 3/5 : CMD ["--server.port=8080"]
     ---> Using cache
     ---> 16556550966c
    Step 4/5 : EXPOSE 8080
     ---> Using cache
     ---> 82e0b5e56013
    Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
     ---> Using cache
     ---> ca41a6e6bb60
    Successfully built ca41a6e6bb60
    Successfully tagged docker_java_webapp:latest
    Starting docker_java_redis_1 ... done
    Starting docker_java_webapp_1 ... done
    

    6、访问测试 curl ip:端口

    [root@VM-0-6-centos docker_java]# curl localhost:8080/hello
    docker Spring Boot!--微服务测试[root@VM-0-6-centos docker_java]# 
    
    • 浏览器中测试

    相关文章

      网友评论

        本文标题:DockerCompose 项目练习-java 微服务

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