Apache dubbo 分布式框架(一)

Apache Dubbo

Dubbo简介

Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开
源Java RPC框架,可以和Spring框架无缝集成。

RPC

RPC全称为remoteprocedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应
用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不
在一个内存空间,不能直接调用,所以需要通过网络来表达调用的语义和传达调用的数据。

需要注意的是RPC并不是一个具体的技术,而是指整个网络远程调用过程。

Dubbo就是Java的RPC框架之一,Dubbo提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现

Dubbo架构

节点角色

节点 角色名称
Provider 暴露服务的服务提供方
Consumer 调用远程服务的服务消费方
Registry 服务注册与发现的注册中心
Monitor 统计服务的调用次数和调用时间的监控中心
Container 服务运行容器

调用关系说明

  • 服务容器负责启动,加载,运行服务提供者
  • 服务提供者在启动时,向注册中心注册自己所提供的服务
  • 服务消费者在启动时,向注册中心订阅自己所需的服务
  • 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者
  • 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选一台调用
  • 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心

服务注册中心Zookeeper

Zookeeper简介

Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo
服务的注册中心,工业强度较高,可用于生产环境,并推荐使用

Dubbo快速入门

Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。本小节就是要创建两个应
用,一个作为服务的提供者,一个作为服务的消费者。通过Dubbo来实现服务消费者远程调用服务提供
者的方法。

开发服务提供者

  1. 创建maven工程 dubbodemo_provider

  2. 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.6
    
    
        io.netty
        netty-all
        4.1.32.Final
    
    
        org.apache.curator
        curator-framework
        4.0.0
        
            
                org.apache.zookeeper
                zookeeper
            
        
    
    
        org.apache.zookeeper
        zookeeper
        3.4.7
    
    
        com.github.sgroschupf
        zkclient
        0.1
    

  1. web.xml


    contextConfigLocation
    classpath*:applicationContext-*.xml


    org.springframework.web.context.ContextLoaderListener

  1. applicationContext-web.xml




    
    
        
        
    

    
    

    
    

    
    


  1. 编写service接口
public interface HelloService {
    public String sayHello(String name);
}

实现类

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {

        return "hello" + name;
    }
}

开发服务消费者

  1. pom 依赖

    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.6
      
        
          org.springframework
          spring-web
        
        
          org.springframework
          spring-beans
        
        
          org.springframework
          spring-context
        
      
    
    
      io.netty
      netty-all
      4.1.32.Final
    
    
      org.apache.curator
      curator-framework
      4.0.0
      
        
          org.apache.zookeeper
          zookeeper
        
      
    
    
      org.apache.zookeeper
      zookeeper
      3.4.7
    
    
      com.github.sgroschupf
      zkclient
      0.1
    
  
  1. web.xml

        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:applicationContext-web.xml
        
        1
    

    
        springmvc
        *.do
    
  1. applicationContext-web.xml


    
    

    
    
        
        
    

    
    

    
    

    
    

  1. HelloController
@Controller
public class HelloController {

    //注入接口
    @Reference
    private HelloService helloService;

    @RequestMapping(value = "/hello")
    @ResponseBody
    public String hello() {
        return helloService.sayHello("test");
    }
}
  • @Reference 一定要使用dubbo的接口
  1. 创建同服务提供者相同的接口

com.ithiema.service.HelloService

public interface HelloService {
    public String sayHello(String name);
}

小结:

  • Service实现类必须使用dubbo的@Service注解
  • Controller中调用Service方法时,必须使用dubbo的@Reference进行注入

抽取公共接口

将接口提取到公共的接口模块dubbodemo_interface中,

服务提供者和服务消费者都需要依赖接口模块

你可能感兴趣的:(Apache dubbo 分布式框架(一))