일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- github actions
- docker
- 국비지원
- 백엔드
- 풀스택
- react native
- 프론트 엔드
- 개발
- Java
- 서버 배포
- JPA
- 리엑트
- 클론코딩
- Spring
- 개발자
- 비전공자
- 프론트엔드
- 무중단 배포
- react
- 스타트업
- 리엑트 네이티브
- 코딩
- 서버 개발자
- 스프링 부트
- Redux
- 스프링
- 자바
- react-native
- spring boot
- SQL
- Today
- Total
오티스의개발일기
무중단 배포 (8) [ spring boot HealthcheckController 작업 및 yml + Dockerfile 작업 편 ] spring boot + mysql + docker + github actions 본문
무중단 배포 (8) [ spring boot HealthcheckController 작업 및 yml + Dockerfile 작업 편 ] spring boot + mysql + docker + github actions
안되면 될때까지.. 2025. 1. 20. 10:04이번시간에는
무중단 배포를 위한 컨트롤러 작업과
추가적인 yml 작업 그리고
Dockerfile을 생성해보겠습니다.
목차
1. 프로젝트 생성
2. SQL 설정
3. git 생성
4. aws EC2 생성
8. spring boot HealthcheckController 작업 및 yml + Dockerfile 작업 😀
10. 최종 배포
전체 코드는 여기에 올라와있습니다.
https://github.com/1domybest/Spring_none_stop_deploy
GitHub - 1domybest/Spring_none_stop_deploy
Contribute to 1domybest/Spring_none_stop_deploy development by creating an account on GitHub.
github.com
상태 체크를 위한 컨트롤러 제작
HealthCheckController.java
package com.example.Spring_JWT.docker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@RestController
public class HealthCheckController {
@Value("${server.env}") // blue or green
private String env;
@Value("${server.port}") // 서버 포트 [이거를 스와이핑하여 무중단 배포]
private String serverPort;
@Value("${server.serverAddress}") // 서버 주소 [localhost or 탄력적 IP]
private String serverAddress;
@Value("${serverName}") // 서버이름
private String serverName;
/**
* 무중단 배포를 위한 상태체크 함수
* 이 함수를 통해 라우터를 변경해줌
* @return
*/
@GetMapping("/hc")
public ResponseEntity<?> healthCheck() {
// blue -> green
Map<String, String> responseData = new TreeMap<>();
responseData.put("serverName", serverName);
responseData.put("serverAddress", serverAddress);
responseData.put("serverPort", serverPort);
responseData.put("env", env);
return ResponseEntity.ok(responseData);
}
@GetMapping("/env")
public ResponseEntity<?> getEnv() {
return ResponseEntity.ok(env);
}
}
- application.yml 수정
# 무중단 배포를 위한 yml 설정
spring:
profiles:
active: local # 처음 활성화는 local 로 한다는 뜻
group:
local: local, common, local_secret
blue: blue, common, blue_secret
green: green, common, green_secret
config:
import: application-secret.yml
server:
env: blue
---
spring:
config:
activate:
on-profile: local
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
ddl-auto: none
properties:
hibernate:
format_sql: true
open-in-view: false
logging:
level:
org.hibernate.SQL: debug
---
spring:
config:
activate:
on-profile: blue
# JPA
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
ddl-auto: update
properties:
hibernate:
format_sql: true
open-in-view: false
---
spring:
config:
activate:
on-profile: green
# JPA
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
ddl-auto: update
properties:
hibernate:
format_sql: true
open-in-view: false
serverName: green_server
---
# 실서버 공통
spring:
config:
activate:
on-profile: common
---
- application-secret.yml
spring:
config:
activate:
on-profile: local
datasource:
url: jdbc:mysql://localhost:3306/springboot_noneStop_deploy?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 8080
serverAddress: localhost
serverName: local_server
---
spring:
config:
activate:
on-profile: blue
datasource:
url: jdbc:mysql://172.31.42.71:3306/springboot_noneStop_deploy?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 8080
serverAddress: 172.31.42.71
serverName: blue_server
---
spring:
config:
activate:
on-profile: green
datasource:
url: jdbc:mysql://172.31.42.71:3306/springboot_noneStop_deploy?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 8081
serverAddress: 172.31.42.71
serverName: green_server
---
여기에있는 172.31.2.171 이 IP는 EC2의 내부 프라이빗 IPv4 주소 입니다.
여기까지 완료가 되었으면
Dockerfile을 생성하겠습니다
가장 중요한건 Dockerfile은 최상단에 위치해야합니다.
이런식으로 프로젝트 명을 클릭한후 파일을 추가해야 최상단에 위치하게됩니다.
그리고 JavaClass가 아닌 그냥 단순 File로 만들어주시고 확장자는 없는 파일입니다.
Dockerfile
# OpenJDK 23 기반의 Gradle 이미지 사용
FROM gradle:7.6.2-jdk17-alpine
# ARG: 빌드 과정에서 사용할 변수 선언
ARG JAR_FILE=build/libs/*.jar
ARG PROFILES
ARG ENV
# 워크 디렉토리 설정
WORKDIR /app
# JAR 파일 복사
COPY ${JAR_FILE} app.jar
# ENTRYPOINT 명령어로 Java 애플리케이션 실행
ENTRYPOINT ["java", "-Dspring.profiles.active=${PROFILES}", "-Dserver.env=${ENV}", "-jar", "/app/app.jar"]
여기까지 완료가되었으면 그대로 git에 push 해줍니다.
여기까지해서 마무리하고
다음시간에는
github용 시크릿기능을 사용해 노출되면 안되는 키를 따로 저장하는 작업을 진행하고
github action을 사용해
workflow CICD.yml 파일을 생성해 보겠습니다.
다음글
spring boot + mysql + docker + github actions 무중단 배포 (9) [ Github Actions 설정 편 ]
이번시간에는Github Action을 사용한 무중단 배포를 하기위해설정을 하고 배포까지하여 마무리하는 방법을 보여드리겠습니다.배포하고 마무리하는 글은 다음글에서 이어서 이야기하겠습니다.
otis.tistory.com