springcloud技术分享|grpc坑之Could not find TLS ALPN provider; no working netty-tcnative

在使用grpc过程中偶尔会遇到下面报错:

Initialization of bean failed; nestedexception is java.lang.IllegalStateException: Could not find TLS ALPN provider; no working netty-tcnative, Conscrypt, or Jetty NPN/ALPN available at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:581) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) at com.shein.RpcDemoApplication.main(RpcDemoApplication.java:14) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) Caused by: java.lang.IllegalStateException: Could not find TLS ALPN provider; no working netty-tcnative, Conscrypt, or Jetty NPN/ALPN available at io.grpc.netty.GrpcSslContexts.defaultSslProvider(GrpcSslContexts.java:258) at io.grpc.netty.GrpcSslContexts.configure(GrpcSslContexts.java:171) at io.grpc.netty.GrpcSslContexts.forClient(GrpcSslContexts.java:120) at net.devh.boot.grpc.client.channelfactory.NettyChannelFactory.configureSecurity(NettyChannelFactory.java:115) at net.devh.boot.grpc.client.channelfactory.NettyChannelFactory.configureSecurity(NettyChannelFactory.java:47) at net.devh.boot.grpc.client.channelfactory.AbstractChannelFactory.configure(AbstractChannelFactory.java:154) at net.devh.boot.grpc.client.channelfactory.AbstractChannelFactory.newManagedChannel(AbstractChannelFactory.java:131) at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) at net.devh.boot.grpc.client.channelfactory.AbstractChannelFactory.createChannel(AbstractChannelFactory.java:98) at net.devh.boot.grpc.client.inject.GrpcClientBeanPostProcessor.processInjectionPoint(GrpcClientBeanPostProcessor.java:110) at net.devh.boot.grpc.client.inject.GrpcClientBeanPostProcessor.postProcessBeforeInitialization(GrpcClientBeanPostProcessor.java:77) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1686) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ... 23 common frames omitted

出现上面的错误,分别有以下几种情况
第一种:grpc服务地址没有配置,这种情况要么是搞忘记配置,或者没有加载配置,也可能是是配置信息存储在配置中心,且服务中发布到docker容器中且继承了istio,这种是域名相关配置istio还没初始化,从而导致拉取配置失败
第二种:netty-tcnative相关的maven库没引入
根据grpc对应的版本,有以下对应关系
springcloud技术分享|grpc坑之Could not find TLS ALPN provider; no working netty-tcnative
文章图片

详情可以查看下面连接https://github.com/grpc/grpc-java/blob/master/SECURITY.md
由于我用的netty-grpc是1.15.1,所以这里配置是2.0.12.Final,如下
io.netty netty-tcnative 2.0.12.Final

【springcloud技术分享|grpc坑之Could not find TLS ALPN provider; no working netty-tcnative】第三种:如果是发布在容器中,需要更换jdk基础镜像
openjdk:8-jdk-alpine 修改为 openjdk:8
https://gitmemory.com/issue/grpc/grpc-java/5369/493463266

    推荐阅读