课程名称:Java架构师-技术专家
课程章节:阶段四 · 服务治理的另一条路 - Dubbo
课程讲师: 慕课讲师团队
课程内容
dubbo注册中心
dubbo对注册中心的选择是比较灵活的,可以有以下的五种注册中心可供选择。
- Multicast
- Zookeeper
- Nacos
- Redis
- Simple(本身就是一个 dubbo 服务)
构建服务提供方
下面实战要做的事:创建一个服务提供者、创建一个服务调用者,服务调用者通过网络远程调用服务提供者提供的方法,体验dubbo rpc。
- 启动 zookeeper 作为注册中心,这里我使用我在云端部署的 zookeeper,地址为
60.205.142.63:2181
- 创建一个 maven 项目,我这里命名为
dubboDemo
,修改打包方式为pom,作为父工程。<?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> <groupId>com.wkq</groupId> <artifactId>dubboDemo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>dubbo-api</module> <module>dubbo-provider</module> <module>dubbo-consumer</module> </modules> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.7.3</version> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> </dependencies>
- 创建 dubbo 服务公用的 api 和对象,我这里将其命名为 dubbo-api,添加两个类,com.wkq.springcloud.User、com.wkq.springcloud.IDubboService。
package com.wkq.springcloud; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @AllArgsConstructor @NoArgsConstructor public class User implements Serializable { private String name; private Integer age; }
package com.wkq.springcloud; public interface IDubboService { User publish(User user); }
- 做完上述准备工作之后,开始编写服务提供者,我这里将其命名为 dubbo-provider。dubbo-provider 作为 dubboDemo 的一个子模块。
步骤一: 在 pom.xml 中引入 dubbo 依赖
步骤二:编写启动入口类,并开启 dubbo 支持<?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"> <parent> <artifactId>dubboDemo</artifactId> <groupId>com.wkq</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-provider</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.wkq</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>3.0.7</version> </dependency> </dependencies> </project>
步骤三:编写 application.yaml 配置文件package com.wkq.springcloud; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
dubbo: application: name: dubbo-provider registry: address: zookeeper://60.205.142.63:2181 protocol: zookeeper check: false metadata-report: # 元数据中心 address: zookeeper://60.205.142.63:2181 protocol: name: dubbo port: 20881 monitor: # 监控中心 protocol: register server: port: 63001
- 至此,已经开启并配置了 dubbo,现在开始编写测试代码,创建 IDubbo 的服务实现者 com.wkq.springcloud.DubboServiceImpl
至此,服务提供者编写完毕,启动 com.wkq.springcloud.ProviderApplication 运行服务器提供者。package com.wkq.springcloud.service; import com.wkq.springcloud.IDubboService; import com.wkq.springcloud.User; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboService; @Slf4j @DubboService public class DubboServiceImpl implements IDubboService { @Override public User publish(User user) { log.info("publish User {}",user.getName()); user.setAge(22); return user; } }
构建服务调用方
服务调用方的创建就比较简单了,也分为三步,这里将服务消费者命名为 dubbo-consumer 。
- 创建 dubbo 工程 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"> <parent> <artifactId>dubboDemo</artifactId> <groupId>com.wkq</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-consumer</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.wkq</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>3.0.7</version> </dependency> </dependencies> </project>
- 编写启动入口类并开启对 dubbo 的支持。
package com.wkq.springcloud; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
- 在 application.yaml 中对 dubbo 进行配置
dubbo: application: name: dubbo-consumer registry: address: zookeeper://60.205.142.63:2181 protocol: zookeeper check: false metadata-report: # 元数据中心 address: zookeeper://60.205.142.63:2181 protocol: name: dubbo port: 20882 monitor: # 监控中心 protocol: register server: port: 63002
- 至此,dubbo服务调用方已经做好基本配置,开始编写测试代码,创建类 com.wkq.springcloud.Controller。
package com.wkq.springcloud.controller; import com.wkq.springcloud.IDubboService; import com.wkq.springcloud.User; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class Controller { @DubboReference(loadbalance = "roundrobin") private IDubboService dubboService; @GetMapping("/publish") public User publish(@RequestParam String name) { User user = new User(); user.setName(name); return dubboService.publish(user); } }
- 至此,服务调用方和服务提供方均已编写好,打开浏览器在地址栏输入
http://localhost:63002/publish?name=test
进行访问测试。可以得到结果如下:
打开服务提供者控制台,可以看到对应日志打印,上述现象说明 服务调用者 通过 rpc 的方式调用 服务提供者成功。{"name":"test","age":12}
课程收获
今天学习了以下内容:
- dubbo 的支持的注册中心众多,主流是 zookeeper 和 nacos
- 实战 dubbo rpc 远程方法调用。