中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

制作手機(jī)網(wǎng)站工具搜索到的相關(guān)信息

制作手機(jī)網(wǎng)站工具,搜索到的相關(guān)信息,中小型企業(yè)網(wǎng)站建設(shè)與管理考試,桂林有幫做公司網(wǎng)站嗎dubbo框架詳細(xì)介紹 一、網(wǎng)站應(yīng)用的演進(jìn)單一應(yīng)用架構(gòu)垂直應(yīng)用架構(gòu)分布式服務(wù)架構(gòu)流動(dòng)計(jì)算架構(gòu) 二、分布式架構(gòu)存在的問(wèn)題分布式架構(gòu)問(wèn)題架構(gòu)問(wèn)題的解決方案 三、dubbo架構(gòu)什么是dubbo?什么是RPC?RPC基本原理dubbo的功能dubbo基本架構(gòu)dubbo支持的協(xié)議dubbo的特點(diǎn) 四、dubbo實(shí)例…

dubbo框架詳細(xì)介紹

  • 一、網(wǎng)站應(yīng)用的演進(jìn)
    • 單一應(yīng)用架構(gòu)
    • 垂直應(yīng)用架構(gòu)
    • 分布式服務(wù)架構(gòu)
    • 流動(dòng)計(jì)算架構(gòu)
  • 二、分布式架構(gòu)存在的問(wèn)題
    • 分布式架構(gòu)問(wèn)題
    • 架構(gòu)問(wèn)題的解決方案
  • 三、dubbo架構(gòu)
    • 什么是dubbo?
      • 什么是RPC?
      • RPC基本原理
      • dubbo的功能
      • dubbo基本架構(gòu)
      • dubbo支持的協(xié)議
      • dubbo的特點(diǎn)
  • 四、dubbo實(shí)例
    • dubbo簡(jiǎn)單案例
        • 創(chuàng)建服務(wù)提供者
        • 創(chuàng)建服務(wù)消費(fèi)者
        • 配置tomcat ,啟動(dòng)服務(wù)
    • dubbo直連案例
        • 使用dubbo最基本的項(xiàng)目結(jié)構(gòu):
        • 代碼實(shí)現(xiàn)
    • zookeeper注冊(cè)中心
      • zookeeper注冊(cè)中心簡(jiǎn)介:
    • dubbo+zookeeper案例(重要)

一、網(wǎng)站應(yīng)用的演進(jìn)

隨著互聯(lián)網(wǎng)的發(fā)展,網(wǎng)站應(yīng)用的規(guī)模不斷擴(kuò)大,常規(guī)的垂直應(yīng)用架構(gòu)已無(wú)法應(yīng)對(duì),分布式服務(wù)架構(gòu)以及流動(dòng)計(jì)算架構(gòu)勢(shì)在必行,亟需一個(gè)治理系統(tǒng)確保架構(gòu)有條不紊的演進(jìn)。
在這里插入圖片描述

單一應(yīng)用架構(gòu)

當(dāng)網(wǎng)站流量很小時(shí),只需一個(gè)應(yīng)用,將所有功能都部署在一起,以減少部署節(jié)點(diǎn)和成本。此時(shí),用于簡(jiǎn)化增刪改查工作量的數(shù)據(jù)訪問(wèn)框架(ORM)是關(guān)鍵。

垂直應(yīng)用架構(gòu)

當(dāng)訪問(wèn)量逐漸增大,單一應(yīng)用增加機(jī)器帶來(lái)的加速度越來(lái)越小,提升效率的方法之一是將應(yīng)用拆成互不相干的幾個(gè)應(yīng)用,以提升效率。此時(shí),用于加速前端頁(yè)面開(kāi)發(fā)的Web框架(MVC)是關(guān)鍵。

分布式服務(wù)架構(gòu)

當(dāng)垂直應(yīng)用越來(lái)越多,應(yīng)用之間交互不可避免,將核心業(yè)務(wù)抽取出來(lái),作為獨(dú)立的服務(wù),逐漸形成穩(wěn)定的服務(wù)中心,使前端應(yīng)用能更快速的響應(yīng)多變的市場(chǎng)需求。此時(shí),用于提高業(yè)務(wù)復(fù)用及整合的分布式服務(wù)框架(RPC)是關(guān)鍵。

流動(dòng)計(jì)算架構(gòu)

當(dāng)服務(wù)越來(lái)越多,容量的評(píng)估,小服務(wù)資源的浪費(fèi)等問(wèn)題逐漸顯現(xiàn),此時(shí)需增加一個(gè)調(diào)度中心基于訪問(wèn)壓力實(shí)時(shí)管理集群容量,提高集群利用率。此時(shí),用于提高機(jī)器利用率的資源調(diào)度和治理中心(SOA)是關(guān)鍵。

二、分布式架構(gòu)存在的問(wèn)題

分布式架構(gòu)問(wèn)題

