nacos-group / nacos-spring-boot-project

Nacos ECO Project for Spring Boot
Apache License 2.0
783 stars 423 forks source link

[ISSUE #10375] Support springboot3 and native-image #306

Closed SuperZ1999 closed 11 months ago

SuperZ1999 commented 11 months ago

Please do not create a Pull Request without creating an issue first.

What is the purpose of the change

适配springboot3 和 native-image

Brief changelog

适配SpringBoot3

根据SpringBoot3的自动配置方式,添加org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,然后更新依赖的nacos-spring-context版本

支持native-image

  1. 首先新建nacos-spring-boot-aot项目,该项目的作用是springboot插件的process-aot目标运行时扫描所有添加的bean,如果bean里使用了@NacosInjected注解或@NacosValue注解,就把注解标注的属性添加到reflect-config.json里面
  2. 因为AOT运行时Spring不会调用ImportBeanDefinitionRegistrar的registerBeanDefinitions方法,所以将registerBeanDefinitions方法中除了注册BeanDefinition操作的其他操作放到EnableNacosDiscoveryAotProcessor和EnableNacosConfigAotProcessor里
  3. AOT运行时@NacosPropertySource注解标注的类的BeanDefinition是RootBeanDefinition,需要将这些RootBeanDefinition转换为AnnotatedGenericBeanDefinition
  4. 添加native-image编译所需的配置文件,然后其他模块导入nacos-spring-boot-aot依赖

Verifying this change

适配SpringBoot3

首先将https://nacos.io/zh-cn/docs/nacos-spring.html里的功能都debug测试一遍,debug时统计没有走到的代码,然后运行所有单元测试即可测试上述没有走到的代码

支持native-image

新建一个项目,导入所有starter和actuator项目,开启GraalVM的Tracing Agent功能(运行java程序时添加参数:-agentlib:native-image-agent=config-output-dir=/path/to/config-dir/),将https://nacos.io/zh-cn/docs/nacos-spring.html里的功能测试一遍,之后将生成的配置文件拷贝到META-INF/native-image下,再使用spring-boot-maven-plugin的process-aot和native-maven-plugin的compile功能,运行mvn clean package -Pnative即可生成可执行程序。 运行生成的可执行程序,再将https://nacos.io/zh-cn/docs/nacos-spring.html里的功能测试一遍,功能正常即测试通过

Follow this checklist to help us incorporate your contribution quickly and easily: