自学了一天的Dubbo,第一次接触RPC框架,印象深刻的例如Dubbo官网的RPC原理图(下文笔记有)、分层注册中心、提供者、消费者的思想以及提供者暴露接口的version字段等,学完受益良多,发此文方便日后复习,也希望可以帮助到需要快速熟悉上手Dubbo、Zookeeper的童鞋。

Dubbo

Dubbo简介

Dubbo是一个高性能轻量级的RPC开源框架,它提供了三大核心能力:面向接口的远程方法调用、智能容错和负载均衡以及服务自动注册和发现。 官网: Apache Dubbo ​ 

为什么说Dubbo性能高

高性能要从底层的原理说起,既然是一个RPC框架,主要干的就是远程方法调用,那么提升性能就从最关键最耗时的两个方面入手:序列化和网络通信。 序列化:我们学习Java网络开发的时候知道,本地的对象要想在网络上传输,必须实现Serializable接口实现序列化,序列化的方案有很多:xml、json、二进制流...其中效率最高的就是二进制流(因为计算机是二进制),Dubbo采用的就是效率最高的二进制流。 网络通信:不同于HTTP协议,需要进行三次握手四次挥手,Dubbo采用Socket通信机制,一步到位,提升了通信效率,并且可以建立长连接,不用反复连接,直接传输数据。

Dubbo前世今生

Dubbo之前一直都是阿里巴巴公司内部使用的框架。 2011年,Dubbo被托管到GitHub上(开源)。 2014年11月发布2.4.11版本后宣布停止更新,此后一段时间很多公司开源了自己基于Dubbo的变种版本(例如:当当网的DubboX,网易考拉的Dubbo K) 2017年SpringCloud横空出世,Dubbo感觉到压力后连续更新了几个版本 2018年1月,阿里巴巴联合当当网将Dubbo和DubboX合并,发布了2.6版本 2018年除夕夜,案例将Dubbo贡献给了Apache基金会 2018年除夕夜至今,由Apache维护和更新Dubbo

Dubbo使用-直连

提供者

创建一个maven web工程:服务的提供者 创建一个实体bean查询的结果

public class User {

   private Integer id;

   private String username;

   private Integer age;

   public Integer getId() {

       return id;

  }

   public void setId(Integer id) {

       this.id = id;

  }

   public String getUsername() {

       return username;

  }

   public void setUsername(String username) {

       this.username = username;

  }

   public Integer getAge() {

       return age;

  }

   public void setAge(Integer age) {

       this.age = age;

  }

}

提供一个服务接口

public interface UserService {

   User queryUserById(Integer id);

}

实现这个服务接口

public class UserServiceImpl implements UserService {

   @Override

   public User queryUserById(Integer id) {

       User user = new User();

       user.setId(1001);

       user.setUsername("张三");

       user.setAge(13);

       return user;

  }

}

配置dubbo服务提供者的核心配置文件

声明dubbo服务提供者的名称:保证唯一

   

声明dubbo使用的协议和端口号

   

   

暴露服务,使用直连方式

   

   

   

添加监听器

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

        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

        version="4.0">

   

   

       contextConfigLocation

       classpath:dubbo-userService-provider.xml

   

   

       org.springframework.web.context.ContextLoaderListener

   

消费者

创建一个Maven web工程:服务的消费者 创建pom文件:添加需要的依赖(Spring Dubbo)

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 4.0.0

 org.example

 002-link-consumer

 1.0-SNAPSHOT

  war

 

   

     org.springframework

     spring-context

     4.3.16.RELEASE

   

   

     org.springframework

     spring-webmvc

     4.3.16.RELEASE

   

   

     com.alibaba

     dubbo

     2.6.2

   

   

     org.example

     001-link-userservice-provider

     1.0-SNAPSHOT

   

 

设置dubbo的核心配置文件

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

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

   

   

   

   

           id="userService"

           interface="com.bjpowernode.dubbo.service.UserService"

           url="dubbo://localhost:20880"

           registry="N/A">

   

编写spring核心配置文件

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

      xmlns:context="http://www.springframework.org/schema/context"

      xmlns:mvc="http://www.springframework.org/schema/mvc"

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   

   

   

   

   

   

       

       

   

编写controller

@Controller

public class UserController {

   @Autowired

   private UserService userService;

   @RequestMapping("/user")

   public String userDetail(Model model,Integer id){

       User user = userService.queryUserById(id);

       model.addAttribute("user",user);

       return "userDetail";

  }

}

配置中央调度器(就是一个Servlet:DispatcherServlet)

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

        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

        version="4.0">

   

       dispatcherServlet

       org.springframework.web.servlet.DispatcherServlet

       

           contextConfigLocation

           classpath:application.xml,classpath:dubbo-consumer.xml

       

   

   

       dispatcherServlet

       /

   