分布式架構(gòu)主要的難點(diǎn)是解決服務(wù)治理問(wèn)題。
在這里插入圖片描述
當(dāng)服務(wù)越來(lái)越多將會(huì)存在如下問(wèn)題:

  1. 服務(wù) URL 配置管理變得非常困難,F5 硬件負(fù)載均衡器的單點(diǎn)壓力也越來(lái)越大。
  2. 服務(wù)間依賴(lài)關(guān)系變得錯(cuò)蹤復(fù)雜,分不清啟動(dòng)順序和架構(gòu)關(guān)系
  3. 服務(wù)的調(diào)用量越來(lái)越大,服務(wù)的容量問(wèn)題就暴露出來(lái),這個(gè)服務(wù)需要多少機(jī)器支撐?什么時(shí)候該加機(jī)器?

架構(gòu)問(wèn)題的解決方案

  1. 提供注冊(cè)中心:需要一個(gè)服務(wù)注冊(cè)中心,動(dòng)態(tài)地注冊(cè)和發(fā)現(xiàn)服務(wù),使服務(wù)的位置透明。并通過(guò)在消費(fèi)方獲取服務(wù)提供方地址列表,實(shí)現(xiàn)軟負(fù)載均衡和 Failover,降低對(duì) F5 硬件負(fù)載均衡器的依賴(lài),也能減少部分成本。
  2. 自動(dòng)提供一個(gè)依賴(lài)關(guān)系圖,縷清架構(gòu)關(guān)系
  3. 實(shí)時(shí)監(jiān)控:要將服務(wù)現(xiàn)在每天的調(diào)用量,響應(yīng)時(shí)間,都統(tǒng)計(jì)出來(lái),作為容量規(guī)劃的參考指標(biāo)。要可以動(dòng)態(tài)調(diào)整權(quán)重,在線上,將某臺(tái)機(jī)器的權(quán)重一直加大,并在加大的過(guò)程中記錄響應(yīng)時(shí)間的變化,直到響應(yīng)時(shí)間到達(dá)閾值,記錄此時(shí)的訪問(wèn)量,再以此訪問(wèn)量乘以機(jī)器數(shù)反推總?cè)萘俊?/li>

三、dubbo架構(gòu)

什么是dubbo?

Apache Dubbo 是一款高性能、輕量級(jí)的開(kāi)源 Java RPC 框架,它提供了三大核心能力:

  1. 面向接口的遠(yuǎn)程方法調(diào)用
  2. 智能容錯(cuò)和負(fù)載均衡
  3. 服務(wù)自動(dòng)注冊(cè)和發(fā)現(xiàn)。

Dubbo 是一個(gè)分布式服務(wù)框架,致力于提供高性能和透明化的 RPC 遠(yuǎn)程服務(wù)調(diào)用方案、服務(wù)治理方案。

什么是RPC?

RPC 【Remote Procedure Call】是指遠(yuǎn)程過(guò)程調(diào)用,是一種進(jìn)程間通信方式,是一種技術(shù)思想,而不是規(guī)范。它允許程序調(diào)用另一個(gè)地址空間(網(wǎng)絡(luò)的另一臺(tái)機(jī)器上)的過(guò)程或函數(shù),而不用開(kāi)發(fā)人員顯式編碼這個(gè)調(diào)用的細(xì)節(jié)。調(diào)用本地方法和調(diào)用遠(yuǎn)程方法一樣。
RPC 的實(shí)現(xiàn)方式可以不同。例如 java 的 rmi, spring 遠(yuǎn)程調(diào)用等。
RPC 概念是在上世紀(jì) 80 年代由 Brue Jay Nelson(布魯·杰伊·納爾遜)提出。使用 PRC 可以將本地的調(diào)用擴(kuò)展到遠(yuǎn)程調(diào)用(分布式系統(tǒng)的其他服務(wù)器)。
RPC 的特點(diǎn)

  1. 簡(jiǎn)單:使用簡(jiǎn)單,建立分布式應(yīng)用更容易。
  2. 高效:調(diào)用過(guò)程看起來(lái)十分清晰,效率高。
  3. 通用:進(jìn)程間通訊的方式,有通用的規(guī)則

RPC基本原理

在這里插入圖片描述
PRC 調(diào)用過(guò)程:

  1. 調(diào)用方 client 要使用右側(cè) server 的功能(方法),發(fā)起對(duì)方法的調(diào)用
  2. client stub 是 PRC 中定義的存根,看做是 client 的助手。stub 把要調(diào)用的方法參數(shù)進(jìn)行序
    列化,方法名稱(chēng)和其他數(shù)據(jù)包裝起來(lái)。
  3. 通過(guò)網(wǎng)絡(luò) socket(網(wǎng)絡(luò)通信的技術(shù)),把方法調(diào)用的細(xì)節(jié)內(nèi)容發(fā)送給右側(cè)的 server
  4. server 端通過(guò) socket 接收請(qǐng)求的方法名稱(chēng),參數(shù)等數(shù)據(jù),傳給 stub。
  5. server 端接到的數(shù)據(jù)由 serverstub(server 的助手)處理,調(diào)用 server 的真正方法,處理業(yè)務(wù)
  6. server 方法處理完業(yè)務(wù),把處理的結(jié)果對(duì)象(Object)交給了助手,助手把 Object 進(jìn)行序
    列化,對(duì)象轉(zhuǎn)為二進(jìn)制數(shù)據(jù)。
  7. server 助手二進(jìn)制數(shù)據(jù)交給網(wǎng)絡(luò)處理程序
  8. 通過(guò)網(wǎng)絡(luò)將二進(jìn)制數(shù)據(jù),發(fā)送給 client。
  9. client 接數(shù)據(jù),交給 client 助手。
  10. client 助手,接收數(shù)據(jù)通過(guò)反序列化為 java 對(duì)象(Object),作為遠(yuǎn)程方法調(diào)用結(jié)果。

