Spring Native构建GRPC AOT镜像运行时提示NioSocketChannel反射配置缺失
GRPC AOT 运行缺失Shaded Netty类反射配置解决方案
你之前配置的@TypeHint(types = NioServerSocketChannel.class)未生效的核心原因是:你引入的是原生Netty包下的NioServerSocketChannel,但报错指向的是gRPC内部重打包的shaded版本类io.grpc.netty.shaded.io.netty.channel.socket.nio.NioSocketChannel,二者所属包路径完全不同,所以引导配置没有覆盖到目标类。
按照以下步骤操作即可解决:
- 新增自定义
RuntimeHintsRegistrar实现类,显式注册gRPC shaded版本Netty类的反射权限
示例代码如下:import io.grpc.netty.shaded.io.netty.channel.socket.nio.NioServerSocketChannel; import io.grpc.netty.shaded.io.netty.channel.socket.nio.NioSocketChannel; import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHintsRegistrar; public class GrpcShadedNettyHints implements RuntimeHintsRegistrar { @Override public void registerHints(RuntimeHints hints, ClassLoader classLoader) { // 注册shaded版本NioSocketChannel反射权限 hints.reflection().registerType(NioSocketChannel.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS ); // 注册shaded版本NioServerSocketChannel反射权限 hints.reflection().registerType(NioServerSocketChannel.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS ); } } - 在项目启动类上关联上述Hints配置
给启动类的@SpringBootApplication注解搭配@ImportRuntimeHints注解使用:import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportRuntimeHints; @ImportRuntimeHints(GrpcShadedNettyHints.class) @SpringBootApplication public class SimulacaoApplication { public static void main(String[] args) { SpringApplication.run(SimulacaoApplication.class, args); } } - 若后续出现同类型其他shaded类缺失反射配置的报错,直接将报错的类按上述格式补充到
GrpcShadedNettyHints的注册逻辑中即可。
内容的提问来源于stack exchange,提问作者Arthur Alves
相关产品推荐
相关产品推荐

