1. 构建SpringBoot环境
1.1 创建一个duubo-spring-boot-demo项目
- duubo-spring-boot-demo:是父工程,方便依赖管理
- duubo-spring-boot-demo-consumer:是服务消费方,继承父工程
- duubo-spring-boot-demo-provider:是服务提供方,继承父工程
- dubbo-interface:存放接口,因为dubbo是根据接口进行服务注册发现的,因此我们要把接口单独拿出来,provider提供方实现该接口并注册服务, consumer要根据接口进行注入
1.2 引入依赖坐标
1.2.1 dubbo-parent
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<dubbo.version>3.2.0-beta.4</dubbo.version>
<spring-boot.version>2.7.8</spring-boot.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
1.2.2 dubbo-consumer
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<parent>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-reload4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
1.2.3 dubbo-provider
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<parent>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-reload4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- spring boot starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
1.2.4 dubbo-interface
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
2. Linux 环境配置
2.1 安装docker
参考https://blog.csdn.net/qq_41428418/article/details/133786336
2.2 安装zookeeper
参考https://blog.csdn.net/qq_41428418/article/details/133857185
3. yml配置文件
3.1 provider提供者yml配置
server:
port: 9000
dubbo:
application:
name: dubbo-provider
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://${zookeeper.address:192.168.56.10}:2181
要在启动类加上@EnableDubbo注解
3.2 consumer提供者yml配置
server:
port: 8000
dubbo:
application:
name: dubbo-consumer
qos-port: 33333
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://${zookeeper.address:192.168.56.10}:2181
消费者如果不提供服务,也就是不把接口注册到注册中心时,可以不添加@EnableDubbo注解
4. 服务测试
4.1 新增接口
package com.itheima.service;
public interface UserService {
public String sayHello();
}
4.2 接口实现
在dubbo-provider中实现dubbo-interface中创建的UserService
package com.itheima.service.impl;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
//@Service定义bean
//将这个类提供的方法〈服务)对外发布。将访问的地址ip,端口,路径注册到注册中心中
@DubboService
public class UserServiceImpl implements UserService {
@Override
public String sayHello() {
return "hello dubbo!~welcome!";
}
}
4.3 服务引用
在dubbo-consumer中创建一个controller,并引用UserService。
package com.itheima.controller;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
//1.从zookeeper注册中心获取userService的访问urL
//2.进行远程调用RPC
//3. 将结果封装为一个代理对象,给变量赋值
@DubboReference
private UserService userService;
@GetMapping("/sayHello")
public String sayHello() {
return userService.sayHello();
}
}
4.4 服务测试
4.3.1 我们先在服务器查看一下zookeeper中的信息
1、执行docker ps
查看zookeeper容器状态,复制zookeeper对应的 CONTAINER ID
2、执行docker exec -it ${CONTAINER ID} /bin/bash
3、进入后ls查看目录,执行cd bin
进入bin目录
4、执行./zkCli.sh
脚本,连接zookeeper客户端
5、执行ls / 查看目录 我们可以看到,现在只有一个zookeeper
4.3.2 启动dubbo-provider和dubbo-consumer
4.3.3 启动后查看zookeeper内容
1、服务启动后,再执行ls /查看内容可以看到多了一个dubbo
2、执行ls /dubbo 查看,可以看到我们注册的接口的路径
3、继续查看,可以看还存在consumer和provider,查看consumer和provider,可以看到具体的注册中心,到这里说明我们已经注册完成了
4.3.4 浏览器访问测试
官方中文文档