dubbo的功能

在這里插入圖片描述

面向接口的好處:

面向接口代理:調(diào)用接口的方法,在 A 服務(wù)器調(diào)用 B 服務(wù)器的方法,由 dubbo 實(shí)現(xiàn)對(duì) B 的調(diào)用,無(wú)需關(guān)心實(shí)現(xiàn)的細(xì)節(jié),就像 MyBatis 訪問(wèn) Dao 的接口,可以操作數(shù)據(jù)庫(kù)一樣。不用關(guān)心 Dao 接口方法的實(shí)現(xiàn)。這樣開(kāi)發(fā)是方便,舒服的

dubbo基本架構(gòu)

在這里插入圖片描述
節(jié)點(diǎn)角色說(shuō)明

節(jié)點(diǎn)角色說(shuō)明
Provider暴露服務(wù)的服務(wù)提供方
Consumer調(diào)用遠(yuǎn)程服務(wù)的服務(wù)消費(fèi)方
Registry服務(wù)注冊(cè)與發(fā)現(xiàn)的注冊(cè)中心
Monitor統(tǒng)計(jì)服務(wù)的調(diào)用次數(shù)和調(diào)用時(shí)間的監(jiān)控中心
Container服務(wù)運(yùn)行容器

調(diào)用關(guān)系說(shuō)明

  • 0.服務(wù)容器負(fù)責(zé)啟動(dòng),加載,運(yùn)行服務(wù)提供者。
  • 1.服務(wù)提供者在啟動(dòng)時(shí),向注冊(cè)中心注冊(cè)自己提供的服務(wù)。
  • 2.服務(wù)消費(fèi)者在啟動(dòng)時(shí),向注冊(cè)中心訂閱自己所需的服務(wù)。
  • 3.注冊(cè)中心返回服務(wù)提供者地址列表給消費(fèi)者,如果有變更,注冊(cè)中心將基于長(zhǎng)連接推送變更數(shù)據(jù)給消費(fèi)者。
  • 4.服務(wù)消費(fèi)者,從提供者地址列表中,基于軟負(fù)載均衡算法,選一臺(tái)提供者進(jìn)行調(diào)用,如果調(diào)用失敗,再選另一臺(tái)調(diào)用。
  • 5.服務(wù)消費(fèi)者和提供者,在內(nèi)存中累計(jì)調(diào)用次數(shù)和調(diào)用時(shí)間,定時(shí)每分鐘發(fā)送一次統(tǒng)計(jì)數(shù)據(jù)到監(jiān)控中心。

dubbo支持的協(xié)議

支持多種協(xié)議:dubbo , hessian , rmi , http, webservice , thrift , memcached , redis。
dubbo 官方推薦使用 dubbo 協(xié)議。dubbo 協(xié)議默認(rèn)端口 20880

dubbo的特點(diǎn)

Dubbo 架構(gòu)具有以下幾個(gè)特點(diǎn),分別是連通性、健壯性、伸縮性、以及向未來(lái)架構(gòu)的升級(jí)性。

四、dubbo實(shí)例

dubbo簡(jiǎn)單案例

實(shí)際生產(chǎn)應(yīng)用中不會(huì)使用直連模式,此處只是為了演示dubbo的遠(yuǎn)程調(diào)用功能,實(shí)際生產(chǎn)中一般都要結(jié)合注冊(cè)中心來(lái)使用,dubbo常用的注冊(cè)中心為zookeeper注冊(cè) 中心。

創(chuàng)建服務(wù)提供者
  1. 通過(guò)maven創(chuàng)建一個(gè)webapp應(yīng)用:dubbo-direct-provider
    結(jié)構(gòu)如下圖:
    在這里插入圖片描述
  2. pom文件中加入dubbo依賴(lài)
    由于dubbo和spring無(wú)縫對(duì)接,所以可以使用spring直接整合dubbo使用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>dubbo-direct-provider</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!-- spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.15.RELEASE</version></dependency><!-- spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.15.RELEASE</version></dependency><!-- dubbo --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.3</version></dependency></dependencies>
</project>
  1. 創(chuàng)建service接口及其實(shí)現(xiàn)類(lèi):
    接口類(lèi):SomeService.java
    實(shí)現(xiàn)類(lèi):SomeServiceImpl.java
