dubbo 入門

1 介紹

1.1 背景

隨着互聯網的發展,網站應用的規模不斷擴大,常規的垂直應用架構已沒法應對,分佈式服務架構以及流動計算架構勢在必行,亟需一個治理系統確保架構有條不紊的演進。html

1.2 說明

DUBBO是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,是阿里巴巴SOA服務化治理方案的核心框架。前端

1.3 功能

啓動時檢查、集羣容錯、負載均衡、線程模型、直連提供者、只訂閱、只註冊、靜態服務、多協議、多註冊中心、服務分組、多版本、分組聚合、參數驗證、結果緩存、泛化引用、泛化實現、回聲測試、上下文信息、隱式傳參、異步調用、本地調用、參數回調、事件通知、本地存根、本地假裝、延遲暴露、併發控制、鏈接控制、延遲鏈接、粘滯鏈接、令牌驗證、路由規則、配置規則、服務降級、優雅停機、主機綁定、日誌適配、訪問日誌、服務容器、Reference Config緩存、分佈式事務java

1.4 支持的協議

  • dubbo
  • rmi
  • http
  • webservice
  • thrift
  • memcached
  • redis

2 環境準備

  • Java七、Java8
  • ZooKeeper
  • Maven
  • Tomcat7
  • Eclipse

3 架構圖

dubbo architecture

節點角色說明及對應demo模塊:git

  • Provider: 暴露服務的服務提供方。對應dubbo-demo-provider。
  • Consumer: 調用遠程服務的服務消費方。對應dubbo-demo-consumer
  • Registry: 服務註冊與發現的註冊中心。這個demo使用ZooKeeper,關於ZooKeeper的搭建可參考這篇文章
  • Monitor: 統計服務的調用次調和調用時間的監控中心。須要搭建dubbo-admin,對應的war包可從這裏下載,也可自行編譯運行。
  • Container: 服務運行容器。使用Tomcat跟Jetty。

個人demo代碼:https://github.com/JasperZXY/dubbo-demogithub


4 開始搭建項目

4.1 說明

下面的代碼是在Java8下運行經過的。web

4.2 建立maven項目

名稱爲dubbo-demo,加入須要的jar,這裏把各個項目須要的jar包都放入了父pom中,實際項目最好作好分類,以下redis

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>4.12</junit.version>
    <spring.version>4.3.8.RELEASE</spring.version>
    <dubbo.version>2.5.3</dubbo.version>
    <zookeeper.version>3.4.10</zookeeper.version>
    <zkclient.version>0.10</zkclient.version>
    <jackson.version>2.7.4</jackson.version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!--Spring MVC集成slf4j-logback -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.logback-extensions</groupId>
        <artifactId>logback-ext-spring</artifactId>
        <version>0.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.12</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
        <exclusions>
            <exclusion>
                <!-- 排除傳遞spring依賴,防止衝突 -->
                <artifactId>spring</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- 導入zookeeper依賴 -->
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>${zookeeper.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.netty</groupId>
        <artifactId>netty</artifactId>
        <version>3.2.10.Final</version>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>${zkclient.version}</version>
    </dependency>

    <!-- jackson -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>
</dependencies>

4.3 dubbo-demo-api

這個模塊用於定義接口以及交互的實體對象,建立一個User跟UserServicespring

public class User implements java.io.Serializable {
    private static final long serialVersionUID = -2218791253527448503L;

    private Long id;
    private String username;
    private String password;
    private Integer age;

    // 這裏省略了get set 方法

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]";
    }

}
public interface UserService {
    public List<User> queryAll();
}

4.4 dubbo-demo-provider

這個模塊用於提供服務,是使用main方法直接運行的。數據庫

一、須要添加dubbo-demo-api這個依賴,pom.xml以下apache

<dependency>
    <groupId>zxy.demo</groupId>
    <artifactId>dubbo-demo-api</artifactId>
    <version>${project.parent.version}</version>
</dependency>

二、UserService的實現

public class UserServiceImpl implements UserService {
    /**
     * 這裏經過模擬查詢數據庫返回用戶信息
     */
    @Override
    public List<User> queryAll() {
        List<User> list = new ArrayList<User>();
        for (int i = 0; i < 3; i++) {
            Random random = new Random();
            User user = new User();
            user.setAge(random.nextInt(10) + 18);
            user.setId(Long.valueOf(i + 1));
            user.setPassword("123456");
            user.setUsername("name_" + i);
            list.add(user);
        }
        return list;
    }
}

