ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring Boot] Spring Boot 2.x에서 Spring Boot 3.x으로 버전 변경
    Spring Boot/기타 2023. 9. 6. 09:14
    반응형

    1. Java 17로 업그레이드

    Spring Boot 3.x부터는 Java 17 이상 버전이 필요하므로 프로젝트의 JDK를 업그레이드하세요.
    build.gradle

    java {
        sourceCompatibility = '17'
    }

    참고 : https://spring.io/blog/2022/05/24/preparing-for-spring-boot-3-0

     

    2. Gradle 버전 업그레이드

    Gradle 7.x(7.5 이상) 또는 8.x 이상 버전이 필요하므로 프로젝트의 Gradle를 업그레이드하세요.

    gradle-wrapper.properties

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
    networkTimeout=10000
    validateDistributionUrl=true
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists

    참고 : https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#introduction

    [IntelliJ] Gradle 버전 확인 및 변경 : https://tychejin.tistory.com/388

     

    3. Spring Boot 2.x에서 Spring Boot 3.x로 버전 변경

    Spring Boot 2.x에서 Spring Boot 3.x로 버전으로 변경하려면, org.springframework.boot를 변경해야 합니다.
    build.gradle

    plugins {
        id 'org.springframework.boot' version '3.1.3'
    }

     

    4. javax 패키지의 변경

    Java EE에서 Jakarta EE로 변경되어, javax 패키지를 jakarta 패키지로 변경해야 합니다.

    • javax.persistence.* -> jakarta.persistence.*
    • javax.validation.* -> jakarta.validation.*
    • javax.servlet.* -> jakarta.servlet.*
    • javax.annotation.* -> jakarta.annotation.*
    • javax.transaction.* -> jakarta.transaction.*


    5. Querydsl 설정 변경

    javax.persistence.가 jakarta.persistence.로 변경되어, Querydsl 관련 build.gradle 설정을 다음과 같이 변경해야 합니다.
    Spring Boot 2.x

    implementation 'com.querydsl:querydsl-core'
    implementation 'com.querydsl:querydsl-jpa:5.0.0'
    annotationProcessor("com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa")
    annotationProcessor 'jakarta.persistence:jakarta.persistence-api'
    annotationProcessor 'jakarta.annotation:jakarta.annotation-api'


    Spring Boot 3.x

    implementation 'com.querydsl:querydsl-core'
    implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
    annotationProcessor("com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta")
    annotationProcessor 'jakarta.persistence:jakarta.persistence-api'
    annotationProcessor 'jakarta.annotation:jakarta.annotation-api'

     

    6. Properties 설정 변경

    Redis의 경우 spring.redis가 spring.data.redis 으로 변경되어, Redis Properties 설정을 다음과 같이 변경해야 합니다.
    Spring Boot 2.x

    spring.redis.database=0
    spring.redis.host=localhost
    spring.redis.port=16379
    spring.redis.password=mypass
    spring.redis.timeout=60000


    Spring Boot 3.x

    spring.data.redis.database=0
    spring.data.redis.host=localhost
    spring.data.redis.port=16379
    spring.data.redis.password=mypass
    spring.data.redis.timeout=60000

    참고 : https://www.baeldung.com/spring-data-redis-properties

     

    6. Apache HttpClient 변경

    Spring Boot 3.1에서는 Apache HttpClient 4에 대한 지원을 하지 않기 때문에 Apache HttpClient 5로 변경해야 합니다.
    Apache HttpClient 4

    import com.daekyo.expr.web.interceptor.ClientHttpInterceptor;
    import java.io.IOException;
    import java.util.Arrays;
    import org.apache.http.client.HttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.client.BufferingClientHttpRequestFactory;
    import org.springframework.http.client.ClientHttpRequestInterceptor;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.retry.policy.SimpleRetryPolicy;
    import org.springframework.retry.support.RetryTemplate;
    import org.springframework.web.client.RestTemplate;
    
    @Configuration
    public class RetryTemplateConfig {
    
      @Bean
      public RestTemplate retryTemplate() {
    
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setConnectTimeout(5000);
        factory.setReadTimeout(3000);
        HttpClient httpClient = HttpClientBuilder.create()
            .setMaxConnTotal(50)
            .setMaxConnPerRoute(20)
            .build();
        factory.setHttpClient(httpClient);
    
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(factory));
        restTemplate.setInterceptors(
            Arrays.asList(
                clientHttpRequestInterceptor(),
                new ClientHttpInterceptor())
        );
        return restTemplate;
      }
    
      public ClientHttpRequestInterceptor clientHttpRequestInterceptor() {
        return (request, body, execution) -> {
          RetryTemplate retryTemplate = new RetryTemplate();
          retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));
          try {
            return retryTemplate.execute(context -> execution.execute(request, body));
          } catch (IOException e) {
            throw new RuntimeException(e);
          }
        };
      }
    }



    Apache HttpClient 5

    import com.daekyo.expr.web.interceptor.ClientHttpInterceptor;
    import java.io.IOException;
    import java.util.Arrays;
    import org.apache.hc.client5.http.classic.HttpClient;
    import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
    import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.client.BufferingClientHttpRequestFactory;
    import org.springframework.http.client.ClientHttpRequestInterceptor;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.retry.policy.SimpleRetryPolicy;
    import org.springframework.retry.support.RetryTemplate;
    import org.springframework.web.client.RestTemplate;
    
    @Configuration
    public class RetryTemplateConfig {
    
      @Bean
      public RestTemplate retryTemplate() {
    
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setConnectTimeout(5000);
        HttpClient httpClient = HttpClientBuilder.create()
            .setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
                .setMaxConnPerRoute(100)
                .setMaxConnTotal(300)
                .build())
            .build();
        factory.setHttpClient(httpClient);
    
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(factory));
        restTemplate.setInterceptors(
            Arrays.asList(
                clientHttpRequestInterceptor(),
                new ClientHttpInterceptor())
        );
        return restTemplate;
      }
    
      public ClientHttpRequestInterceptor clientHttpRequestInterceptor() {
        return (request, body, execution) -> {
          RetryTemplate retryTemplate = new RetryTemplate();
          retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));
          try {
            return retryTemplate.execute(context -> execution.execute(request, body));
          } catch (IOException e) {
            throw new RuntimeException(e);
          }
        };
      }
    }

    참고 : https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.1-Release-Notes




     

    반응형

    댓글

Designed by Tistory.