SpringBoot/개인프로젝트

https 적용 후 무중단 배포가 안되는 문제

ewok 2023. 5. 2. 23:11

배포 시 스크립트 파일에 따라

 

application-real1.properties

server.port=8081
spring.profiles.include=real1,oauth,real-db
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.jpa.properties.hibernate.dialect.storage_engine=innodb
spring.session.store-type=jdbc

 

application-real2.properties

server.port=8082
spring.profiles.include=real2,oauth,real-db
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.jpa.properties.hibernate.dialect.storage_engine=innodb
spring.session.store-type=jdbc

 

8081과 8082 포트를 번갈아 가며 사용하게 되어 무중단 배포가 가능해진다. 하지만 https 적용 후 아래와 같이 profile.sh의 15 라인에 매개변수가 많다는 오류가 나온다.

 

profile.sh를 살펴보면 http://localhost/profile을 호출해서 현재 활성화 된 프로필 정보를 가져온다.

#!/usr/bin/env bash

# 쉬고 있는 profile 찾기: real1이 사용 중이면 real2가 쉬고 있고, 반대면 real1이 쉬고 있음

function find_idle_profile() {
    RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost/profile)

    if [ ${RESPONSE_CODE} -ge 400 ]  # 400보다 크면 (즉, 40x/50x 모두 포함)
    then
      CURRENT_PROFILE=real2
    else
      CURRENT_PROFILE=$(curl -s http://localhost/profile)
    fi

    if [ ${CURRENT_PROFILE} == real1 ]
    then
      IDLE_PROFILE=real2
    else
      IDLE_PROFILE=real1
    fi

    echo "${IDLE_PROFILE}"
}

# 쉬고 있는 profile의 port 찾기
function find_idle_port() {
    IDLE_PROFILE=$(find_idle_profile)

    if [ ${IDLE_PROFILE} == real1 ]
    then
      echo "8081"
    else
      echo "8082"
    fi
}
@RequiredArgsConstructor
@RestController
public class ProfileController {
    private final Environment env;

    @GetMapping("/profile")
    public String profile() {
        List<String> profiles = Arrays.asList(env.getActiveProfiles());
        List<String> realProfiles = Arrays.asList("real", "real1", "real2");
        String defaultProfile = profiles.isEmpty() ? "default" : profiles.get(0);

        return profiles.stream()
                .filter(realProfiles::contains)
                .findAny()
                .orElse(defaultProfile);
    }
}

 

하지만 이 상황에서 http://localhost/profile을 호출하면 301 Moved Permanently가 나온다. 이것 때문에 too many arguments 에러가 발생한 것이다.

 

이를 해결하기 위해 nginx의 설정을 변경해보았다.

 

 

기존

 

변경

 

변경하고 나니 현재 활성화된 프로필이 제대로 나온다.

그리고 여전히 http로 접속해도 https로 접속되며 자물쇠가 걸려있다.

 

그리고 다시 배포를 해보면

이번에는 제대로 프로필이 변경되어 real2가 나온다.