1 简介

dubbo是一个分布式服务框架,由阿里巴巴的工程师开发,致力于提供高性能和透明化的RPC远程服务调用。可惜的是该项目在2012年之后就没有再更新了,之后由当当基于dubbo开发了dubbox。这里对dubbo的入门构建进行简单的介绍。不涉及dubbo的运行机制,只是搭建过程,方便学习者快速构建项目,运行、熟悉该框架。

dubbo提供了两种构建项目的方法。1.通过Spring容器快速构建,其中还有注解的方式;2.通过直接使用API(不推荐)。以下对其一一说明。

2 前期工作

创建一个普通的maven项目,导入dubbo的依赖:

com.alibaba

dubbo

2.5.3

com.101tec

zkclient

0.4

下载zookeeper作为注册中心,具体步骤参考这里。

3 Spring配置方式

3.1 生产者Provider

dubbo的生产者是用于提供服务的,先定义服务接口和服务的实现类:

public interface DemoService {

public String greet(String name);

public List getUsers();

}

这里有两个服务一个是输入一个字符串,一个是返回一个一个List对象,User类的内容如下:

public class User implements Serializable{

private static final long serialVersionUID = 1L;

private String name;

private int age;

private String sex;

public User(String name, int age, String sex) {

this.name = name;

this.age = age;

this.sex = sex;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

@Override

public String toString() {

return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";

}

}

由于接口中会传递一个List的User对象,所以User需要实现Serializable接口。下面是DemoServiceImpl,接口的实现类中的内容:

public class DemoServiceImpl implements DemoService{

@Override

public String greet(String name) {

return "Hello " + name;

}

@Override

public List getUsers() {

List list = new ArrayList();

User user1 = new User("张三",10,"男");

User user2 = new User("李四",11,"女");

User user3 = new User("王五",12,"男");

list.add(user1);

list.add(user2);

list.add(user3);

return list;

}

}

这就是一个简单的生产者提供的服务了,和普通的服务类没有什么区别,关键是下面的dubbo配置了。与Spring结合,需要一个dubbo的配置xml文件,我命名为provider.xml,里面的内容如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

上面的XML配置文件就将服务暴露出去了,将其注册到了zookeeper中。最后运行Spring,测试:

public class Provider {

public static void main(String[] args) throws IOException {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});

context.start();

System.in.read();

}

}

控制台输出日志如下,就可以了:

3.2 消费者

消费者的配置就更加简单了,其只需要想要调用的服务的接口,在这里就是DemoService接口,注意要确保是同一个接口。然后配置消费者的consumer.xml,配置如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

reference就代表着引用一个服务,从暴露服务注册的注册中心获取,在spring中就有一个这样的接口实例了。测试类:

public class Consumer {

public static void main(String[] args) {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});

context.start();

DemoService demoService = (DemoService) context.getBean("demoService");

System.out.println(demoService.greet("张三"));

System.out.println(demoService.getUsers());

}

}

开启刚刚的生产者测试类,再运行这个消费者测试类,就会看到打印出:

3.3 注解方式

注解的方式配置起来非常的简单,全部如下:

生产者就是实现类上打上@Service注解就可以了,注意这个注解是com.alibaba.dubbo.config.annotation.Service,不是Spring的Service注解。

@Service

public class AnnotationProvider implements DemoService{

@Override

public String greet(String name) {

return "Hello " + name;

}

@Override

public List getUsers() {

List list = new ArrayList();

User user1 = new User("张三",10,"男");

User user2 = new User("李四",11,"女");

User user3 = new User("王五",12,"男");

list.add(user1);

list.add(user2);

list.add(user3);

return list;

}

}

配置文件也就是去掉了bean和,使用取代了:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

消费者也相差无几:

@Service

public class AnnotationComsumer {

@Reference(check=false)

private DemoService demoService;

public void print() {

System.out.println(demoService.greet("张三"));

System.out.println(demoService.getUsers());

}

}

注意这个Service是Spring的注解。配置文件如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