package org.example.service;import org.springframework.stereotype.Service;public interface SomeService {public String hello(String msg);
}
package org.example.service.impl;import org.example.service.SomeService;public class SomeServiceImpl implements SomeService {public String hello(String msg) {return "hello "+msg;}
}
  1. 在resources下新建dubbo的配置文件
    dubbo-direct-provider.xml
    配置dubbo相關(guān)參數(shù)
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbohttp://dubbo.apache.org/schema/dubbo/dubbo.xsd"><!--聲明dubbo 的服務(wù)名稱(chēng),唯一應(yīng)用標(biāo)識(shí)--><dubbo:application name="dubbo-direct-provider"/><!--聲明dubbo的協(xié)議和端口號(hào)--><dubbo:protocol name="dubbo" port="20880"/><!--暴露服務(wù)(dubbo是基于接口提供服務(wù)的)ref:實(shí)現(xiàn)類(lèi)的引用registry:由于是使用直連的方式,值為“N/A”--><dubbo:service interface="org.example.service.SomeService" ref="someServiceImpl" registry="N/A"/><!--聲明接口實(shí)現(xiàn)類(lèi)--><bean id="someServiceImpl" class="org.example.service.impl.SomeServiceImpl"/>
</beans>
  1. 在web.xml文件中配置監(jiān)聽(tīng)器
    由于dubbo需要監(jiān)聽(tīng)器,監(jiān)控其服務(wù)動(dòng)態(tài)
<?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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--聲明dubbo配置文件所在位置--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:dubbo-direct-provider.xml</param-value></context-param><!--配置監(jiān)聽(tīng)器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>
  1. 修改pom.xml文件中的打包類(lèi)型為jar包,之后執(zhí)行 maven 的clean和install,將服務(wù)提供者打包到maven倉(cāng)庫(kù)
    (由于沒(méi)有使用注冊(cè)中心,打包到倉(cāng)庫(kù)是為了讓服務(wù)消費(fèi)者能夠從倉(cāng)庫(kù)中依賴(lài)當(dāng)前的服務(wù)提供者的接口)
<packaging>jar</packaging>

打包完成之后將打包類(lèi)型還原成war包,因?yàn)槲覀冃枰獑?dòng)服務(wù)提供者,為消費(fèi)者提供服務(wù)。

<packaging>war</packaging>
創(chuàng)建服務(wù)消費(fèi)者
  1. 通過(guò)maven創(chuàng)建一個(gè)webapp應(yīng)用:dubbo-direct-consumer
    結(jié)構(gòu)如下圖:
    在這里插入圖片描述
  2. pom文件中加入dubbo依賴(lài):
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>dubbo-direct-consumer</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!-- spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.15.RELEASE</version></dependency><!-- spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.15.RELEASE</version></dependency><!-- dubbo --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.3</version></dependency><!--添加服務(wù)提供者依賴(lài)--><dependency><groupId>org.example</groupId><artifactId>dubbo-direct-provider</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
  1. 創(chuàng)建SomeController.java
package org.example.web;import org.example.service.SomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class SomeController {@Autowiredprivate SomeService someService;@RequestMapping("/hello")public String hello(Model model) {String word = someService.hello("word0000");// String word = "測(cè)試";model.addAttribute("word", word);return "hello";}
}
  1. 在resources下創(chuàng)建服務(wù)消費(fèi)者的dubbo配置文件:
    dubbo-direct-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://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應(yīng)用名稱(chēng)--><dubbo:application name="dubbo-direct-consumer"/><!--配置dubbo服務(wù)--><!--id:遠(yuǎn)程服務(wù)接口的bean idinterface:遠(yuǎn)程服務(wù)接口的全名registry:由于沒(méi)有使用注冊(cè)中心,所以值依舊是"N/A"url:dubbo服務(wù)路徑,由于使用的是dubbo協(xié)議,所以值為:"dubbo://localhost:20880"--><dubbo:reference id="someService" interface="org.example.service.SomeService" registry="N/A" url="dubbo://localhost:20880" />
</beans>
  1. 由于我們要使用jsp頁(yè)面輸出測(cè)試值,需要配置springmvc.xml
    在resources下創(chuàng)建springmvc.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="org.example.web"/><!--配置注解驅(qū)動(dòng)--><mvc:annotation-driven/><!--配置視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean>
</beans>
  1. 在web.xml文件中配置servlet映射
<?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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--配置servlet映射--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--配置容器啟動(dòng)加載配置文件--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:dubbo-direct-consumer.xml,classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
  1. 創(chuàng)建hello.jsp文件
    在webapp下創(chuàng)建 hello.jsp
<%--Created by IntelliJ IDEA.User: ghqDate: 2021/6/19Time: 13:41To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>${word}</h1>
</body>
</html>
配置tomcat ,啟動(dòng)服務(wù)

注意事項(xiàng):
配置tomcat時(shí),兩個(gè)服務(wù)的HTTP port和JMX port不能一樣,否則端口會(huì)被占用。
啟動(dòng)時(shí)注意啟動(dòng)順序,先啟動(dòng)服務(wù)提供者,再啟動(dòng)服務(wù)消費(fèi)者

dubbo-direct-provider
在這里插入圖片描述dubbo-direct-consumer
在這里插入圖片描述啟動(dòng)服務(wù)消費(fèi)者之后出現(xiàn)如下所示,表明調(diào)用成功

在這里插入圖片描述

dubbo直連案例

使用dubbo最基本的項(xiàng)目結(jié)構(gòu):
  1. 接口工程:業(yè)務(wù)接口和實(shí)體類(lèi)
  2. 服務(wù)提供者工程:業(yè)務(wù)接口的實(shí)現(xiàn)類(lèi)
  3. 服務(wù)消費(fèi)者工程:消費(fèi)業(yè)務(wù)接口
代碼實(shí)現(xiàn)
  1. 新建一個(gè)maven java工程,用于提供業(yè)務(wù)接口和實(shí)體類(lèi)
    結(jié)構(gòu)如下圖所示:
    在這里插入圖片描述
  2. 在dubbo-interface中創(chuàng)建一個(gè)接口和一個(gè)實(shí)體類(lèi),用于定義接口和相關(guān)實(shí)體類(lèi)
    接口為:SomeService.java
    實(shí)體類(lèi)為:User.java
package com.guo.service;import com.guo.pojo.User;public interface SomeService {public String hello(String msg);public User userInfo();}
package com.guo.pojo;import java.io.Serializable;public class User implements Serializable {private Integer id;private String name;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}
}
  1. 創(chuàng)建一個(gè)服務(wù)提供者工程:dubbo-provider
    項(xiàng)目結(jié)構(gòu)如下:
    在這里插入圖片描述
  2. pom.xml文件中加入相關(guān)依賴(lài):
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.guo</groupId><artifactId>dubbo-provider</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!--dubbo依賴(lài)--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.3</version></dependency><!--webmvc依賴(lài)--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.15.RELEASE</version></dependency><!--spring -context依賴(lài)--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.15.RELEASE</version></dependency><!--接口jar包--><dependency><groupId>com.guo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
  1. 在當(dāng)前工程中實(shí)現(xiàn)業(yè)務(wù)接口:
    SomeServiceImpl.java
    代碼如下:
