天天看點

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

Dubbo

  • 第一章、Dubbo介紹
    • 1.1 什麼是分布式架構
    • 1.2 應用架構的發展演變
    • 1.3 Dubbo架構
      • 1.3.1 為什麼Dubbo說自己性能高
      • 1.3.2 别的RPC架構
      • 1.3.3 Dubbo的前世今生
      • 1.3.4 Dubbo的概述
      • 1.3.4 Dubbo的基本架構
      • 1.3.5 Dubbo支援的協定
      • 1.3.6 Dubbo直連方式案例(了解)
        • 1.3.6.1 建立服務提供者
          • 1.利用Maven建立web項目001-link-userservice-provider
          • 2.在Maven中添加spring和dubbo的依賴
          • 3.建立User實體類
          • 4.建立UserService接口和實作類
          • 5.建立dubbo配置檔案dubbo-userservice-provider.xml
          • 6.在web.xml中添加spring監聽器
          • 7.添加服務提供者的Tomcat
        • 1.3.6.2 建立服務消費者
          • 1.利用Maven建立web項目002-link-comsumer
          • 2.在Maven中添加spring、dubbo和前面建立的服務提供者的依賴
          • 3.建立dubbo配置檔案
          • 4.在web.xml中配置中央排程器
          • 5.建立UserController類
          • 6.建立userDetail頁面
          • 7.建立服務消費者的Tomcat
          • 8.在浏覽器通路,先啟動提供者的Tomcat,再啟動消費者的Tomcat
      • 1.3.7 Dubbo服務化最佳實踐(了解)
        • 1.3.7.1 分包
        • 1.3.7.2 粒度
        • 1.3.7.3 版本
        • 1.3.7.4 改造Dubbo項目
          • 1.3.7.4.1 建立公共資源項目
          • 1.3.7.4.2 建立服務提供者004-link-userservice-provider
          • 1.在pom.xml中添加依賴
          • 2.建立dubbo-userservice-provider.xml
          • 3.在web.xml中配置監聽器
          • 4.建立service接口的實作類
          • 5.建立提供者的Tomcat
        • 1.3.7.4.3 建立服務消費者005-link-cosumer
          • 1.在pom.xml中添加依賴
          • 2.建立aubbo-consumer.xml
          • 3.建立applicationContext.xml
          • 4.在web.xml中配置中央排程器
          • 5.建立UserController類
          • 6.建立結果頁面
          • 7.配置消費者的Tomcat
          • 8.浏覽器通路,先啟動提供者的Tomcat,再啟動消費者的Tomcat
  • 第二章、注冊中心-Zookeeper
    • 2.1 注冊中心概述
    • 2.2 注冊中心工作方式
    • 2.3 Zookeeper注冊中心
    • 2.4 Windows平台Zookeeper安裝,配置
      • 2.4.1 下載下傳Zookeeper
      • 2.4.2 配置Zookeeper
        • 1.下載下傳的檔案zookeeper-3.5.8-beta.tar.gz. 解壓後到目錄就可以了,例如D:\Program Files\zookeeper\apache-zookeeper-3.5.8-bin\conf修改zookeeper-3.5.8/conf/目錄下配置檔案
        • 2.複制zoo-sample.cfg改名為zoo.cfg檔案内容
        • 3.打開bin目錄中的,zkServer.cmd
    • 2.6 改造dubbo—使用Zookeeper(了解)
      • 2.6.1 啟動zookeeper
      • 2.6.2 建立公共資源
        • 1.建立user實體類
        • 2.建立service包下的userService接口
      • 2.6.3 建立服務提供者
        • 1.在pom.xml添加依賴
        • 2.建立dubbo-zk-userservice-provider.xml
        • 3.在web.xml中注冊監聽器
        • 4.建立service的實體類UserServiceImpl
        • 5.建立服務提供者的Tomcat
      • 2.5.4 建立服務消費者
        • 1.在pom.xml中添加依賴
        • 2.建立dubbo-zk-consumer.xml
        • 3.建立applicationContext.xml
        • 4.在web.xml中注冊中央排程器
        • 5.建立UserController類
        • 6.建立結果頁面
        • 7.建立服務消費者的Tomcat
        • 8.先啟動提供者的Tomcat,再啟動消費者的Tomcat,浏覽器通路
    • 2.7 使用version版本号(掌握)
  • 第三章、dubbo的配置
    • 3.1 配置原則
    • 3.2 關閉檢查
    • 3.3 重試次數
    • 3.4 逾時時間
        • 1.dubbo服務端
        • 2.dubbo消費端
  • 第四章、監控中心
    • 4.1 什麼是監控中心
    • 4.2 釋出配置中心
      • 4.2.1 下載下傳監控中心
      • 4.2.2 修改配置dubbo-properties檔案,運作即可

