Dubbo是什么?Dubbo干什么?Dubbo怎么用? |
您所在的位置:网站首页 › 暗堡是干什么的 › Dubbo是什么?Dubbo干什么?Dubbo怎么用? |
Dubbo是什么?
Dubbo官网地址:http://dubbo.apache.org Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC框架,可以和Spring框架无缝集成。 什么是RPC? RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不在一个内存空间,不能直接调用,所以需要通过网络来表达调用的语义和传达调用的数据。 需要注意的是RPC并不是一个具体的技术,而是指整个网络远程调用过程。 RPC是一个泛化的概念,严格来说一切远程过程调用手段都属于RPC范畴。各种开发语言都有自己的RPC框架。Java中的RPC框架比较多,广泛使用的有RMI、Hessian、Dubbo等。 Dubbo干什么?Dubbo提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。 Dubbo架构图(Dubbo官方提供)如下: 节点角色说明: 节点角色名称Provider 暴露服务的服务提供方Consumer 调用远程服务的服务消费方Registry服务注册与发现的注册中心Monitor统计服务的调用次数和调用时间的监控中心Container服务运行容器调用关系说明: 0. 服务容器负责启动,加载,运行服务提供者。 1. 服务提供者在启动时,向注册中心注册自己提供的服务。 2. 服务消费者在启动时,向注册中心订阅自己所需的服务。 3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。 4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。 5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。 Registry服务注册中心Zookeeper 通过前面的Dubbo架构图可以看到,Registry(服务注册中心)在其中起着至关重要的作用。Dubbo官方推荐使用Zookeeper作为服务注册中心。 Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 。 Zookeeper树型目录服务: 流程说明: 服务提供者(Provider)启动时: 向 `/dubbo/com.foo.BarService/providers` 目录下写入自己的 URL 地址 服务消费者(Consumer)启动时: 订阅 `/dubbo/com.foo.BarService/providers` 目录下的提供者 URL 地址。并向 `/dubbo/com.foo.BarService/consumers` 目录下写入自己的 URL 地址 监控中心(Monitor)启动时: 订阅 `/dubbo/com.foo.BarService` 目录下的所有提供者和消费者 URL 地址 这里不在详细说明,Zookeeper的安装启动流程。 Dubbo怎么用?Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。一个作为服务的提供方,一个作为服务的消费方。通过Dubbo来实现服务消费方远程调用服务提供方的方法。 服务提供方开发 1.创建maven项目pom.xml文件中导入如下坐标 UTF-8 1.8 1.8 5.0.5.RELEASE org.springframework spring-context ${spring.version} org.springframework spring-beans ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-jdbc ${spring.version} org.springframework spring-aspects ${spring.version} org.springframework spring-jms ${spring.version} org.springframework spring-context-support ${spring.version} com.alibaba dubbo 2.6.0 org.apache.zookeeper zookeeper 3.4.7 com.github.sgroschupf zkclient 0.1 javassist javassist 3.12.1.GA com.alibaba fastjson 1.2.47 org.apache.maven.plugins maven-compiler-plugin 2.3.2 1.8 1.8 org.apache.tomcat.maven tomcat7-maven-plugin 8081 /2.配置web.xml文件 Archetype Created Web Application contextConfigLocation classpath:applicationContext*.xml org.springframework.web.context.ContextLoaderListener3.创建服务接口 public interface HelloService { public String sayHello(String name); }4.创建服务实现类 @Service public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "hello " + name; } }5.在src/main/resources下创建applicationContext-service.xml 注意:这里配置需要写zookeeper所在的IP地址 7.启动服务 服务消费方开发 1.创建maven工程,pom.xml配置和上面服务提供者相同,只需要将Tomcat插件的端口号改为8082即可 2. 配置web.xml文件 Archetype Created Web Application springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext-web.xml 1 springmvc *.do3.将服务提供者工程中的HelloService接口复制到当前工程 4.编写Controller 注意:Controller中注入HelloService使用的是Dubbo提供的@Reference注解 @Controller @RequestMapping("/demo") public class HelloController { @Reference private HelloService helloService; @RequestMapping("/hello") @ResponseBody public String getName(String name){ //远程调用 String result = helloService.sayHello(name); System.out.println(result); return result; } }5.在src/main/resources下创建applicationContext-web.xml 6.运行测试 在浏览器输入http://localhost:8082/demo/hello.do?name=Jack,查看浏览器输出结果
|
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |