Dubbo2.7.8+Nacos @DubboService注解 和Spring Boot结合 遇坑

服务生产者注解是@DubboService
在本地启动不报错,在服务器上启动报如下错误,最可气的是,报错之后NACOS上还提示服务注册成功了,在NOCOS的服务列表里找到有一个健康实例:

java.lang.IllegalArgumentException: @Service interfaceClass() or interfaceName() or interface class must be present!
	at org.springframework.util.Assert.notNull(Assert.java:201)
	at org.apache.dubbo.config.spring.util.DubboAnnotationUtils.resolveServiceInterfaceClass(DubboAnnotationUtils.java:122)
	at org.apache.dubbo.config.spring.beans.factory.annotation.ServiceClassPostProcessor.registerServiceBean(ServiceClassPostProcessor.java:288)
	at org.apache.dubbo.config.spring.beans.factory.annotation.ServiceClassPostProcessor.registerServiceBeans(ServiceClassPostProcessor.java:175)
	at org.apache.dubbo.config.spring.beans.factory.annotation.ServiceClassPostProcessor.postProcessBeanDefinitionRegistry(ServiceClassPostProcessor.java:134)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:126)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at com.bba.rtm.vms.VehicleManagementServiceApplication.main(VehicleManagementServiceApplication.java:18)

我的代码结构和一般的代码结构不一样,为了防止未来将Dubbo替换成其他框架,因此我是将DubboService作为Controller层来用的,这样未来换框架我的Service和Dao层的代码都不用改了。
因此我的代码里存在@DubboService和@Service注解
具体包是:
org.apache.dubbo.config.annotation.DubboService

org.springframework.stereotype.Service

我一开始以为是2个包冲突,查了半天Dubbo扫描包的方式,都没解决,网上找的大部分帖子都是说@DubboService的注解类没有实现接口,没有 implements ,但是这种低级错误是不可能的,我的类有实现接口类,虽然包路径是不一样的,但是肯定是有的,网上说的都不太靠谱,最后从别的帖子里找到个属性interfaceClass

解决方式:
在@DubboService注解里,加入interfaceClass 属性还有一个
interfaceName属性,这个属性我没试,毕竟class更直接一些。代码如下:

@DubboService(group = "lwb", interfaceClass = com.lwb.dubbo.TestService.class)

注意,这里必须是类的全路径如果只写类名,因为上边import了导致写法不报错,但是启动还是找不到TestService这个接口,一样报错。

@DubboService(group = "lwb", interfaceClass = TestService.class)

猜你喜欢

转载自blog.csdn.net/lwb314/article/details/121075920