package com.guo.service;import com.guo.pojo.User;public class SomeServiceImpl implements SomeService {@Overridepublic String hello(String msg) {return msg;}@Overridepublic User userInfo() {User user = new User();user.setId(1);user.setName("張三");user.setAge(30);return user;}
}
  1. 在resource下新建dubbo服務(wù)提供者配置文件
    dubbo-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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbohttp://dubbo.apache.org/schema/dubbo/dubbo.xsd"><!--dubbo應(yīng)用名--><dubbo:application name="dubbo-provider"/><!--聲明dubbo使用協(xié)議和端口號(hào)--><dubbo:protocol name="dubbo" port="20880"/><!--暴露公有接口--><dubbo:service interface="com.guo.service.SomeService"ref="someServiceImpl"registry="N/A"/><!--聲明接口實(shí)現(xiàn)類(lèi)--><bean id="someServiceImpl" class="com.guo.service.SomeServiceImpl"/>
</beans>
  1. 在web.xml配置文件中配置監(jiān)聽(tīng)器
    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/javaeehttp://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-provider.xml</param-value></context-param><!--配置監(jiān)聽(tīng)器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>
  1. 創(chuàng)建服務(wù)消費(fèi)者工程
    結(jié)構(gòu)如下:
    在這里插入圖片描述
  2. 在pom.xml文件中加入依賴(lài)
    pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.guo</groupId><artifactId>dubbo-consumer</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.3</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.15.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.15.RELEASE</version></dependency><dependency><groupId>com.guo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
  1. 在消費(fèi)者工程中創(chuàng)建SomeController.java,用于消費(fèi)服務(wù),給頁(yè)面返回用戶數(shù)據(jù)
    SomeController.java
package com.guo.web;import com.guo.pojo.User;
import com.guo.service.SomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class SomeController {@Autowiredprivate SomeService someService;@RequestMapping("/hello")public String hello(Model model) {String hello = someService.hello("dubbo的hello測(cè)試");model.addAttribute("hello", hello);return "hello";}@RequestMapping("/user")public String userInfo(Model model) {User user = someService.userInfo();model.addAttribute("user", user);return "user";}
}
  1. 配置消費(fèi)者工程的dubbo配置文件
    dubbo-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://dubbo.apache.org/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbohttp://dubbo.apache.org/schema/dubbo/dubbo.xsd"><!--聲明dubbo應(yīng)用--><dubbo:application name="dubbo-consumer"/><!--聲明dubbo調(diào)用接口--><dubbo:reference id="someService"interface="com.guo.service.SomeService"url="dubbo://localhost:20880"registry="N/A"/>
</beans>
  1. 在springmvc.xml文件中配置視圖解析器
    springmvc.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--配置組件掃描--><context:component-scan base-package="com.guo.web"/><!--配置注解驅(qū)動(dòng)--><mvc:annotation-driven/><!--配置試圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean>
</beans>
  1. 配置web.xml文件
    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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--配置servlet--><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-consumer.xml,classpath:springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
  1. 部署工程:先啟動(dòng)服務(wù)提供者,再啟動(dòng)服務(wù)消費(fèi)者工程
    得到如圖所示:
    在這里插入圖片描述

zookeeper注冊(cè)中心

本案例使用zookeeper作為注冊(cè)中心

zookeeper注冊(cè)中心簡(jiǎn)介:

Zookeeper 是 Apache Hadoop 的子項(xiàng)目,是一個(gè)樹(shù)型的目錄服務(wù),支持變更推送,適合作為 Dubbo 服務(wù)的注冊(cè)中心,工業(yè)強(qiáng)度較高,可用于生產(chǎn)環(huán)境,并推薦使用 。

