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

本文解决了在使用Dubbo框架时遇到的服务注册失败的问题。通过在@DubboService注解中明确指定interfaceClass属性,成功避免了由于DubboService与Spring Service注解共存而导致的错误。

服务生产者注解是@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)
要创建一个集成了 DubboNacos Spring Boot 的示例项目,可以按照以下步骤进行配置开发。该项目将实现服务注册与发现、配置管理以及远程调用等功能。 ### 3.1 环境准备 - Java 1.8 或更高版本 - Maven 构建工具 - Spring Boot 2.x - Dubbo 2.7+ - Nacos Server(建议使用 2.0+ 版本) 确保已经安装并启动了 Nacos Server,并可以通过 `localhost:8848` 访问控制台。 ### 3.2 创建 Maven 项目结构 推荐使用多模块结构,便于管理服务提供者服务消费者: ``` dubbo-nacos-springboot-demo ├── dubbo-api (定义接口) ├── dubbo-provider (服务提供者) └── dubbo-consumer (服务消费者) ``` ### 3.3 添加依赖配置 在 `pom.xml` 中添加 Spring BootDubbo Nacos 的相关依赖: ```xml <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.8</version> </dependency> <!-- Nacos Service Discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.9.RELEASE</version> </dependency> <!-- Nacos Config Center --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.9.RELEASE</version> </dependency> ``` ### 3.4 配置 Nacos 注册中心配置中心 在 `bootstrap.yml` 中配置 Nacos 地址、应用名等信息: ```yaml server: port: 8080 spring: application: name: dubbo-service-provider cloud: nacos: discovery: server-addr: localhost:8848 # Nacos Server 地址 config: server-addr: localhost:8848 # Nacos Config 地址 file-extension: yaml ``` ### 3.5 定义服务接口 在 `dubbo-api` 模块中定义服务接口: ```java public interface DemoService { String sayHello(String name); } ``` ### 3.6 实现服务提供者 在 `dubbo-provider` 模块中编写服务实现类,并通过注解发布服务: ```java import org.apache.dubbo.config.annotation.DubboService; import org.springframework.stereotype.Component; @DubboService @Component public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { return "Hello, " + name; } } ``` 同时,在主类上启用 Dubbo Nacos 服务发现功能: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } } ``` ### 3.7 调用服务的消费者 在 `dubbo-consumer` 模块中注入远程服务: ```java import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @Reference private DemoService demoService; @GetMapping("/hello") public String hello(@RequestParam String name) { return demoService.sayHello(name); } } ``` 同样地,启用服务发现: ```java @SpringBootApplication @EnableDiscoveryClient public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } } ``` ### 3.8 启动与验证 - 启动 Nacos Server。 - 先启动服务提供者,再启动消费者。 - 在浏览器访问 `http://localhost:8080/hello?name=test`,如果返回 `"Hello, test"` 则表示调用成功。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

其实我是真性情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值