启动程序如下:

public static void main(String[] args) throws IOException {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"annotation-provider.xml"});

context.start();

System.in.read();

}

public static void main(String[] args) {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"annotation-consumer.xml"});

context.start();

AnnotationComsumer demoService = (AnnotationComsumer) context.getBean("annotationComsumer");

demoService.print();

}

最终效果和之前的一样。

4. API配置方式

这个简单说明一下,其实看API的方式和配置文件的方式就会明白一些,不做详细介绍。

public class APIProvider {

public static boolean running = true;

public static void main(String[] args) {

DemoService demoService = new DemoServiceImpl();

ApplicationConfig applicationConfig = new ApplicationConfig();

applicationConfig.setName("provider");

RegistryConfig registryConfig = new RegistryConfig();

registryConfig.setAddress("zookeeper://127.0.0.1:2181");

ProtocolConfig protocolConfig = new ProtocolConfig();

protocolConfig.setName("dubbo");

protocolConfig.setPort(20886);

protocolConfig.setPayload(16*1024*1024);

ServiceConfig service = new ServiceConfig();

service.setApplication(applicationConfig);

service.setRegistry(registryConfig);

service.setProtocol(protocolConfig);

service.setInterface(DemoService.class);

service.setRef(demoService);

service.export();

Runtime.getRuntime().addShutdownHook(new Thread() {

public void run() {

synchronized (APIProvider.class) {

running = false;

APIProvider.class.notify();

}

}

});

synchronized(APIProvider.class) {

while(running) {

try {

APIProvider.class.wait();

} catch (Throwable e) {

}

}

}

}

}

public class APIConsumer {

public static void main(String[] args) {

ApplicationConfig applicationConfig = new ApplicationConfig();

applicationConfig.setName("consumer");

RegistryConfig registryConfig = new RegistryConfig();

registryConfig.setAddress("zookeeper://127.0.0.1:2181");

ReferenceConfig reference = new ReferenceConfig();

reference.setApplication(applicationConfig);

reference.setRegistry(registryConfig);

reference.setInterface(DemoService.class);

DemoService demoService = reference.get();

System.out.println(demoService.greet("李四"));

System.out.println(demoService.getUsers());

}

}

都是要先定义ApplicationConfig和一致,后面RegistryConfig也一样。具体过程之后篇章介绍。这种API的方法不被推荐使用。

5 后记

本篇主要是帮助新手快速入门搭建一个dubbo服务,之后会从整体结构上介绍一下dubbo是如何工作的,再往后会讲解一下源码实现。水平有限,有错请指教。

