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服务的类直接继承了多个接口, 是否会每个接口的方法都会注册。 经过验证确实如此
下一篇:
软件开发方法简史(收藏)