Dubbox分散式框架

来源:https://www.cnblogs.com/superbc/archive/2018/12/13/10117197.html
-Advertisement-
Play Games

一:簡介:前身是阿裡巴巴的一個開源的項目,後來停止維護,由噹噹網繼續維護,它致力於rpc遠程的調度方案.是一個服務框架 二:執行原理圖: 節點角色說明: · Provider: 暴露服務的服務提供方。 · Consumer: 調用遠程服務的服務消費方。 · Registry: 服務註冊與發現的註冊中 ...


一:簡介:前身是阿裡巴巴的一個開源的項目,後來停止維護,由噹噹網繼續維護,它致力於rpc遠程的調度方案.是一個服務框架

二:執行原理圖:

 

 

節點角色說明:

· Provider: 暴露服務的服務提供方。

· Consumer: 調用遠程服務的服務消費方。

· Registry: 服務註冊與發現的註冊中心。

· Monitor: 統計服務的調用次調和調用時間的監控中心。

· Container: 服務運行容器。

 

調用關係說明:

 

· 0. 服務容器負責啟動,載入,運行服務提供者。

 

· 1. 服務提供者在啟動時,向註冊中心註冊自己提供的服務。

 

· 2. 服務消費者在啟動時,向註冊中心訂閱自己所需的服務。

 

· 3. 註冊中心返回服務提供者地址列表給消費者,如果有變更,註冊中心將基於長連接推

 

送變更數據給消費者。

 

· 4. 服務消費者,從提供者地址列表中,基於軟負載均衡演算法,選一臺提供者進行調用,

 

如果調用失敗,再選另一臺調用。

 

· 5. 服務消費者和提供者,在記憶體中累計調用次數和調用時間,定時每分鐘發送一次統計

 

數據到監控中心。

三:使用

1.建議使用zookper註冊中心(zookper是Apacahe 的子項目)安裝到linux系統上

2.Dubbox本地jar包部署,maven中央倉庫沒有需要手動安裝

 

先將dubbo-2.8.4.jar包放到d:\setup, 然後輸入命令(jar包自行下載)

 

mvn install:install-file -Dfile=d:\setup\dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar

3.入門demo

  1.服務提供者,創建Maven工程(WARdubboxdemo-service  ,在pom.xml中引入依賴

 

<properties>

 

<spring.version>4.2.4.RELEASE</spring.version>

 

   </properties>    

 

<dependencies>

 

<!-- Spring 相關 -->

 

<dependency>

 

  <groupId>org.springframework</groupId>

 

  <artifactId>spring-context</artifactId>

 

  <version>${spring.version}</version>

 

</dependency>

 

<dependency>

 

   <groupId>org.springframework</groupId>

 

  <artifactId>spring-beans</artifactId>

 

  <version>${spring.version}</version>

 

</dependency>

 

<dependency>

 

  <groupId>org.springframework</groupId>

 

  <artifactId>spring-webmvc</artifactId>

 

  <version>${spring.version}</version>

 

</dependency>

 

<dependency>

 

  <groupId>org.springframework</groupId>

 

  <artifactId>spring-jdbc</artifactId>

 

  <version>${spring.version}</version>

 

</dependency>

 

<dependency>

 

  <groupId>org.springframework</groupId>

 

  <artifactId>spring-aspects</artifactId>

 

  <version>${spring.version}</version>

 

</dependency>

 

<dependency>

 

  <groupId>org.springframework</groupId>

 

  <artifactId>spring-jms</artifactId>

 

  <version>${spring.version}</version>

 

</dependency>

 

<dependency>

 

  <groupId>org.springframework</groupId>

 

  <artifactId>spring-context-support</artifactId>

 

  <version>${spring.version}</version>

 

</dependency>

 

 

<!-- dubbo相關 -->

 

<dependency>

 

  <groupId>com.alibaba</groupId>

 

  artifactId>dubbo</artifactId>

 

  <version>2.8.4</version>

 

</dependency>

 

<dependency>

 

  <groupId>org.apache.zookeeper</groupId>

 

  <artifactId>zookeeper</artifactId>

 

  <version>3.4.6</version>

 

</dependency>

 

<dependency>

 

  <groupId>com.github.sgroschupf</groupId>

 

  <artifactId>zkclient</artifactId>

 

  <version>0.1</version>

 

</dependency>

 

<dependency>

 

  <groupId>javassist</groupId>

 

  <artifactId>javassist</artifactId>

 

  <version>3.11.0.GA</version>

 

</dependency>

2)在工程的webapps下創建WEB-INF文件夾,創建web.xml

<!-- 載入spring容器 -->

<context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:applicationContext*.xml</param-value>

</context-param>

  <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

3)創建業務介面

創建包cn.itcast.dubbodemo.service,用於存放業務介面,創建介面

4)創建業務實現類