第一章、Dubbo介紹

1.1 什麼是分布式架構

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.2 應用架構的發展演變

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3 Dubbo架構

1.3.1 為什麼Dubbo說自己性能高

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.2 别的RPC架構

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.3 Dubbo的前世今生

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.4 Dubbo的概述

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.4 Dubbo的基本架構

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.5 Dubbo支援的協定

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.6 Dubbo直連方式案例(了解)

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.6.1 建立服務提供者

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
1.利用Maven建立web項目001-link-userservice-provider
2.在Maven中添加spring和dubbo的依賴
<dependencies>
      <!--spring依賴-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.3.2</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.3.2</version>
      </dependency>
      <!--Dubbo依賴-->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>dubbo</artifactId>
          <version>2.6.2</version>
      </dependency>
  </dependencies>
  <build>
      <!--jdk1.8編譯插件-->
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
              </configuration>
          </plugin>
      </plugins>
  </build>
           
3.建立User實體類

隻要是分布式開發,實體類必須進行序列化操作 Implements Serializable

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
4.建立UserService接口和實作類

UserService接口

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

UserServiceImp實作類

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
5.建立dubbo配置檔案dubbo-userservice-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--服務提供者聲明名稱:必須保證服務名稱的唯一性,它的名稱是dubbo内部使用的唯一辨別-->
    <dubbo:application name="001-link-userservice-provider"/>

    <!--
        通路服務協定的名稱及端口号,dubbo官方推薦使用的是dubbo協定
        端口号預設為:20880
        name:指定協定的名稱
        port:指定協定的端口号(預設20880)
    -->
    <dubbo:protocol name="dubbo" port="20880"/>
    
    <!--
        暴露服務接口==>dubbo:service
        interface:暴露服務接口的全限定類名
        ref:接口引用的實作類在spring容器中的辨別
        registry:如果不使用中心,則值為:N/A
    -->
    <dubbo:service interface="com.wangbeita.dubbo.service.UserService" ref="userService" registry="N/A" />

    <!--将接口的實作類加載到spring容器中-->
    <bean id="userService" class="com.wangbeita.dubbo.service.impl.UserServiceImpl"/>
</beans>
           
6.在web.xml中添加spring監聽器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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">
  
    <!--注冊監聽器-->
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:dubbo-userservice-provider.xml</param-value>
    </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>
           
7.添加服務提供者的Tomcat
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.6.2 建立服務消費者

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
1.利用Maven建立web項目002-link-comsumer
2.在Maven中添加spring、dubbo和前面建立的服務提供者的依賴

先将前面的服務提供者進行打包字尾是.jar

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.2</version>
    </dependency>
    <!--Dubbo依賴-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>

    <!--添加服務提供者
      這樣才能使用它中的實體類
    -->
    <dependency>
      <groupId>com.wangbeita</groupId>
      <artifactId>001-link-userservice-provider</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
  <build>
    <!--jdk1.8編譯插件-->
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
           
3.建立dubbo配置檔案
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
4.在web.xml中配置中央排程器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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">

  <!--配置中央排程器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:application.xml,classpath:dubbo-consumer.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

           
5.建立UserController類
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
6.建立userDetail頁面
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
7.建立服務消費者的Tomcat
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
8.在浏覽器通路,先啟動提供者的Tomcat,再啟動消費者的Tomcat
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.7 Dubbo服務化最佳實踐(了解)

不是工作中使用

直連方法存在的問題,消費者可以不面向接口,直接通路接口的實作類

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.7.1 分包

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.7.2 粒度

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.7.3 版本

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.7.4 改造Dubbo項目

1.3.7.4.1 建立公共資源項目

