Dokcer14

Dokcer14,第1张

Dokcer14

Dokcer14_6:Docker Compose搭建springboot项目

搭建步骤HelloControllerpom.xmlapplication.ymlDockerfiledocker-compose.yml运行输出

搭建步骤
    编写微服务项目dockerfile构建镜像docker-compose.yml编排项目放到服务器执行docker-compose up
HelloController
package com.example.helloworld.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {


    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping("/hello")
    public String hello(){
        Long views = redisTemplate.opsForValue().increment("views");
        return "hello ==== "+views;
    }
}
pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.3
         
    
    com.example
    HelloWorld
    0.0.1-SNAPSHOT
    HelloWorld
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
            RELEASE
            compile
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-web
            RELEASE
            compile
        
    

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


application.yml
spring:
  redis:
    host: redis
Dockerfile
# 指定基础镜像,代码依托于jdk8来运行
FROM java:8

# 当前目录下的所有jar拷贝到app.jar
COPY *.jar /app.jar

CMD ["--server.port=8080"]

# 对外暴露端口8080
EXPOSE 8080

# 执行java -jar对当前目录下的app.jar文件
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml
# compose版本,向下兼容的,选择最高版本
version: '3.9'
# 定义服务
services:
  hellow_orld:
    build: .
    image: hellow_orld
    depends_on:
      - redis
    ports:
    - "8080:8080"
  redis:
    image: "library/redis:alpine"
运行输出
[root@VM-0-3-centos helloworlddemo]# docker-compose up -d
Building hellow_orld
Sending build context to Docker daemon  27.56MB

Step 1/5 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
 ---> bea79f8886a0
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in b72cbd5cb918
Removing intermediate container b72cbd5cb918
 ---> 30dcb328c62d
Step 4/5 : EXPOSE 8080
 ---> Running in 9124a1fde72e
Removing intermediate container 9124a1fde72e
 ---> 8d07b17afbac
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in 112e6cb7bb1d
Removing intermediate container 112e6cb7bb1d
 ---> 8302836dba05
Successfully built 8302836dba05
Successfully tagged hellow_orld:latest
WARNING: Image for service hellow_orld was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating helloworlddemo_redis_1 ... done
Creating helloworlddemo_hellow_orld_1 ... done
[root@VM-0-3-centos helloworlddemo]# docker ps
ConTAINER ID   IMAGE              COMMAND                  CREATED          STATUS         PORTS                                         NAMES
5b33999b74c1   hellow_orld        "java -jar /app.jar …"   9 seconds ago    Up 8 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp     helloworlddemo_hellow_orld_1
384f8a5968e3   redis:alpine       "docker-entrypoint.s…"   10 seconds ago   Up 8 seconds   6379/tcp                                      helloworlddemo_redis_1
6d25dfce59f5   wordpress:latest   "docker-entrypoint.s…"   2 days ago       Up 2 days      0.0.0.0:8000->80/tcp, :::8000->80/tcp         wordpress_wordpress_1
6f3764e26dd0   mysql:5.7          "docker-entrypoint.s…"   2 days ago       Up 2 days      3306/tcp, 33060/tcp                           wordpress_db_1
ec5e06e95677   redis:alpine       "docker-entrypoint.s…"   3 days ago       Up 3 days      6379/tcp                                      test_redis_1
c863e9cd5098   helloapp:0.1       "java -jar /app.jar …"   5 days ago       Up 5 days      0.0.0.0:49158->8080/tcp, :::49158->8080/tcp   helloapp01
[root@VM-0-3-centos helloworlddemo]# curl localhost:8080/hello
hello ==== 1[root@VM-0-3-centos helloworlddemo]#

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5715166.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存