zookeeper樹(shù)形結(jié)構(gòu)圖如下:
在這里插入圖片描述
結(jié)合dubbo的流程說(shuō)明:

  • 服務(wù)提供者啟動(dòng)時(shí): 向 /dubbo/com.foo.BarService/providers 目錄下寫(xiě)入自己的 URL 地址
  • 服務(wù)消費(fèi)者啟動(dòng)時(shí): 訂閱 /dubbo/com.foo.BarService/providers 目錄下的提供者 URL 地址。并向 /dubbo/com.foo.BarService/consumers 目錄下寫(xiě)入自己的 URL 地址
  • 監(jiān)控中心啟動(dòng)時(shí): 訂閱 /dubbo/com.foo.BarService 目錄下的所有提供者和消費(fèi)者 URL 地址。

zookeeper結(jié)合dubbo實(shí)現(xiàn)的功能

  • 當(dāng)提供者出現(xiàn)斷電等異常停機(jī)時(shí),注冊(cè)中心能自動(dòng)刪除提供者信息
  • 當(dāng)注冊(cè)中心重啟時(shí),能自動(dòng)恢復(fù)注冊(cè)數(shù)據(jù),以及訂閱請(qǐng)求
  • 當(dāng)會(huì)話過(guò)期時(shí),能自動(dòng)恢復(fù)注冊(cè)數(shù)據(jù),以及訂閱請(qǐng)求
  • 當(dāng)設(shè)置 <dubbo:registry check=“false” /> 時(shí),記錄失敗注冊(cè)和訂閱請(qǐng)求,后臺(tái)定時(shí)重試
  • 可通過(guò) <dubbo:registry username=“admin” password=“1234” /> 設(shè)置 zookeeper 登錄信息
  • 可通過(guò) <dubbo:registry group=“dubbo” /> 設(shè)置 zookeeper 的根節(jié)點(diǎn),不配置將使用默認(rèn)的根節(jié)點(diǎn)。
  • 支持 * 號(hào)通配符 <dubbo:reference group="" version="" />,可訂閱服務(wù)的所有分組和所有版本的提供者

dubbo中使用zookeeper注冊(cè)中心
在使用zookeeper時(shí)需要安裝zookeeper:
zookeeper安裝包下載:https://zookeeper.apache.org/
注意:zookeeper的linux安裝包和windows安裝包為同一個(gè)。
解壓下載的安裝包,進(jìn)入conf 目錄,復(fù)制zoo_sample.cfg,并改名為:zoo.cfg
使用編輯器打開(kāi)zoo.cfg配置文件,原始內(nèi)容如下:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=\temp\data
# the port at which the clients will connect
clientPort=2181
admin.serverPort=8888
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

修改如下內(nèi)容:

  • 新增端口配置:
    由于zookeeper 3.5以后的版本啟動(dòng)時(shí)會(huì)占用8080端口,在實(shí)際的開(kāi)發(fā)中8080端口我們通常用作tomcat的服務(wù)端口,需要將zookeeper占用的此端口修改為其他端口,如修改為8888端口:
    在zoo.cfg配置文件中加入如下配置
    admin.serverPort=8888

  • 將dataDir=\temp\data修改為自定義的路徑,
    比如:
    dataDir=D:\apache-zookeeper-3.5.9-bin\data

  • 增加日志文件路徑:
    dataLogDir=D:\apache-zookeeper-3.5.9-bin\log

  • 在apache-zookeeper-3.5.9-bin根目錄下新建datalog這兩個(gè)文件夾,
    如圖所示:
    在這里插入圖片描述修改后的配置文件如下

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=D:\apache-zookeeper-3.5.9-bin\data
dataLogDir=D:\apache-zookeeper-3.5.9-bin\log
# the port at which the clients will connect
clientPort=2181
admin.serverPort=8888
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

修改完成之后,進(jìn)入bin目錄點(diǎn)擊zkServer.cmd,啟動(dòng)服務(wù)
啟動(dòng)完畢之后,啟動(dòng)客戶端zkCli.cmd檢測(cè)是否啟動(dòng)成功,
客戶端顯示:Welcome to ZooKeeper! 說(shuō)明啟動(dòng)成功,啟動(dòng)成功的客戶端顯示如下:
在這里插入圖片描述
至此,zookeeper的成功安裝,成功啟動(dòng)

注:zookeeper的服務(wù)端口號(hào)為2181

后面的zookeeper配置會(huì)用到此端口號(hào),用于將服務(wù)注冊(cè)到zookeeper.

dubbo+zookeeper案例(重要)

(重點(diǎn)案例,生產(chǎn)環(huán)境下使用的dubbo模式)

  1. 新建公用的接口工程,此工程為普通maven的jar工程,結(jié)構(gòu)如下:
    在這里插入圖片描述
    在工程中新建一個(gè)接口類(lèi)和一個(gè)實(shí)體類(lèi):
    接口類(lèi):SomeService.java
    實(shí)體類(lèi):User.java
    由于接口中的方法和參數(shù)有可能需要用到實(shí)體對(duì)象,一般會(huì)選擇將業(yè)務(wù)所需要的實(shí)體類(lèi)放在接口工程中,同時(shí)可以提供給其他業(yè)務(wù)實(shí)現(xiàn)工程使用。
    代碼如下:
    接口類(lèi):SomeService.java