注意:003-link-interface是一個Maven java項目

建立entity包下的User實體類

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

建立service包下的UserService接口

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
1.3.7.4.2 建立服務提供者004-link-userservice-provider
1.在pom.xml中添加依賴
<dependencies>
    <!--spring依賴-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.2</version>
    </dependency>
    <!--Dubbo依賴-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>

    <dependency>
      <groupId>com.wangbeita</groupId>
      <artifactId>003-link-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
  <build>
    <!--jdk1.8編譯插件-->
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
           
2.建立dubbo-userservice-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--聲明dubbo服務提供者的名稱:保證唯一性-->
    <dubbo:application name="004-link-userservice-provider"/>

    <!--
        通路服務協定的名稱及端口号,dubbo官方推薦使用的是dubbo協定
        端口号預設為:20880
        name:指定協定的名稱
        port:指定協定的端口号(預設20880)
    -->
    <dubbo:protocol name="dubbo" port="20880"/>
    
    <!--暴露端口服務-->
    <dubbo:service interface="com.wangbeita.dubbo.service.UserService" ref="userService" registry="N/A"/>

    <!--加載業務實作類到spring容器中-->
    <bean id="userService" class="com.wangbeita.dubbo.service.impl.UserServiceImpl"/>
    
</beans>
           
3.在web.xml中配置監聽器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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">

  <!--注冊監聽器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:dubbo-userservice-provider.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

           
4.建立service接口的實作類
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
5.建立提供者的Tomcat
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.3.7.4.3 建立服務消費者005-link-cosumer

1.在pom.xml中添加依賴
<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.2</version>
    </dependency>
    <!--Dubbo依賴-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>

    <!--添加003的接口工程-->
    <dependency>
      <groupId>com.wangbeita</groupId>
      <artifactId>003-link-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

  </dependencies>
  <build>
    <!--jdk1.8編譯插件-->
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
           
2.建立aubbo-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--聲明服務消費者名稱:保證唯一性-->
    <dubbo:application name="005-link-cosumer"/>

    <!--引用遠端接口服務-->
    <dubbo:reference
            id="userService"
            interface="com.wangbeita.dubbo.service.UserService"
            url="dubbo://localhost:20880"
            registry="N/A"
    />

</beans>
           
3.建立applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--掃描元件-->
    <context:component-scan base-package="com.wangbeita.dubbo.web"/>

    <!--配置注解驅動-->
    <mvc:annotation-driven/>

    <!--配置視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
           
4.在web.xml中配置中央排程器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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">

  <!--配置中央排程器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml,classpath:dubbo-consumer.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

           
5.建立UserController類
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
6.建立結果頁面
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
7.配置消費者的Tomcat
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
8.浏覽器通路,先啟動提供者的Tomcat,再啟動消費者的Tomcat
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

第二章、注冊中心-Zookeeper

2.1 注冊中心概述

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.2 注冊中心工作方式

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.3 Zookeeper注冊中心

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.4 Windows平台Zookeeper安裝,配置

2.4.1 下載下傳Zookeeper

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.4.2 配置Zookeeper

1.下載下傳的檔案zookeeper-3.5.8-beta.tar.gz. 解壓後到目錄就可以了,例如D:\Program Files\zookeeper\apache-zookeeper-3.5.8-bin\conf修改zookeeper-3.5.8/conf/目錄下配置檔案

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.複制zoo-sample.cfg改名為zoo.cfg檔案内容

再修改dataDir,添加admin.serverPort=8888

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

3.打開bin目錄中的,zkServer.cmd

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

看到紅框的内容就說明配置成功

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.6 改造dubbo—使用Zookeeper(了解)

2.6.1 啟動zookeeper

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心
Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.6.2 建立公共資源

建立一個006-zk-interface的maven java項目

1.建立user實體類

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.建立service包下的userService接口

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.6.3 建立服務提供者

建立一個007-zk-userservice-provider的maven web工程

1.在pom.xml添加依賴

<dependencies>
    <!--spring依賴-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.2</version>
    </dependency>

    <!--springmvc依賴-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.2</version>
    </dependency>

    <!--dubbo依賴-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>

    <!--接口工程依賴-->
    <dependency>
      <groupId>com.wangbeita</groupId>
      <artifactId>006-link-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <!--zookeeper依賴-->
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.1.0</version>
    </dependency>
  </dependencies>
           

