首先安装服务端的zookeeper 版本最好和spring dubbo一致 2.7.3对应的zk版本是3.4.13 (安装过程略)
使用idea 构建多模块项目 引入需要的依赖
parent为项目的依赖中心 所有模块都是parent的子模块
common中放通用工具类等
interface中放业务层接口
service放业务实现类
web放controller、拦截器、过滤器、权限框架等
model放pojo等
common被除parent以外所有项目依赖
interface被service和web依赖
model被interface、service、web依赖
dubbo及zk所需的关键依赖
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<boot.version>2.2.0.RELEASE</boot.version>
<dubbo.version>2.7.3</dubbo.version>
<jackson.version>2.10.0</jackson.version>
<mp.version>3.2.0</mp.version>
<jdbc.version>8.0.17</jdbc.version>
<servlet.version>3.1.0</servlet.version>
<lombok.version>1.18.8</lombok.version>
<commons-collections.version>3.2.2</commons-collections.version>
<validation.version>2.0.1.Final</validation.version>
<jwt.version>3.8.2</jwt.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>${mp.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mp.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-annotation</artifactId>
<version>${mp.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${jdbc.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons-collections.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
service中application.yml关键配置 数据源等按需配置启动
dubbo.registry.address的内容为你服务端ip地址和zkserver端口
dubbo.scan.base-packages里放的是你service实现类的包名
service实现类注解 注意这个service和spring的service不是同一个
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
web配置同理
controller
依赖不再是构造注入或autowied
而是@Reference
因为接口在interface模块 而service和web都依赖了interface模块
所以service和web模块都能找得到这个接口
最后调用即可
显示灰色应该是idea还未支持这种方式注入
最后service和web的启动类上都要加
这个注解开启dubbo