Dubbo 2.7應用級服務發(fā)現(xiàn)踩坑小記
背景
本文記錄最近一位讀者反饋的dubbo 2.7.x中應用級服務發(fā)現(xiàn)的問題,關于dubbo應用級服務發(fā)現(xiàn)的相關介紹可以參考之前的文章《dubbo應用級服務發(fā)現(xiàn)初體驗》,這里不再贅述。
讀者反饋他們在基于dubbo 2.7應用級服務發(fā)現(xiàn)開發(fā)dubbo網關,根據(jù)文章《dubbo應用級服務發(fā)現(xiàn)初體驗》寫了demo調用時報no provider的錯誤。
首先覺得他們挺有想法,把dubbo應用級服務發(fā)現(xiàn)搬上生產的公司不多。其次當時寫文章時測試并沒有遇到問題,但本著幫讀者解決問題的態(tài)度,還是重新寫個demo測試下。
問題定位
隨手拿了一個平時測試用的dubbo demo工程(注意不是dubbo源碼中的demo),發(fā)現(xiàn)確實注冊不到zookeeper上,接著測試了不同的版本,發(fā)現(xiàn)都注冊不了,在2.7.5 ~ 2.7.11版本不報錯,2.7.12版本會報如下的NPE錯誤
- 2021-06-16 13:17:31,086 [Dubbo-framework-scheduler-thread-1] ERROR org.apache.dubbo.config.bootstrap.DubboBootstrap (DubboBootstrap.java:1172) - [DUBBO] refresh metadata and instance failed, dubbo version: 2.7.12, current host: 172.23.233.52
- java.lang.NullPointerException
- at org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.calInstanceRevision(ServiceInstanceMetadataUtils.java:249)
- at org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.lambda$refreshMetadataAndInstance$6(ServiceInstanceMetadataUtils.java:272)
- at java.util.ArrayList.forEach(ArrayList.java:1259)
- at org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.refreshMetadataAndInstance(ServiceInstanceMetadataUtils.java:271)
- at org.apache.dubbo.config.bootstrap.DubboBootstrap.lambda$registerServiceInstance$20(DubboBootstrap.java:1170)
- at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
- at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
- at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
- at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
- at java.lang.Thread.run(Thread.java:748)
推測服務注冊時存在問題,跟著這個錯誤棧debug,很快就定位到問題
直接導致NPE的是位于org.apache.dubbo.registry.client.AbstractServiceDiscovery#register
在<=2.7.11版本中
- @Override
- public final void register(ServiceInstance serviceInstance) throws RuntimeException {
- this.serviceInstance = serviceInstance;
- doRegister(serviceInstance);
- }
而在2.7.12版本中代碼順序被調整成了
- @Override
- public final void register(ServiceInstance serviceInstance) throws RuntimeException {
- doRegister(serviceInstance);
- this.serviceInstance = serviceInstance;
- }
為什么調整了代碼順序導致了報錯?
追蹤下來發(fā)現(xiàn)NPE的來源是this.serviceInstance為null,原先的代碼先對其賦值再執(zhí)行doRegister。調整過后的代碼先執(zhí)行doRegister再賦值,然而在執(zhí)行diRegister時拋出異常了,不幸的是這個異常被吃掉了,doRegister的實現(xiàn)如下
- @Override
- public final void register(ServiceInstance serviceInstance) throws RuntimeException {
- assertDestroyed(REGISTER_ACTION);
- assertInitialized(REGISTER_ACTION);
- executeWithEvents(
- of(new ServiceInstancePreRegisteredEvent(serviceDiscovery, serviceInstance)),
- () -> serviceDiscovery.register(serviceInstance),
- of(new ServiceInstanceRegisteredEvent(serviceDiscovery, serviceInstance))
- );
- }
而這個executeWithEvents會將異常以事件的形式發(fā)出去
- protected final void executeWithEvents(Optional<? extends Event> beforeEvent,
- ThrowableAction action,
- Optional<? extends Event> afterEvent) {
- beforeEvent.ifPresent(this::dispatchEvent);
- try {
- action.execute();
- } catch (Throwable e) {
- dispatchEvent(new ServiceDiscoveryExceptionEvent(this, serviceDiscovery, e));
- }
- afterEvent.ifPresent(this::dispatchEvent);
- }
然而這個事件丟出去之后并沒有被處理,也就是說這個異常被吃掉了。這也就是為什么之前的dubbo版本沒有拋出異常,也不能注冊上服務。
這個異常是什么?
- va.lang.NoClassDefFoundError: org/apache/curator/x/discovery/ServiceDiscovery
其實只是少引入了一個依賴。加入以下以下就能解決這個問題
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-registry-zookeeper</artifactId>
- <version>${version}</version>
- </dependency>
為什么當時寫文章時沒有遇到這個問題?
恰好本次調試是基于一個demo工程,《dubbo應用級服務發(fā)現(xiàn)初體驗》文章是直接在dubbo源碼中修改demo,源碼中已經把依賴都引入了
更進一步
其實是個小問題,但對用戶來說挺困惑的,為什么沒有報錯但也沒法注冊服務?如果不是2.7.12有個附帶的報錯,可能排查起來更加困難。
于是提了個issue和社區(qū)的朋友交流下,得出的結論是2.7.x的應用級服務發(fā)現(xiàn)不再維護,3.x會繼續(xù)維護。
https://github.com/apache/dubbo/issues/8061
提個issue也是讓有問題的用戶能搜索到,少走彎路。
順便也提了個PR,加一行l(wèi)og,方便直觀的發(fā)現(xiàn)這個問題。
https://github.com/apache/dubbo/pull/8066
新版本(>=2.7.13)如果有朋友再遇到這個問題,會直接打印出錯誤,就像這樣
- 2021-06-16 16:58:02,210 [main] ERROR org.apache.dubbo.registry.client.EventPublishingServiceDiscovery (EventPublishingServiceDiscovery.java:287) - [DUBBO] Execute action throws and dispatch a ServiceDiscoveryExceptionEvent, dubbo version: 2.7.12, current host: 172.23.233.52
- java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: org/apache/curator/x/discovery/ServiceDiscovery
- at org.apache.dubbo.registry.zookeeper.ZookeeperServiceDiscovery.doRegister(ZookeeperServiceDiscovery.java:92)
- at org.apache.dubbo.registry.client.AbstractServiceDiscovery.register(AbstractServiceDiscovery.java:33)
- at org.apache.dubbo.registry.client.EventPublishingServiceDiscovery.lambda$register$0(EventPublishingServiceDiscovery.java:159)
- at org.apache.dubbo.registry.client.EventPublishingServiceDiscovery.executeWithEvents(EventPublishingServiceDiscovery.java:285)
- at org.apache.dubbo.registry.client.EventPublishingServiceDiscovery.register(EventPublishingServiceDiscovery.java:157)
- at org.apache.dubbo.config.bootstrap.DubboBootstrap.lambda$doRegisterServiceInstance$21(DubboBootstrap.java:1192)
- at java.util.ArrayList.forEach(ArrayList.java:1259)
- ...
既然2.7.x的應用級服務發(fā)現(xiàn)不再更新,下次寫一篇分析3.0版本的應用級服務發(fā)現(xiàn)源碼的文章吧~