dubbo官方推荐必须有一个接口工程,他就是一个maven java工程

要求接口工程里存放到内容如下:

对外暴露的服务接口(Service接口) 实体bean对象

Dubbo使用注册中心-Zookeeper

前期准备:

网站:Apache ZooKeeper 下载的压缩包解压缩后会发现 有四个重要文件夹 ​ bin中有启动命令之类的。 conf存放资源 docs存放文档 lib存放所需要的依赖 打开conf复制一份zoo_sample.cfg,改名为zoo.cfg更改其中的配置:

加入:admin.serverPort=8888。默认是8080,修改目的是防止8080和Tomcat端口冲突 也可以在根目录创建文件夹data存放数据,将dataDir改为路径。 bin中的zkServer.cmd是Windows系统启动命令 bin中的zkServer.sh是Linux系统启动命令

使用Zookeeper当作注册中心

实体类及服务接口

创建一个maven Java工程 添加实体类并实现set、get方法、Serializable序列化接口

public class User implements Serializable {

   private Integer id;

   private String username;

   public Integer getId() {

       return id;

  }

   public void setId(Integer id) {

       this.id = id;

  }

   public String getUsername() {

       return username;

  }

   public void setUsername(String username) {

       this.username = username;

  }

}

创建服务接口

public interface UserService {

   User queryUserById(Integer id,String username);

}

提供者

创建Maven Web项目 pom.xml中添加spring的依赖、dubbo依赖、zookeeper的依赖以及实体类及服务接口的GAV

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 4.0.0

 org.example

 009-zk-userservice-multi-provider

 1.0-SNAPSHOT

 war

 

   

     org.springframework

     spring-context

     4.3.16.RELEASE

   

   

     org.springframework

     spring-webmvc

     4.3.16.RELEASE

   

   

     com.alibaba

     dubbo

     2.6.2

   

   

     org.example

     006-zk-interface

     1.0-SNAPSHOT

   

   

     org.apache.curator

     curator-framework

     4.1.0

   

 

 

   

     

     

       maven-compiler-plugin

       

         1.8

         1.8

       

     

   

 

实现UserService接口:我这里创建了两个实现类,目的是用下文的version版本号进行区分。

package dubbo.service.impl;

import dubbo.model.User;

import dubbo.service.UserService;

public class UserServiceImpl implements UserService{

   @Override

   public User queryUserById(Integer id, String username) {

       User user = new User();

       user.setId(id);

       user.setUsername(username+"-1");

       return user;

  }

}

package dubbo.service.impl;

import dubbo.model.User;

import dubbo.service.UserService;

public class UserServiceImpl2 implements UserService {

   @Override

   public User queryUserById(Integer id, String username) {

       User user = new User();

       user.setId(id);

       user.setUsername(username+"-2");

       return user;

  }

}

​ 创建dubbo配置文件

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

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

   

   

   

   

   

   

   

   

   

   

   

   

   

在web.xml中配置监听器

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

        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

        version="4.0">

   

       contextConfigLocation

       classpath:dubbo-zk-userservice-provider.xml

   

   

       org.springframework.web.context.ContextLoaderListener

   

消费者

创建Maven Web项目 pom.xml中添加spring的依赖、dubbo依赖、zookeeper的依赖以及实体类及服务接口的GAV(与提供者相同) 创建controller类

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller

public class UserController {

   @Resource

   private UserService userService;

   @Resource

   private UserService userService2;

   @RequestMapping(value = "/userDetail")

   public String userDetail(Model model,Integer id,String username){

       User user = userService.queryUserById(id, username);

       User user2 = userService2.queryUserById(id, username);

       model.addAttribute("user",user);

       model.addAttribute("user2",user2);

       return "userDetail";

  }

}

创建dubbo配置文件

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

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

   

   

   

   

   

   

创建Spring配置文件

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

      xmlns:context="http://www.springframework.org/schema/context"

      xmlns:mvc="http://www.springframework.org/schema/mvc"

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   

   

   

       

       

   

在web.xml文件里配置控制器

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

        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

        version="4.0">

   

       dispatcherServlet

       org.springframework.web.servlet.DispatcherServlet

       

           contextConfigLocation

           classpath:application.xml,classpath:dubbo-multi-consumer.xml

       

   

   

       dispatcherServlet

       /

   

创建userDetail.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

   Title

用户1

用户编号:${user.id}

用户姓名:${user.username}

用户2

用户编号:${user2.id}

用户姓名:${user2.username}

运行

首先启动zookeeper ​ 然后启动提供者的tomcat,这里我将提供者的端口号改为8081 然后启动消费者的tomcat,端口号不变8080 打开浏览器输入url 结果

查看原文