写在前面的话:技术路上的坑只能一个一个的踩过去了吗?以为很简单,但还是浪费了很长时间。。。这是目前发现的配置最简单的一种搭建方式。
项目GitHub地址
https://github.com/libaolei007/springboot-dubbo-zookeeper
搭建前需要安装Zookeeper
https://blog.csdn.net/weixin_39819880/article/details/98472507
api中一般存放entity实体类和service接口,consumer和provider模块都需要依赖这个模块。
consumer作为消费者的角色,可以存放controller控制类,当然,如果有其他的,比如拦截器,权限控制,也可以放这里。
provider作为消费者角色,可以存放service接口的实现类,如果项目中集成了mybatis,也可以放在这个模块下
在springboot-dubbo-zookeeper的pom.xml文件添加module依赖
<modules>
<module>apimodule>
<module>consumermodule>
<module>providermodule>
modules>
添加
jar
package com.rpc.api;
public interface DemoService {
String sayHello(String name);
}
[外链图片转存失败(img-8OhtlkxK-1565009124239)(./1565000077208.png)]
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.6.RELEASEversion>
<relativePath/>
parent>
<groupId>com.rpc.consumergroupId>
<artifactId>consumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>consumername>
<description>Demo project for Spring Bootdescription>
<packaging>jarpackaging>
<properties>
<java.version>1.8java.version>
<dubbo-spring-boot>1.0.0dubbo-spring-boot>
properties>
<dependencies>
<dependency>
<groupId>com.rpc.demogroupId>
<artifactId>apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>io.dubbo.springbootgroupId>
<artifactId>spring-boot-starter-dubboartifactId>
<version>${dubbo-spring-boot}version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12artifactId>
<groupId>org.slf4jgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
## 避免和provider工程端口冲突
server.port=7001
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
## Dubbo 服务对象的注册中心zookeeper的地址和端口
spring.dubbo.registry.address=zookeeper://192.168.19.22:2181
## 服务对象的被注入的包扫描范围
spring.dubbo.scan=com.rpc.consumer.controller
package com.rpc.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.rpc.api.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
@Reference(version = "1.0.0")
private DemoService demoService;
@RequestMapping("/getHello")
public String getHello() {
return demoService.sayHello("李保磊");
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.6.RELEASEversion>
<relativePath/>
parent>
<groupId>com.rpc.providergroupId>
<artifactId>providerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>providername>
<description>Demo project for Spring Bootdescription>
<packaging>jarpackaging>
<properties>
<java.version>1.8java.version>
<dubbo-spring-boot>1.0.0dubbo-spring-boot>
properties>
<dependencies>
<dependency>
<groupId>com.rpc.demogroupId>
<artifactId>apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>io.dubbo.springbootgroupId>
<artifactId>spring-boot-starter-dubboartifactId>
<version>${dubbo-spring-boot}version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12artifactId>
<groupId>org.slf4jgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.12version>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
server.port=8001
## Dubbo 服务提供者配置
spring.dubbo.application.name=provider
## Dubbo 服务对象的注册中心zookeeper的地址和端口
spring.dubbo.registry.address=zookeeper://192.168.19.22:2181
## 用Dubbo协议在20880端口暴露服务
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
## 包扫描范围
spring.dubbo.scan=com.rpc.provider.impl;
@Service
注解,是dubbo提供的注解
package com.rpc.provider.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.rpc.api.DemoService;
@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello" + name;
}
}
首先要启动zookeeper,它是注册中心,没有启动的话,provider的服务提供者怎么注册呢?
注意,由于consumer和provider都依赖api,那么要将api模块通过maven,依次通过clean,install命令,将api安装到consumer和provider模块中。
启动provider,将服务注册到zookeeper中心
@Reference注解引入的service实例为null
我哭了,你呢???
哭了几个小时之后,我好了!
我的原因是:
首先检查一下你的spring boot版本是多少?
如果是2.X 不用看了,spring boot 2.x 必定会出现这个问题,
改为 1.5.8 版本。
将consumer和provider里面的pom.xml文件里的parenet标签下的version标签内容改为1.5.8.RELEASE
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.8.RELEASEversion>
<relativePath/>
parent>