说一下dubbo项目简单的搭建过程_dubbo学习(1)--简单的入门搭建实例相关推荐

  1. linux词语大全,简单词语大全二字学习软件-简单词语大全四字下载v1.5.3-Linux公社...

    简单词语大全二字学习软件是一款可以让用户快速背单词的软件,这款软件为用户提供了英语内容搭配影音的例句,让用户可以轻松学习英语.其中,用户可以在简单词语大全二字学习软件上对多人进行挑战,看自己的英语水平 ...

  2. 【Vue全家桶+SSR+Koa2全栈开发】项目搭建过程 整合 学习目录(持续更新中)

    写在开头 大家好,这里是lionLoveVue,基础知识决定了编程思维,学如逆水行舟,不进则退.金三银四,为了面试也还在慢慢积累知识,Github上面可以直接查看所有前端知识点梳理,github传送门 ...

  3. 实战项目(一)嵌入式基础学习与上位机入门设计

    Hello,大家好,首先自我介绍一下,我是大家的新朋友,也是这个教程的主要创建人,大家可以称呼我David,我个人从大学一直到研究生,甚至到工作一直在学习应用嵌入式,从最初的51单片机,到后期深度学习 ...

  4. ESP32学习笔记(51)——搭建环境、编译烧写(Windows+Espressif-IDE)

    VS Code 环境搭建参看 ESP32学习笔记(1)--搭建环境.编译烧写(Windows+VS Code) 一.搭建环境 1.1 官方资料 ESP-IDF 编程指南 1.2 下载Espressif ...

  5. ESP32学习笔记(1)——搭建环境、编译烧写(Windows+VS Code)

    Espressif-IDE 环境搭建参看 ESP32学习笔记(50)--搭建环境.编译烧写(Windows+Espressif-IDE) 一.搭建环境 1.1 官方资料 ESP-IDF 编程指南 1. ...

  6. ESP8266学习笔记(1)——搭建环境、编译烧写(NONOS SDK)

    RTOS SDK环境搭建参看 ESP8266学习笔记(17)--搭建环境.编译烧写(RTOS SDK) 一.搭建环境 1.1 ESP8266 SDK 入门指南 官网下载:https://www.esp ...

  7. Maven多模块,Dubbo分布式服务框架,SpringMVC,前后端分离项目,基础搭建,搭建过程出现的问题...

    现互联网公司后端架构常用到Spring+SpringMVC+MyBatis,通过Maven来构建.通过学习,我已经掌握了基本的搭建过程,写下基础文章为而后的深入学习奠定基础. 首先说一下这篇文章的主要 ...

  8. Maven多模块,Dubbo分布式服务框架,SpringMVC,前后端分离项目,基础搭建,搭建过程出...

    一.Maven多模块项目的创建 我们需要建立一个多模块的maven项目,其目录结构为 其中student-api用于暴露接口:student-service用语处理业务逻辑及调用数据访问对象,返回相应 ...

  9. minigpt4搭建过程记录,简单体验图文识别乐趣

    引言 从3月开始,aigc进入了疯狂的开端,正如4月12日无界 AI直播 在<探索 AIGC 与人类合作的无限可能>中关于梳理的时间线一样,aigc的各种产品如雨后春笋般进入了不可逆的态势 ...

最新文章

  1. oracle job的迁移
  2. 带有Hibernate OGM的NoSQL –第三部分:在WildFly上构建REST应用程序
  3. poj2186Popular Cows(Kosaraju算法--有向图的强连通分量的分解)
  4. 实现自己的promise
  5. 【16年浙江省赛 B ZOJ 3937】More Health Points【树上dfs、斜率优化dp、动态维护下凸壳】
  6. win7系统升级ie11,在KB2729094更新失败时的解决方法
  7. 18个免费视频素材网站,超高清、不限速、无版权、可商用,1秒解决你90%的视频剪辑难题!
  8. IPguard文档控制策略
  9. Hyrax: Doubly-efficient zkSNARKs without trusted setup学习笔记
  10. 手机锁屏时显示无服务器,手机总显示“请勿遮挡屏幕顶端”?别慌,它其实是一个贴心的功能...
  11. 南大软院大神养成计划--CSS网页布局
  12. 局部加权回归(LWR) Matlab模板
  13. 在Win32下使用OpenGL
  14. sFlow - 简介
  15. 洛谷 :P1104 生日
  16. 验证码----svg-captcha
  17. android h5调用百度地图,h5页面如何调用百度地图获取当前位置(代码)
  18. java支持arm64吗_VS2017预览版现已支持开发ARM64的UWP
  19. 修改unity代码编辑器
  20. Eclipse入门教程

热门文章

  1. 第二章 编程初步 Ivor Horton
  2. 使用WinWedge软件记录satorius天平的数据(记录)
  3. 苹果商店上架流程_苹果应用商店APP上架流程介绍!(ASO推广优化)
  4. Web实现:完整版垃圾分类网站 html+css 内含效果图
  5. 重装Win10系统其他盘的东西还在吗?
  6. 工作流任务调度系统:Apache DolphinScheduler
  7. Syntactic sugar Syntactic salt
  8. 2022 数维杯 A 题银行效率评价与破产成因分析
  9. excel怎么设置自动计算_Excel财务表格大全!公式已设置好,数据自动计算产生...
  10. html textarea 缩放,textarea自动撑开缩放