spring+dubbo环境搭建网上汗牛充栋,但是实践过程中遇到种种麻烦,小记一篇,免得自己或者别人再次踩坑。 主要的问题是,项目需要引入哪些包,对有些版本过时的配置该如何修正,遇到异常如何处理(hello world应用代码极少,主要是配置问题)
参考文章Idea创建dubbo项目步骤和简单实例开发(dubo+zookeeper)
文章写的非常清楚,我们主要聚焦几个问题
1 dubboprovider 和 dubboconsumer的 pom配置
bash
<!-- dubboprovider和dubbocomsumer 都需要引入,提供了dubbo注解,并会自动关联相应dubbo包,这里是2.6.2 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- dubboprovider和dubbocomsume spring web的启动器,不引入会遇到 4 那种问题 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
只有dubboconsumer用到的包
bash
<!-- Controller需要用到 @RequestMapping 等-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
2 dubboprovider 和 dubboconsumer的 application.properties文件配置
dubboprovider 相关配置
bash
# apllication name
dubbo.application.name=dubboprovider
# register center
dubbo.registry.protocol=zookeeper
dubbo.registry.address=localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.monitor.protocol=registry
#开启包扫描,因代码有 @EnableDubbo,所以不需要
#dubbo.scan.base-packages=com.dubbolearn.dubboprovider.service.impl
dubboconsumer需要的配置
server.port=7070
bash
server.port=7070
# apllication name
dubbo.application.name=dubboconsumer
# register center
dubbo.registry.protocol=zookeeper
dubbo.registry.address=localhost:2181
#dubbo.protocol.name=dubbo
#dubbo.protocol.port=20880
dubbo.monitor.protocol=registry
3 启动文件配置
bash
package com.dubbolearn.consumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
// 注意现在的写法需要改成这样
@EnableDubbo
public class DubboconsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboconsumerApplication.class, args);
}
}
4 Current Spring Boot Application is await...的问题
启动dubboprovider 、 dubboconsumer 时,如果没有引入spring-boot-starter-web就会遇到该问题
5 Whitelabel Error Page...的问题
通过浏览器访问 localhost:7070/hello
如果遇到
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jul 26 20:59:17 CST 2021
There was an unexpected error (type=Internal Server Error, status=500).
spring启动是成功的,但dubbo调用comsumer找不到provider
会在dubbocomsuer报错
检查 dubboprovider 的StudentServiceImpl的
@Service(interfaceClass = StudentService.class, version="1.0.0",timeout=15000)
和 dubboprovider的studentsController的
@Reference(interfaceClass = StudentService.class,version="1.0.0",check=false)
private StudentService studentService;
是否匹配一致
例如有没有这种错 https://www.cnblogs.com/wangchao928/p/15063380.html