记录jdk8->jdk17 遇到的坑和解决方案

最近项目在升级jdk8->jdk17

springboot2->springboot3

顺序先升级业务服务,后升级组件服务。跟随迭代开发一起验证功能。

1.  使用parent pom 版本管理 spring相关组件的版本。 组件依赖低版本parent不变。

业务服务依赖高版本parent。

2. 修改maven jdk17 编译

maven-compiler-plugin

17

17

17

1.5.5.Final

3. security 

        1)新架构差异:新的 OAuth2 授权服务器采用完全不同的模块 ,删掉废弃spring-security-oauth2 引用spring-security-oauth2-client,spring-security-oauth2-authorization-server 

        2)改造security 中安全配置方法,支持spring security 6

ResourceServerConfigurerAdapter 类已经被移除,改用新的配置方式
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
            .authorizeHttpRequests(authz -> {
                oAuth2Config.getMapping().forEach((key, value) -> {
                    String[] urls = value.toArray(String[]::new);
                    authz.requestMatchers(urls).access((authentication, context) -> {
                        if (authentication instanceof OAuth2AuthenticationToken oAuth2Authentication) {
                            boolean hasScope = oAuth2Authentication.getAuthorities().stream()
                                    .anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("SCOPE_" + key));

                            return new AuthorizationDecision(hasScope);
                        }
                        return new AuthorizationDecision(false);
                    });
                });
                authz.requestMatchers("/oauth/token", "/oauth2/**", "/nano/**", "/test/**").permitAll();
            })
            .csrf(AbstractHttpConfigurer::disable);

    return http.build();
}

        3)旧框架已废弃:AuthorizationServerConfigurerAdapter 在 Spring Security 5.3+ 已废弃,Spring Security 6 完全移除

4.MybatisDynamicSqlInterceptor 删掉继承AbstractSqlParserHandler
- 适配MyBatis-Plus 3.5+版本
- 支持Spring Boot 3的依赖管理

5.HandlerInterceptorAdapter 改为实现 HandlerInterceptor 接口

6.import javax.annotation.Resource 改为import jakarta.annotation.Resource

7.MybatisPlusConfig 适配新3.5.0+

@Bean
public ConfigurationCustomizer configurationCustomizer() {
    return configuration -> {
       // 新版本已废弃 useDeprecatedExecutor 配置
       // 其他自定义配置可以放在这里
    };
}

8.javax.servlet 替换 jakarta.servlet

9.nacos1 升级到nacos2,部署nacos2服务端。要不会有兼容问题,同时要修改应用编排支持jdk17启动和nacos配置。

- env:
        - name: DEPLOY_ENV
          value: xxx
        - name: NACOS_SERVER_URL
          value: xxx
        - name: NACOS_NAMESPACE
          value: xxx
        - name: NACOS_GROUP
          value: xxx
        - name: NACOS_ENABLE
          value: "true"
        - name: SPRING_DISCOVERY_ENABLE
          value: "false"
        - name: OPEN_SQL
          value: open
        - name: JVM_OPTS
          value: -Xms512m -Xmx512m -XX:-OmitStackTraceInFastThrow -XX:+UseCountedLoopSafepoints
            -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/root/startup -Xlog:async
            -Xlog:gc*:file=/alidata1/admin/gc%t.log:utctime,level,tags:filecount=10,filesize=10M
            --add-opens java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED
        - name: NACOS_PWD
          value: xxx
        - name: NACOS_USER
          value: xxx
        - name: SPECIFIED_JAVA_HOME
          value: /opt/amazon-corretto-17.0.6.10.1-linux-x64
        - name: JAVA_OPTS
          value: -Dloader.path=./plugins -Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true

10.由于网关服务还没有升级jdk17, 而且nacos已经分为了高版本和低版本两个所以通过nacos服务发现方式转发路由已经不适用,单独采用了url链接方式。 其他服务还是使用serverid方式。

zuul:
  routes:
    xxx:
      path: /xxx/**
      #serviceId: xxxx
      url: xxx

你可能感兴趣的:(java,jdk17,zuul)