創建包cn.itcast.dubbodemo.service.impl ,用於存放業務實現類。創建業務實現類:

 

 註意:Service註解與原來不同,需要引入com.alibaba包下的

 

5)編寫配置文件

src/main/resources下創建applicationContext-service.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:p="http://www.springframework.org/schema/p"

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

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

     

<dubbo:application name="dubboxdemo-service"/>  

<dubbo:registry address="zookeeper://192.168.25.132:2181"/><!--虛擬機上的ip-->

<dubbo:annotation package="cn.itcast.dubboxdemo.service" />

</beans>

 

 

 註意:dubbo:annotation用於掃描@Service註解

2.服務消費者開發

創建Maven工程(WARdubboxdemo-web ,在pom.xml引入依賴 ,同“dubboxdemo-service”工程

 

2)在webapps目錄下創建WEB-INF 目錄,並創建web.xml

<?xml version="1.0" encoding="UTF-8"?>

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

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

   <!-- 解決post亂碼 -->

<filter>

<filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

<init-param>  

            <param-name>forceEncoding</param-name>  

            <param-value>true</param-value>  

        </init-param>  

</filter>

<filter-mapping>

<filter-name>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

  <servlet>

   <servlet-name>springmvc</servlet-name>   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

   <!-- 指定載入的配置文件 ,通過參數contextConfigLocation載入-->

   <init-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath:applicationContext-web.xml</param-value>

   </init-param>

  </servlet>  

  <servlet-mapping>

   <servlet-name>springmvc</servlet-name>

   <url-pattern>*.do</url-pattern>

  </servlet-mapping>

</web-app>

 

 

 3)拷貝業務介面

 

dubboxdemo-service”工程的cn.itcast.dubboxdemo.service 包以及下麵的介面拷貝至此工程。

 

 4)編寫Controller

5)編寫spring配置文件

src/main/resources下創建applicationContext-web.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:p="http://www.springframework.org/schema/p"

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

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 

<mvc:annotation-driven >

<mvc:message-converters register-defaults="false">

<bean class="org.springframework.http.converter.StringHttpMessageConverter">  

<constructor-arg value="UTF-8" />

</bean>  

</mvc:message-converters>

</mvc:annotation-driven>

<!-- 引用dubbo 服務 -->

<dubbo:application name="dubboxdemo-web" />

<dubbo:registry address="zookeeper://192.168.25.132:2181"/>

     <dubbo:annotation package="cn.itcast.dubboxdemo.controller" />

</beans>

 

 

接下來測試運行

3.6管理中心的部署(百度一下看看怎麼使用安裝部署)

 

 

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在 WEB 項目中返回 JSON 數據是常見的交互形式,在 Spring Boot 中這一切都變得十分簡單。So easy!!! 你所需具備的基礎 "什麼是 Spring Boot?" "Spring Boot 核心配置文件詳解" "Spring Boot 開啟的 2 種方式" "Spring Bo ...
  • 簡介 從今天開始,我們嘗試用2篇博客的內容量,搞定一個網站叫做“美空網”網址為:http://www.moko.cc/, 這個網站我分析了一下,我們要爬取的圖片在 下麵這個網址 http://www.moko.cc/post/1302075.html 然後在去分析一下,我需要找到一個圖片列表頁面是最 ...
  • Python 中可以讀取 word 文件的庫有 python-docx 和 pywin32。 pywin32 這個庫很強大,不僅僅可以讀取 word,但是網上介紹用 pywin32 讀取 .doc 的文章真不多,因為,真心不好用。 以下是 pywin32 讀取 .doc 的代碼示例,但是讀取表格有問 ...
  • 在我刻板的印象里,西游記里的那段孫悟空和二郎神的精彩對戰就能很好的解釋“多態”這個詞:一個孫悟空,能七十二變;一個二郎神,也能七十二變;他們都可以變成不同的形態,但只需要悄悄地喊一聲“變”。 ...
  • 題目內容: 設計一個表示分數的類Fraction。這個類用兩個int類型的變數分別表示分子和分母。 這個類的構造函數是: Fraction(int a, int b) 構造一個a/b的分數。 這個類要提供以下的功能: double toDouble(); 將分數轉換為double Fraction ...
  • Onenet控制繼電器教程 Onenet控制繼電器教程 本文基於STM32物聯網開發版:https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.29e71debNLqzWg&id=583890254748 12-18日有大額優惠券哦!!! 內 ...
  • 一 簡介:Spring Security是一個能夠為基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency ...
  • MyBatis開發原始Dao層請閱讀我的上一篇博客:MyBatis開發Dao層的兩種方式(原始Dao層開發) 接上一篇博客繼續介紹MyBatis開發Dao層的第二種方式:Mapper動態代理方式 Mapper介面開發方法只需要程式員編寫Mapper介面(相當於Dao介面),由Mybatis框架根據接 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...