package com.guo.service;import com.guo.pojo.User;public interface SomeService {public String hello(String msg);public User userInfo();}

實(shí)體類(lèi):User.java

package com.guo.pojo;import java.io.Serializable;public class User implements Serializable {private Integer id;private String name;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}
}
  1. 新建服務(wù)提供者工程:用于實(shí)現(xiàn)接口工程中的業(yè)務(wù)功能。
    結(jié)構(gòu)如圖所示:
    在這里插入圖片描述
  2. 在當(dāng)前工程的pom.xml中引入所需依賴(lài)
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.guo</groupId><artifactId>dubbo-zk-provider</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!--dubbo依賴(lài)--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.3</version></dependency><!--spring-context依賴(lài)--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.15.RELEASE</version></dependency><!--spring-webmvc依賴(lài)--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.15.RELEASE</version></dependency><!--zookeeper注冊(cè)中心客戶端依賴(lài)--><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>5.1.0</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>5.1.0</version></dependency><!--接口工程jar包--><dependency><groupId>com.guo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>

zookeeper客戶端說(shuō)明:

  • Dubbo 支持 zkclient 和 curator 兩種 Zookeeper 客戶端實(shí)現(xiàn)
  • 從 2.2.0 版本開(kāi)始缺省為 zkclient 實(shí)現(xiàn),以提升 zookeeper 客戶端的健壯性。zkclient 是 Datameer 開(kāi)源的一個(gè) Zookeeper 客戶端實(shí)現(xiàn)。
  • 注意:在2.7.x的版本中已經(jīng)移除了zkclient的實(shí)現(xiàn),如果要使用zkclient客戶端,需要自行拓展
  • 由于接口工程和業(yè)務(wù)實(shí)現(xiàn)提供者工程為兩個(gè)工程,所以需要引入接口工程java包:
 <!--接口工程jar包--><dependency><groupId>com.guo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency>
  • 本案例使用的dubbo版本2.7.3,所以使用curator客戶端
  1. 創(chuàng)建接口業(yè)務(wù)實(shí)現(xiàn)類(lèi):SomeServiceImpl.java
package com.guo.service;import com.guo.pojo.User;public class SomeServiceImpl implements SomeService {@Overridepublic String hello(String msg) {return msg;}@Overridepublic User userInfo() {User user = new User();user.setId(1);user.setName("張三");user.setAge(25);return user;}
}
  1. 在resources下新建dubbo配置文件dubbo-zk-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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbohttp://dubbo.apache.org/schema/dubbo/dubbo.xsd"><!--聲明dubbo服務(wù)提供者--><dubbo:application name="dubbo-zk-provider"/><!--配置dubbo協(xié)議以及端口號(hào)--><dubbo:protocol name="dubbo" port="20880"/><!--配置注冊(cè)中心--><dubbo:registry address="zookeeper://localhost:2181" client="curator"/><!--配置需要暴露的接口--><dubbo:service interface="com.guo.service.SomeService" ref="someService"/><!--配置接口實(shí)現(xiàn)類(lèi)--><bean id="someService" class="com.guo.service.SomeServiceImpl"/>
</beans>
  1. 在web.xml文件中配置監(jiān)聽(tīng)器 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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><display-name>Archetype Created Web Application</display-name><!--加載配置文件--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:dubbo-zk-provider.xml</param-value></context-param><!--配置監(jiān)聽(tīng)器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>
  1. 創(chuàng)建服務(wù)消費(fèi)者工程,結(jié)構(gòu)如圖所示:
    在這里插入圖片描述
  2. 在pom.xml文件中引入依賴(lài)
    pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.guo</groupId><artifactId>dubbo-zk-consumer</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!--dubbo依賴(lài)--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.3</version></dependency><!--spring-webmvc依賴(lài)--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.15.RELEASE</version></dependency><!--spring-context依賴(lài)--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.15.RELEASE</version></dependency><!--zookeeper注冊(cè)中心客戶端依賴(lài)--><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>5.1.0</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>5.1.0</version></dependency><!--業(yè)務(wù)接口依賴(lài)--><dependency><groupId>com.guo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
  1. 創(chuàng)建消費(fèi)者controller,用于調(diào)用服務(wù)提供者的業(yè)務(wù)實(shí)現(xiàn)
    SomeController.java
package com.guo.web;import com.guo.pojo.User;
import com.guo.service.SomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class SomeController {@Autowiredprivate SomeService someService;@RequestMapping("/hello")public String hello(Model model) {String hello = someService.hello("zk-dubbo-測(cè)試");model.addAttribute("hello", hello);return "hello";}@RequestMapping("/user")public String getUser(Model model) {User user = someService.userInfo();model.addAttribute("user", user);return "user";}
}
  1. 在resources下創(chuàng)建dubbo配置文件和springmvc配置文件
    dubbo-zk-consumer.xml
    springmvc.xml
    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://dubbo.apache.org/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://dubbo.apache.org/schema/dubbohttp://dubbo.apache.org/schema/dubbo/dubbo.xsd"><!--配置服務(wù)消費(fèi)者--><dubbo:application name="dubbo-zk-consumer"/><!--配置注冊(cè)中心--><dubbo:registry address="zookeeper://localhost:2181" client="curator"/><!--配置服務(wù)接口--><dubbo:reference id="someService" interface="com.guo.service.SomeService"/>
