diff --git a/README.md b/README.md index d72c493..41d6db8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # ๐ฆ Team Backend ๐ฆ ๐ ๋ชจ์ฌํค ๋ฐฑ์๋ ๋ ํฌ์งํ ๋ฆฌ +โ ๏ธ ํธ์ฌํ๋ค๋ฉด ๊ผญ ๋งํด์ฃผ๊ธฐ + ## ๐งโ๐ป ๋ฉค๋ฒ |์ด๋ฆ|[์ด์์](https://github.com/eunxeum)|[์ก์ฑ๋ฏผ](https://github.com/tjdals4716)|[๋ฐ์ ์](https://github.com/Zero982)| |:---:|:---:|:---:|:---:| @@ -21,7 +23,7 @@ ### ๐ Deployment
- +
### ๐ฃ๏ธ Communication @@ -32,7 +34,4 @@ ## ์๋ฒ ํ๋ก์ฐ ๋์-
- -## API ๋์ -- ์ถ๊ฐ ์์ \ No newline at end of file + \ No newline at end of file diff --git a/appspec.yml b/appspec.yml new file mode 100644 index 0000000..068b167 --- /dev/null +++ b/appspec.yml @@ -0,0 +1,26 @@ +version: 0.0 #CodeDeploy ๋ฒ์ ์(๋ฌด์กฐ๊ฑด 0.0์ผ๋ก ๊ณ ์ ) +os: linux +files: + - source: / #destination์ผ๋ก ์ด๋์ํฌ ํ์ผ. ์ฌ๊ธฐ์๋ ์ ์ฒด ํ์ผ์ ์๋ฏธ. + destination: /home/ec2-user/app/zip/ #source์์ ์ง์ ๋ ํ์ผ์ ๋ฐ๋ ์์น + overwrite: yes #๊ธฐ์กด ํ์ผ๋ค์ ๋ฎ์ด์ธ์ง ์ฌ๋ถ + +permissions: #CodeDeploy์์ EC2 ์๋ฒ๋ก ๋๊ฒจ์ค ํ์ผ๋ค์ ๋ชจ๋ ec2-user ๊ถํ์ ๊ฐ๋๋ก ํจ + - object: / + pattern: "**" + owner: ec2-user + group: ec2-user + +hooks: + AfterInstall: + - location: stop.sh #์์ง์์ค์ ์ฐ๊ฒฐ๋์ด ์์ง ์์ ์คํ๋ง ๋ถํธ๋ฅผ ์ข ๋ฃ + timeout: 60 + runas: ec2-user #stop.sh๋ฅผ ec2-user ๊ถํ์ผ๋ก ์คํ + ApplicationStart: + - location: start.sh #์์ง์์ค์ ์ฐ๊ฒฐ๋์ด ์์ง ์์ Port๋ก ์ ๋ฒ์ ์ ์คํ๋ง ๋ถํธ๋ฅผ ์์ + timeout: 60 + runas: ec2-user + ValidateService: + - location: health.sh #์ ์คํ๋ง ๋ถํธ๊ฐ ์ ์์ ์ผ๋ก ์คํ๋๋์ง ํ์ธ + timeout: 60 + runas: ec2-user \ No newline at end of file diff --git a/build.gradle b/build.gradle index 5358dcc..8c73de6 100644 --- a/build.gradle +++ b/build.gradle @@ -56,3 +56,7 @@ dependencies { tasks.named('test') { useJUnitPlatform() } + +jar{ + enabled = false +} \ No newline at end of file diff --git a/scripts/health.sh b/scripts/health.sh new file mode 100644 index 0000000..e0c9830 --- /dev/null +++ b/scripts/health.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +ABSPATH=$(readlink -f $0) +ABSDIR=$(dirname $ABSPATH) +source ${ABSDIR}/profile.sh +source ${ABSDIR}/switch.sh + +IDLE_PORT=$(find_idle_port) + +echo "> Health Check Start!" +echo "> IDLE_PORT: $IDLE_PORT" +echo "> curl -s http://127.0.0.1:$IDLE_PORT/profile" +sleep 10 + +for RETRY_COUNT in {1..10} +do + RESPONSE=$(curl -s http://127.0.0.1:${IDLE_PORT}/profile) + UP_COUNT=$(echo ${RESPONSE} | grep 'real' | wc -l) + + if [ ${UP_COUNT} -ge 1 ] # Nginx์ ์ฐ๊ฒฐ๋์ง ์์ ํฌํธ๋ก ์คํ๋ง ๋ถํธ๊ฐ ์ ์คํ๋์๋์ง ์ฒดํฌ + then # $up_count >= 1 ("real" ๋ฌธ์์ด์ด ์๋์ง ๊ฒ์ฆ) + echo "> Health Check ์ฑ๊ณต" + switch_proxy # ์ ์คํ๋์ด ์๋ค๋ฉด ํ๋ก์ ์ค์ ์ ๋ณ๊ฒฝ + break + else + echo "> Health Check์ ์๋ต์ ์ ์ ์๊ฑฐ๋ ํน์ ์คํ ์ํ๊ฐ ์๋๋๋ค." + echo "> Health Check: ${RESPONSE}" + fi + + if [ ${RETRY_COUNT} -eq 10 ] + then + echo "> Health check ์คํจ. " + echo "> ์์ง์์ค์ ์ฐ๊ฒฐํ์ง ์๊ณ ๋ฐฐํฌ๋ฅผ ์ข ๋ฃํฉ๋๋ค." + exit 1 + fi + + echo "> Health check ์ฐ๊ฒฐ ์คํจ. ์ฌ์๋..." + sleep 10 +done \ No newline at end of file diff --git a/scripts/profile.sh b/scripts/profile.sh new file mode 100644 index 0000000..462762b --- /dev/null +++ b/scripts/profile.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +# ์ฌ๊ณ ์๋ profile ์ฐพ๊ธฐ +# real1์ด ์ฌ์ฉ ์ค์ด๋ฉด real2๊ฐ ์ฌ๊ณ ์๊ณ ๋ฐ๋๋ฉด real1์ด ์ฌ๊ณ ์์ + +function find_idle_profile() { + RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://newteamsgoody.shop/profile) # ํ์ฌ Nginx๊ฐ ๋ฐ๋ผ๋ณด๊ณ ์๋ ์คํ๋ง ๋ถํธ๊ฐ ์ ์์ ์ผ๋ก ์ํ ์ค์ธ์ง ํ์ธํ๊ณ ์๋ต๊ฐ์ผ๋ก ์ํ์ฝ๋๋ฅผ ์ ๋ฌ๋ฐ์ + + if [ ${RESPONSE_CODE} -ge 400 ] # 400๋ฒ๋ ์ด์์ ์ค๋ฅ์ผ ๊ฒฝ์ฐ real2๋ฅผ ์ฌ์ฉ + then + CURRENT_PROFILE=real2 + else + CURRENT_PROFILE=$(curl -s https://newteamsgoody.shop/profile) + fi + + if [ ${CURRENT_PROFILE} == real1 ] + then + IDLE_PROFILE=real2 + else + IDLE_PROFILE=real1 + fi + + echo "${IDLE_PROFILE}" # bash ์คํฌ๋ฆฝํธ๋ ๋ฐํ ๊ธฐ๋ฅ์ด ์๊ธฐ ๋๋ฌธ์ echo๋ก ๊ฐ์ ์ถ๋ ฅํ๊ณ ํด๋ผ์ด์ธํธ์์ ๊ทธ ๊ฐ์ ์ก์์ ์ฌ์ฉ +} + +# ์ฌ๊ณ ์๋ profile์ port ์ฐพ๊ธฐ +function find_idle_port() { + IDLE_PROFILE=$(find_idle_profile) + + if [ ${IDLE_PROFILE} == real1 ] + then + echo "8081" + else + echo "8082" + fi +} \ No newline at end of file diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100644 index 0000000..0ff9da2 --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +ABSPATH=$(readlink -f $0) +ABSDIR=$(dirname $ABSPATH) +source ${ABSDIR}/profile.sh + +REPOSITORY=/home/ec2-user/app + +echo "> Build ํ์ผ์ ๋ณต์ฌํฉ๋๋ค." + +cp $REPOSITORY/zip/*.jar $REPOSITORY/ + +echo "> ์ ์ ํ๋ฆฌ์ผ์ด์ ๋ฐฐํฌ" +JAR_NAME=$(ls -tr $REPOSITORY/*.jar | tail -n 1) + +echo "> JAR NAME: $JAR_NAME" + +echo "> $JAR_NAME ์ ์คํ ๊ถํ์ ๋ถ์ฌํฉ๋๋ค." + +chmod +x $JAR_NAME + +IDLE_PROFILE=$(find_idle_profile) + +echo "> ์ ์ ํ๋ฆฌ์ผ์ด์ ์ $IDLE_PROFILE ๋ก ์คํํฉ๋๋ค." + +# ์ค์ ํ์ผ์ ์์น๋ฅผ ์ง์ ํ๊ณ active profile์ ํตํด ๊ตฌ๋๋ ํฌํธ๋ฅผ ์ง์ +nohup java -jar \ +-Dspring.config.location=$REPOSITORY/config/application.yml,\ +$REPOSITORY/config/application-prod.yml,\ +$REPOSITORY/config/application-$IDLE_PROFILE.yml \ +-Dspring.profiles.active=$IDLE_PROFILE,prod \ +$JAR_NAME > $REPOSITORY/nohup.out 2>&1 & \ No newline at end of file diff --git a/scripts/stop.sh b/scripts/stop.sh new file mode 100644 index 0000000..c082b70 --- /dev/null +++ b/scripts/stop.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +ABSPATH=$(readlink -f $0) # ํ์ฌ stop.sh๊ฐ ์ํด ์๋ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ + +ABSDIR=$(dirname $ABSPATH) +source ${ABSDIR}/profile.sh # ์ผ์ข ์ import ๊ตฌ๋ฌธ์ผ๋ก stop.sh์์๋ profile.sh์ function์ ์ฌ์ฉํ ์ ์๊ฒ ํจ + +IDLE_PORT=$(find_idle_port) + +echo "> $IDLE_PORT ์์ ๊ตฌ๋ ์ค์ธ ์ ํ๋ฆฌ์ผ์ด์ pid ํ์ธ" +IDLE_PID=$(lsof -ti tcp:${IDLE_PORT}) + +if [ -z ${IDLE_PID} ] +then + echo "> ํ์ฌ ๊ตฌ๋ ์ค์ธ ์ ํ๋ฆฌ์ผ์ด์ ์ด ์์ผ๋ฏ๋ก ์ข ๋ฃํ์ง ์์ต๋๋ค." +else + echo "> kill -9 $IDLE_PID" + kill -9 ${IDLE_PID} + sleep 5 +fi \ No newline at end of file diff --git a/scripts/switch.sh b/scripts/switch.sh new file mode 100644 index 0000000..6eb49bc --- /dev/null +++ b/scripts/switch.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +ABSPATH=$(readlink -f $0) +ABSDIR=$(dirname $ABSPATH) +source $ABSDIR/profile.sh + +function switch_proxy() { + IDLE_PORT=$(find_idle_port) + + echo "> ์ ํํ port: $IDLE_PORT" + echo "> Port ์ ํ" + echo "set \$service_url http://127.0.0.1:${IDLE_PORT};" | sudo tee /etc/nginx/conf.d/service-url.inc # ์์ง์์ค๊ฐ ๋ณ๊ฒฝํ ํ๋ก์ ์ฃผ์๋ฅผ ์์ฑํ์ฌ service-url.inc๋ก ๋ฎ์ด ์ + + echo "> ์์ง์์ค Reload" + sudo systemctl reload nginx +} \ No newline at end of file diff --git a/src/main/java/com/example/moyeothon/Config/Swagger/SwaggerConfig.java b/src/main/java/com/example/moyeothon/Config/Swagger/SwaggerConfig.java index 89a6aa0..54d1fbc 100644 --- a/src/main/java/com/example/moyeothon/Config/Swagger/SwaggerConfig.java +++ b/src/main/java/com/example/moyeothon/Config/Swagger/SwaggerConfig.java @@ -6,6 +6,7 @@ import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; import io.swagger.v3.oas.models.servers.Server; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -14,6 +15,9 @@ @Configuration public class SwaggerConfig { + @Value("${swagger.server-url}") + private String serverUrl; + @Bean public OpenAPI openAPI() { String securitySchemeName = "๋ก๊ทธ์ธ ํ ๋ฐ๊ธ๋ฐ์ JWT ์ ์ฉํ๊ธฐ"; @@ -28,13 +32,13 @@ public OpenAPI openAPI() { )) .addSecurityItem(new SecurityRequirement().addList(securitySchemeName)) .info(apiInfo()) - .servers(List.of(new Server().url("https://newteamsgoody.com"))); // HTTPS ์๋ฒ ์ค์ + .servers(List.of(new Server().url(serverUrl))); } private Info apiInfo() { return new Info() .title("\uD83E\uDD81 ๋ฉ์ฌ ๋ชจ์ฌํค 10ํ ์ค์จ๊ฑฐ \uD83D\uDC8E") - .description("๋ชจ์ฌํค 10ํ์ ์ค์จ๊ฑฐ์ ๋๋ค.") + .description("๋ชจ์ฌํค 10ํ์ ์ค์จ๊ฑฐ์ ๋๋ค") .version("1.0.0"); } } diff --git a/src/main/java/com/example/moyeothon/Controller/MessageController.java b/src/main/java/com/example/moyeothon/Controller/MessageController.java index a971013..a84426d 100644 --- a/src/main/java/com/example/moyeothon/Controller/MessageController.java +++ b/src/main/java/com/example/moyeothon/Controller/MessageController.java @@ -46,8 +46,8 @@ public ResponseEntity