Step By Step
1、编写项目微服务;
2、dockerfile构建镜像;
3、docker-compose.yml编排项目;
4、docker-compose up 构建项目。
一、编写项目微服务
- 1.1 SpringBoot(2.2.5.RELEASE)依赖
<dependencies>
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 1.2 RestController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
StringRedisTemplate redisTemplate;
@GetMapping("/hello")
public String hello()
{
Long views = redisTemplate.opsForValue().increment("views");
return "浏览的次数:" + views;
}
}
- 1.3 application.properties
server.port=8080
spring.redis.host=redis
- 1.4 项目结构
- 1.5 项目打包
二、dockerfile构建镜像
- 2.1 Dockerfile文件内容
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
三、docker-compose.yml编排项目
- 3.1 docker-compose.yml内容
version: '3.3'
services:
taroapp:
build: .
image: taroapp
depends_on:
- redis
ports:
- "8080:8080"
redis:
image: "redis:alpine"
四、docker-compose up 构建项目
- 4.1 文件上传到ECS
- 4.2 docker-compose up --build