2.建立dubbo-zk-userservice-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--聲明dubbo服務提供者的名稱:保證唯一性-->
    <dubbo:application name="007-zk-userservice-provider"/>

    <!--聲明dubbo使用的協定名和端口号-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--現在要使用zookeper注冊中心-->
    <!--指定注冊中心位址和端口号-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--暴露服務接口-->
    <dubbo:service interface="com.wangbeita.dubbo.service.UserService" ref="userServiceImpl" timeout="1200000"/>

    <!--加載接口實作類-->
    <bean id="userServiceImpl" class="com.wangbeita.dubbo.service.impl.UserServiceImpl"/>
</beans>
           

3.在web.xml中注冊監聽器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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">
  <!--注冊監聽器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:dubbo-zk-userservice-provider.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

           

4.建立service的實體類UserServiceImpl

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

5.建立服務提供者的Tomcat

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.5.4 建立服務消費者

建立008-zk-consumer的maven web項目

1.在pom.xml中添加依賴

<dependencies>
    <!--spring依賴-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.2</version>
    </dependency>

    <!--springmvc依賴-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.2</version>
    </dependency>

    <!--dubbo依賴-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>

    <!--接口工程依賴-->
    <dependency>
      <groupId>com.wangbeita</groupId>
      <artifactId>006-link-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <!--zookeeper依賴-->
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.1.0</version>
    </dependency>
  </dependencies>
           

2.建立dubbo-zk-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--聲明dubbo服務消費者名稱:保證唯一性-->
    <dubbo:application name="008-zk-consumer"/>

    <!--指定注冊中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--引用遠端接口服務-->
    <dubbo:reference id="userService" interface="com.wangbeita.dubbo.service.UserService"/>
</beans>
           

3.建立applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--配置元件掃描器-->
    <context:component-scan base-package="com.wangbeita.dubbo.web"/>

    <!--配置注解驅動-->
    <mvc:annotation-driven/>

    <!--配置視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
           

4.在web.xml中注冊中央排程器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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">
  <!--配置中央排程器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:dubbo-zk-consumer.xml,classpath:applicationContext.xml</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

           

5.建立UserController類

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

6.建立結果頁面

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

7.建立服務消費者的Tomcat

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

8.先啟動提供者的Tomcat,再啟動消費者的Tomcat,浏覽器通路

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.7 使用version版本号(掌握)

工作中使用

一般用在如果接口有個老功能,UserServiceImpl,此時要更新一個新功能UserServiceImpl2,但是我們不能把老功能去掉,因為還一部分使用者在使用,此時我就要用到version來區分使用的版本.

  1. 如果有兩個實作類的話,我們可以在服務提供者duboo配置檔案中配置版本号來區分
<dubbo:service interface="com.wangbeita.dubbo.service.UserService" version="1.0.0" ref="userServiceImpl1" />
<dubbo:service interface="com.wangbeita.dubbo.service.UserService" version="2.0.0" ref="userServiceImpl2" />
           
  1. 可以在消費者duboo配置檔案中配置版本号來區分調用
<dubbo:reference id="userService"  interface="com.wangbeita.dubbo.service.UserService" version="1.0.0" />
    <dubbo:reference id="userService"  interface="com.wangbeita.dubbo.service.UserService" version="2.0.0" />
           

第三章、dubbo的配置

3.1 配置原則

在服務提供者配置通路參數。因為服務提供者更了解服務的各種參數。

3.2 關閉檢查

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

3.3 重試次數

一般不用

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

3.4 逾時時間

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

1.dubbo服務端

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

2.dubbo消費端

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

第四章、監控中心

4.1 什麼是監控中心

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

4.2 釋出配置中心

4.2.1 下載下傳監控中心

https://github.com/apache/incubator-dubbo-ops

這裡下載下傳的是源代碼,需要手工編譯才能使用。以下是編譯

4.2.2 修改配置dubbo-properties檔案,運作即可

Dubbo知識點第一章、Dubbo介紹第二章、注冊中心-Zookeeper第三章、dubbo的配置第四章、監控中心

繼續閱讀