三、Spring配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="dubbo-demo-provider"/>
    
    <!-- 這裏使用的註冊中心是zookeeper -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" />
    
    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />
    
    <!-- 將該接口暴露到dubbo中 -->
    <dubbo:service interface="zxy.demo.dubbo.api.service.UserService" ref="userServiceImpl"/>

    <!-- 將具體的實現類加入到Spring容器中 -->
    <bean id="userServiceImpl" class="zxy.demo.dubbo.provider.service.UserServiceImpl" />
</beans>

:若是<dubbo:標籤有報錯的,可下載一個dubbo.xsd文件(可從個人git上找到),在Eclipse上windows->preferrence->XML->XML Catalog->add->catalog entry->file system,選擇剛剛下載的文件路徑,修改key值(dubbo.xsd),保存。在xml文件右鍵validate便可解決。

四、編寫啓動方法

ublic class Application {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {
        (new ClassPathXmlApplicationContext(new String[] { "spring/provider.xml" })).start();

        // 保持main方法不退出
        CountDownLatch latch = new CountDownLatch(1);
        latch.await();
    }

}

4.5 dubbo-demo-consumer

這個模塊用於調用dubbo-demo-provider提供的服務,使用SpringMVC進行運行。

一、須要添加dubbo-demo-api這個依賴,pom.xml以下

<dependency>
    <groupId>zxy.demo</groupId>
    <artifactId>dubbo-demo-api</artifactId>
    <version>${project.parent.version}</version>
</dependency>

二、Spring的配置

appContext.xml,主配置文件

<context:component-scan base-package="zxy.demo.dubbo.consumer">
</context:component-scan>

<import resource="classpath*:spring/dubbo-*.xml"/>
<import resource="classpath*:spring/springmvc-servlet.xml"/>

springmvc-servlet.xml

<mvc:annotation-driven />

dubbo-consumer.xml

<!-- 提供方應用信息,用於計算依賴關係 -->
<dubbo:application name="dubbo-demo-consumer" />

<!-- 這裏使用的註冊中心是zookeeper -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"
    client="zkclient" />

<!-- 從註冊中心中查找服務 -->
<dubbo:reference id="userService"
    interface="zxy.demo.dubbo.api.service.UserService" />

三、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/appContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

四、controller

調用遠程服務並返回前端數據。

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    
    @RequestMapping("/list")
    @ResponseBody
    public Object listAll() {
        return userService.queryAll();
    }
}

4.6 運行

  1. 先啓動ZooKeeper
  2. 啓動dubbo-demo-provider
  3. 啓動dubbo-demo-consumer,mvn jetty:run -Djetty.port=8083
  4. 訪問http://127.0.0.1:8083/user/list獲取數據

5 監控

自行編譯運行dubbo-admin或從個人git上下載dubbo-admin-2.5.3.war,放入tomcat中運行。默認用的註冊中心是zookeeper://127.0.0.1:2181,可修改,在WEB-INF/dubbo.properties中進行修改。

啓動後訪問http://127.0.0.1:8080/dubbo-admin-2.5.3,若是war包是更名爲ROOT.war後運行的,則訪問http://127.0.0.1:8080/,輸入帳號密碼(root/root)。

注意,因爲如今不少機器都安裝了高版本的Java8,運行這個dubbo-admin將致使出錯,報錯信息爲「Bean property 'URIType'
is not writable or has an invalid setter method」,詳見官方的issues。我本身的解決方法是下載一個低版本的Java,修改tomcat對應的Java版本,找到setclasspath.bat中的set _RUNJAVAset _RUNJDB這兩行,修改以下:

set _RUNJAVA="C:\Program Files\Java\jdk1.7.0_80\bin\java.exe"
set _RUNJDB="C:\Program Files\Java\jdk1.7.0_80\bin\jdb.exe"

6 參考

  • http://dubbo.io/
  • http://dubbo.io/User+Guide-zh.htm
  • http://dubbo.io/Developer+Guide-zh.htm
  • https://github.com/alibaba/dubbo
  • https://github.com/alibaba/dubbo/issues/50
  • http://www.jianshu.com/p/302001c1c21f
相關文章
相關標籤/搜索