Dubbo的IllegalArgumentException問題的解決

異常信息

一次項目啓動失敗的異常信息。

**********  省略前面的堆棧信息  ***********

Caused by: java.lang.IllegalArgumentException: @Service interfaceClass() or interfaceName() or interface class must be present!
		at org.springframework.util.Assert.notNull(Assert.java:193)
		at com.alibaba.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor.resolveServiceInterfaceClass(ServiceAnnotationBeanPostProcessor.java:344)
		at com.alibaba.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor.registerServiceBean(ServiceAnnotationBeanPostProcessor.java:251)
		at com.alibaba.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor.registerServiceBeans(ServiceAnnotationBeanPostProcessor.java:143)
		at com.alibaba.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor.postProcessBeanDefinitionRegistry(ServiceAnnotationBeanPostProcessor.java:104)
		at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271)
		at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:121)
		at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:692)
		at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:530)
		at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
		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.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:157)
		at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:137)
		at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:91)
		at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
		at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
		... 44 more

異常信息: @Service interfaceClass() or interfaceName() or interface class must be present!
說的是dobbo提供的服務的類必須實現接口。但是下面的堆棧異常中有沒有打印出來有問題的異常的類的信息, 最後還是我一個個檢查dubbo服務發現問題。

問題原因:

dubbo提供服務的類必須直接實現接口, 間接繼承實現接口不行

重現問題

接口

public interface TestService {

    String  test();
}

spring容器中實現類

import com.souche.intention.dubbo.service.TestService;
import org.springframework.stereotype.Component;

@Component
public class TestServiceImpl implements TestService {
    @Override
    public String test() {
        return "test";
    }
}

提供的dubbo服務

@com.alibaba.dubbo.config.annotation.Service
public class DubboTestService extends TestServiceImpl {
}

啓動服務後果然報了同樣的錯誤。

因爲我圖方便, 直接集成了spring中的bean類,然後來發布dubbo服務,這樣我可以共用業務代碼,所以導致了這個問題

解決辦法

將dubbo服務類改成下面這樣既集成業務類, 也實現對應接口就可以了

@com.alibaba.dubbo.config.annotation.Service
public class DubboTestService extends TestServiceImpl  implements TestService{
}

改好後啓動項目. 啓動後在註冊中心找到了註冊的服務TestService。 問題解決

思考

java中類可以繼承多個接口, 如果dubbo服務的類直接繼承了多個接口,
是否會每個接口的方法都會註冊。 經過驗證確實如此

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章