当前位置: 首页 > news >正文

Dubbo快速入门(二):第一个Dubbo程序(附源码)

文章目录

  • 一、生产者工程
    • 0.目录结构
    • 1.依赖
    • 2.配置文件
    • 3.启动类
    • 4.生产者服务
  • 二、消费者工程
    • 0.目录结构
    • 1.依赖
    • 2.配置文件
    • 3.启动类
    • 4.服务接口
    • 5.controller接口
  • 三、测试代码

本博客配套源码:gitlab仓库

首先,在服务器上部署zookeeper并运行,可以参考我的另一篇教程:https://blog.csdn.net/Tracycoder/article/details/142792750

注意事项:

  • 生产者、消费者中服务的包路径一定要一致,不然会导致注册和消费失败!
  • 先启动生产者,再启动消费者!不然会启动失败!
  • 各依赖的版本一定要兼容,不然项目会启动失败!

一定要注意以上几点,踩了几个小时的坑,说多了都是泪!

一、生产者工程

0.目录结构

在这里插入图片描述

1.依赖

pom.xml

<?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>org.example</groupId><artifactId>dubbo_study_provider</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><!-- Spring Boot Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!-- Dubbo Starter --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>3.2.0</version></dependency><!-- Zookeeper Client --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>5.4.0</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>5.4.0</version></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.29.2-GA</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.curator/curator-x-discovery-server --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-x-discovery-server</artifactId><version>5.4.0</version></dependency><!-- Spring Boot Test Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>

2.配置文件

applicaiton.yml

server:port: 8080# Dubbo
dubbo:application:name: dubbo_providerregistry:address: zookeeper://你的zookeeperIP:2181protocol:name: dubboport: 20880

3.启动类

package tracy;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableDubbo(scanBasePackages = "tracy.provider.service")
public class ProviderApplication {public static void main(String[] args) {System.setProperty("zookeeper.sasl.client", "false");SpringApplication.run(ProviderApplication.class, args);System.out.println("provider服务启动成功!!!");}
}

4.生产者服务

package tracy.provider.service;public interface HelloService {String sayHello(String name);
}
package tracy.provider.service;import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;@DubboService(version = "1.0.0")
@Component
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {return "hello "+name+"!";}
}

二、消费者工程

0.目录结构

在这里插入图片描述

1.依赖

pom.xml

<?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>org.example</groupId><artifactId>dubbo_study_consumer</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><!-- Spring Boot Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Dubbo Starter --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>3.2.0</version></dependency><!-- Zookeeper Client --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>5.4.0</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>5.4.0</version></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.29.2-GA</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.curator/curator-x-discovery-server --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-x-discovery-server</artifactId><version>5.4.0</version></dependency><!-- Spring Boot Test Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>

2.配置文件

application.yml

server:port: 8081# Dubbo
dubbo:application:name: dubbo_consumerregistry:address: zookeeper://你的zookeeperIP:2181consumer:check: false  # 设置为 false,避免消费者启动时检查提供者状态

3.启动类

package tracy;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) {System.setProperty("zookeeper.sasl.client", "false");SpringApplication.run(ConsumerApplication.class, args);System.out.println("consumer服务启动成功!!!");}
}

4.服务接口

package tracy.provider.service;public interface HelloService {String sayHello(String name);
}

5.controller接口

package tracy.controller;import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tracy.provider.service.HelloService;@RestController
@RequestMapping("/hello")
public class HelloController {@DubboReference  // 使用 DubboReference 注解引用远程服务private HelloService helloService;@GetMapping("/sayHello")public String sayHello(@RequestParam String name) {return helloService.sayHello(name);}
}

三、测试代码

先启动zookeeper,然后启动provider,最后启动consumer。

然后在接口测试工具中访问接口:

在这里插入图片描述

访问成功!

http://www.lryc.cn/news/461473.html

相关文章:

  • 不同数据类型转换与转义的对比差异
  • Kylin系统安装VMwareTools工具
  • uni-app 拍照图片添加水印
  • Docker-registry私有镜像仓库的安装
  • 在vue3中实现祖组件给后代组件传参,可以跨域几层。
  • 【优选算法】——双指针(上篇)!
  • 【C语言】数据输出格式控制
  • Qt-界面优化选择器的用法(70)
  • 全国职业技能大赛——信息安全管理与评估第一阶段BC、FW、WAF题目详细解析过程
  • 基于Vite创建项目
  • 面试题:在 React 中如何绑定事件
  • 前端将JSON或者table直接导出为excel
  • 算法之排序
  • 深度学习:LSTM循环神经网络实现评论情感分析
  • 基于Arduino的环境监测装置
  • 深度学习:模型攻击(Model Attack)详解
  • CesiumLab介绍
  • PyQt 入门教程(3)基础知识 | 3.2、加载资源文件
  • 老照片修复工作流教程:用 ComfyUI 轻松还原历史记忆
  • ESP-IDF Blink实例学习
  • QT QML 练习8-Simple Transformations
  • 低空产业园搭建技术详解
  • Python网络爬虫从入门到实战
  • 探索Theine:Python中的AI缓存新贵
  • js拼图(神鹰黑手哥)
  • 值得推荐的五款数据恢复工具!!
  • 股票金融市场中的tick,分钟,日线数据
  • OKG Research:如何衡量链上数据的开放价值?
  • 向日葵下载教程以及三款远程控制工具推荐!!!
  • Studio One 6中文版及最新功能介绍 Studio One 6音乐软件安装包