Dokcer14_6:Docker Compose搭建springboot项目
搭建步骤
- 编写微服务项目
- 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
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloWorld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
redis:
host: redis
Dockerfile
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml
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]
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]
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]
hello ==== 1[root@VM-0-3-centos helloworlddemo]
|