</beans>

springmvc.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--配置組件掃描--><context:component-scan base-package="com.guo.web"/><!--配置注解驅(qū)動(dòng)--><mvc:annotation-driven/><!--配置試圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean>
</beans>
  1. 配置web.xml
    在web.xml中配置servlet路勁映射
    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/javaeehttp://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:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
  1. 創(chuàng)建jsp文件用于視圖展現(xiàn),顯示我們的測(cè)試數(shù)據(jù)
    hello.jsp
    user.jsp
    hello.jsp內(nèi)容如下:
<%--Created by IntelliJ IDEA.User: ghqDate: 2021/6/20Time: 0:13To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>${hello}</h1>
</body>
</html>

uer.jsp內(nèi)容如下:

<%--Created by IntelliJ IDEA.User: ghqDate: 2021/6/20Time: 0:13To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>${user.id}</h1>
<h1>${user.name}</h1>
<h1>${user.age}</h1>
</body>
</html>

至此,整個(gè)dubbo-zookeeper項(xiàng)目案例整合完畢,只需配置tomcat啟動(dòng)服務(wù)

注意:

  1. 在啟動(dòng)項(xiàng)目之前先啟動(dòng)zookeeper服務(wù)端
  2. 啟動(dòng)項(xiàng)目的時(shí)候需要先啟動(dòng)服務(wù)提供者工程,再啟動(dòng)服務(wù)消費(fèi)者工程
  3. 更多的dubbo配置請(qǐng)參考dubbo官網(wǎng)的 [參考手冊(cè)]
    dubbo官網(wǎng):https://dubbo.apache.org/zh/
    參考手冊(cè):https://dubbo.apache.org/zh/docs/v2.7/user/references/
http://www.risenshineclean.com/news/37158.html

相關(guān)文章:

  • wordpress判斷是否登錄英文seo兼職
  • 找設(shè)計(jì)案例的網(wǎng)站接推廣app任務(wù)的平臺(tái)
  • html編輯器在哪里設(shè)置seo是哪個(gè)國(guó)家
  • jsp網(wǎng)站首頁(yè)那欄怎么做南平seo
  • 做網(wǎng)站開(kāi)發(fā)哪里好網(wǎng)站收錄一鍵提交
  • php做的大型網(wǎng)站百度搜索引擎推廣步驟
  • h5網(wǎng)站建設(shè)機(jī)構(gòu)阿里云模板建站
  • 網(wǎng)站開(kāi)發(fā)發(fā)送短信seo網(wǎng)站推廣工作內(nèi)容
  • 哪些是大型網(wǎng)站友情鏈接樣式
  • 網(wǎng)站引導(dǎo)頁(yè)是什么問(wèn)題新產(chǎn)品推廣方案策劃
  • 東莞企石網(wǎng)站建設(shè)南寧百度推廣代理公司
  • 汕頭高端網(wǎng)站開(kāi)發(fā)什么是域名
  • 網(wǎng)站在線備案太原seo全網(wǎng)營(yíng)銷(xiāo)
  • 建設(shè)一個(gè)菠菜網(wǎng)站成本我贏網(wǎng)seo優(yōu)化網(wǎng)站
  • 怎樣在微信中做網(wǎng)站六六seo基礎(chǔ)運(yùn)營(yíng)第三講
  • 做老師好還是網(wǎng)站編輯好常見(jiàn)的系統(tǒng)優(yōu)化軟件
  • 域名過(guò)期做的網(wǎng)站怎么辦全國(guó)十大跨境電商排名
  • Wordpress主題 仿魅族青島seo杭州廠商
  • 如何做淘客發(fā)單網(wǎng)站海外推廣
  • 怎樣做醫(yī)院網(wǎng)站做電商一個(gè)月能掙多少錢(qián)
  • 產(chǎn)品開(kāi)發(fā)的流程seo外包公司多少錢(qián)
  • 網(wǎng)站排名不可有利就前正規(guī)網(wǎng)絡(luò)推廣服務(wù)
  • tp框架做網(wǎng)站的優(yōu)點(diǎn)seo網(wǎng)站排名助手
  • 招聘網(wǎng)站開(kāi)發(fā)流程自動(dòng)搜索關(guān)鍵詞軟件
  • 畢業(yè)網(wǎng)站設(shè)計(jì)代做友情鏈接外鏈
  • 優(yōu)惠券網(wǎng)站要怎么做的朋友圈推廣廣告
  • 品牌建設(shè)的四條主線seo頁(yè)面優(yōu)化技術(shù)
  • 做網(wǎng)站特別注意什么百度網(wǎng)盤(pán)app怎么打開(kāi)鏈接
  • 外貿(mào)網(wǎng)站制作百度純凈版首頁(yè)入口
  • 住房和城鄉(xiāng)建設(shè)部網(wǎng)站買(mǎi)賣(mài)合同seo網(wǎng)站推廣經(jīng)理招聘