制作手機網(wǎng)站工具搜索到的相關(guān)信息
dubbo框架詳細介紹
- 一、網(wǎng)站應(yīng)用的演進
- 單一應(yīng)用架構(gòu)
- 垂直應(yīng)用架構(gòu)
- 分布式服務(wù)架構(gòu)
- 流動計算架構(gòu)
- 二、分布式架構(gòu)存在的問題
- 分布式架構(gòu)問題
- 架構(gòu)問題的解決方案
- 三、dubbo架構(gòu)
- 什么是dubbo?
- 什么是RPC?
- RPC基本原理
- dubbo的功能
- dubbo基本架構(gòu)
- dubbo支持的協(xié)議
- dubbo的特點
- 四、dubbo實例
- dubbo簡單案例
- 創(chuàng)建服務(wù)提供者
- 創(chuàng)建服務(wù)消費者
- 配置tomcat ,啟動服務(wù)
- dubbo直連案例
- 使用dubbo最基本的項目結(jié)構(gòu):
- 代碼實現(xiàn)
- zookeeper注冊中心
- zookeeper注冊中心簡介:
- dubbo+zookeeper案例(重要)
一、網(wǎng)站應(yīng)用的演進
隨著互聯(lián)網(wǎng)的發(fā)展,網(wǎng)站應(yīng)用的規(guī)模不斷擴大,常規(guī)的垂直應(yīng)用架構(gòu)已無法應(yīng)對,分布式服務(wù)架構(gòu)以及流動計算架構(gòu)勢在必行,亟需一個治理系統(tǒng)確保架構(gòu)有條不紊的演進。
單一應(yīng)用架構(gòu)
當網(wǎng)站流量很小時,只需一個應(yīng)用,將所有功能都部署在一起,以減少部署節(jié)點和成本。此時,用于簡化增刪改查工作量的數(shù)據(jù)訪問框架(ORM)是關(guān)鍵。
垂直應(yīng)用架構(gòu)
當訪問量逐漸增大,單一應(yīng)用增加機器帶來的加速度越來越小,提升效率的方法之一是將應(yīng)用拆成互不相干的幾個應(yīng)用,以提升效率。此時,用于加速前端頁面開發(fā)的Web框架(MVC)是關(guān)鍵。
分布式服務(wù)架構(gòu)
當垂直應(yīng)用越來越多,應(yīng)用之間交互不可避免,將核心業(yè)務(wù)抽取出來,作為獨立的服務(wù),逐漸形成穩(wěn)定的服務(wù)中心,使前端應(yīng)用能更快速的響應(yīng)多變的市場需求。此時,用于提高業(yè)務(wù)復(fù)用及整合的分布式服務(wù)框架(RPC)是關(guān)鍵。
流動計算架構(gòu)
當服務(wù)越來越多,容量的評估,小服務(wù)資源的浪費等問題逐漸顯現(xiàn),此時需增加一個調(diào)度中心基于訪問壓力實時管理集群容量,提高集群利用率。此時,用于提高機器利用率的資源調(diào)度和治理中心(SOA)是關(guān)鍵。
二、分布式架構(gòu)存在的問題
分布式架構(gòu)問題
分布式架構(gòu)主要的難點是解決服務(wù)治理問題。
當服務(wù)越來越多將會存在如下問題:
- 服務(wù) URL 配置管理變得非常困難,F5 硬件負載均衡器的單點壓力也越來越大。
- 服務(wù)間依賴關(guān)系變得錯蹤復(fù)雜,分不清啟動順序和架構(gòu)關(guān)系
- 服務(wù)的調(diào)用量越來越大,服務(wù)的容量問題就暴露出來,這個服務(wù)需要多少機器支撐?什么時候該加機器?
架構(gòu)問題的解決方案
- 提供注冊中心:需要一個服務(wù)注冊中心,動態(tài)地注冊和發(fā)現(xiàn)服務(wù),使服務(wù)的位置透明。并通過在消費方獲取服務(wù)提供方地址列表,實現(xiàn)軟負載均衡和 Failover,降低對 F5 硬件負載均衡器的依賴,也能減少部分成本。
- 自動提供一個依賴關(guān)系圖,縷清架構(gòu)關(guān)系
- 實時監(jiān)控:要將服務(wù)現(xiàn)在每天的調(diào)用量,響應(yīng)時間,都統(tǒng)計出來,作為容量規(guī)劃的參考指標。要可以動態(tài)調(diào)整權(quán)重,在線上,將某臺機器的權(quán)重一直加大,并在加大的過程中記錄響應(yīng)時間的變化,直到響應(yīng)時間到達閾值,記錄此時的訪問量,再以此訪問量乘以機器數(shù)反推總?cè)萘俊?/li>
三、dubbo架構(gòu)
什么是dubbo?
Apache Dubbo 是一款高性能、輕量級的開源 Java RPC 框架,它提供了三大核心能力:
- 面向接口的遠程方法調(diào)用
- 智能容錯和負載均衡
- 服務(wù)自動注冊和發(fā)現(xiàn)。
Dubbo 是一個分布式服務(wù)框架,致力于提供高性能和透明化的 RPC 遠程服務(wù)調(diào)用方案、服務(wù)治理方案。
什么是RPC?
RPC 【Remote Procedure Call】是指遠程過程調(diào)用,是一種進程間通信方式,是一種技術(shù)思想,而不是規(guī)范。它允許程序調(diào)用另一個地址空間(網(wǎng)絡(luò)的另一臺機器上)的過程或函數(shù),而不用開發(fā)人員顯式編碼這個調(diào)用的細節(jié)。調(diào)用本地方法和調(diào)用遠程方法一樣。
RPC 的實現(xiàn)方式可以不同。例如 java 的 rmi, spring 遠程調(diào)用等。
RPC 概念是在上世紀 80 年代由 Brue Jay Nelson(布魯·杰伊·納爾遜)提出。使用 PRC 可以將本地的調(diào)用擴展到遠程調(diào)用(分布式系統(tǒng)的其他服務(wù)器)。
RPC 的特點
- 簡單:使用簡單,建立分布式應(yīng)用更容易。
- 高效:調(diào)用過程看起來十分清晰,效率高。
- 通用:進程間通訊的方式,有通用的規(guī)則
RPC基本原理
PRC 調(diào)用過程:
- 調(diào)用方 client 要使用右側(cè) server 的功能(方法),發(fā)起對方法的調(diào)用
- client stub 是 PRC 中定義的存根,看做是 client 的助手。stub 把要調(diào)用的方法參數(shù)進行序
列化,方法名稱和其他數(shù)據(jù)包裝起來。 - 通過網(wǎng)絡(luò) socket(網(wǎng)絡(luò)通信的技術(shù)),把方法調(diào)用的細節(jié)內(nèi)容發(fā)送給右側(cè)的 server
- server 端通過 socket 接收請求的方法名稱,參數(shù)等數(shù)據(jù),傳給 stub。
- server 端接到的數(shù)據(jù)由 serverstub(server 的助手)處理,調(diào)用 server 的真正方法,處理業(yè)務(wù)
- server 方法處理完業(yè)務(wù),把處理的結(jié)果對象(Object)交給了助手,助手把 Object 進行序
列化,對象轉(zhuǎn)為二進制數(shù)據(jù)。 - server 助手二進制數(shù)據(jù)交給網(wǎng)絡(luò)處理程序
- 通過網(wǎng)絡(luò)將二進制數(shù)據(jù),發(fā)送給 client。
- client 接數(shù)據(jù),交給 client 助手。
- client 助手,接收數(shù)據(jù)通過反序列化為 java 對象(Object),作為遠程方法調(diào)用結(jié)果。
dubbo的功能
面向接口的好處:
面向接口代理:調(diào)用接口的方法,在 A 服務(wù)器調(diào)用 B 服務(wù)器的方法,由 dubbo 實現(xiàn)對 B 的調(diào)用,無需關(guān)心實現(xiàn)的細節(jié),就像 MyBatis 訪問 Dao 的接口,可以操作數(shù)據(jù)庫一樣。不用關(guān)心 Dao 接口方法的實現(xiàn)。這樣開發(fā)是方便,舒服的
dubbo基本架構(gòu)
節(jié)點角色說明
節(jié)點 | 角色說明 |
---|---|
Provider | 暴露服務(wù)的服務(wù)提供方 |
Consumer | 調(diào)用遠程服務(wù)的服務(wù)消費方 |
Registry | 服務(wù)注冊與發(fā)現(xiàn)的注冊中心 |
Monitor | 統(tǒng)計服務(wù)的調(diào)用次數(shù)和調(diào)用時間的監(jiān)控中心 |
Container | 服務(wù)運行容器 |
調(diào)用關(guān)系說明
- 0.服務(wù)容器負責啟動,加載,運行服務(wù)提供者。
- 1.服務(wù)提供者在啟動時,向注冊中心注冊自己提供的服務(wù)。
- 2.服務(wù)消費者在啟動時,向注冊中心訂閱自己所需的服務(wù)。
- 3.注冊中心返回服務(wù)提供者地址列表給消費者,如果有變更,注冊中心將基于長連接推送變更數(shù)據(jù)給消費者。
- 4.服務(wù)消費者,從提供者地址列表中,基于軟負載均衡算法,選一臺提供者進行調(diào)用,如果調(diào)用失敗,再選另一臺調(diào)用。
- 5.服務(wù)消費者和提供者,在內(nèi)存中累計調(diào)用次數(shù)和調(diào)用時間,定時每分鐘發(fā)送一次統(tǒng)計數(shù)據(jù)到監(jiān)控中心。
dubbo支持的協(xié)議
支持多種協(xié)議:dubbo , hessian , rmi , http, webservice , thrift , memcached , redis。
dubbo 官方推薦使用 dubbo 協(xié)議。dubbo 協(xié)議默認端口 20880
dubbo的特點
Dubbo 架構(gòu)具有以下幾個特點,分別是連通性、健壯性、伸縮性、以及向未來架構(gòu)的升級性。
四、dubbo實例
dubbo簡單案例
實際生產(chǎn)應(yīng)用中不會使用直連模式,此處只是為了演示dubbo的遠程調(diào)用功能,實際生產(chǎn)中一般都要結(jié)合注冊中心來使用,dubbo常用的注冊中心為zookeeper注冊 中心。
創(chuàng)建服務(wù)提供者
- 通過maven創(chuàng)建一個webapp應(yīng)用:dubbo-direct-provider
結(jié)構(gòu)如下圖:
- pom文件中加入dubbo依賴
由于dubbo和spring無縫對接,所以可以使用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>
- 創(chuàng)建service接口及其實現(xiàn)類:
接口類:SomeService.java
實現(xiàn)類: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;}
}
- 在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ù)名稱,唯一應(yīng)用標識--><dubbo:application name="dubbo-direct-provider"/><!--聲明dubbo的協(xié)議和端口號--><dubbo:protocol name="dubbo" port="20880"/><!--暴露服務(wù)(dubbo是基于接口提供服務(wù)的)ref:實現(xiàn)類的引用registry:由于是使用直連的方式,值為“N/A”--><dubbo:service interface="org.example.service.SomeService" ref="someServiceImpl" registry="N/A"/><!--聲明接口實現(xiàn)類--><bean id="someServiceImpl" class="org.example.service.impl.SomeServiceImpl"/>
</beans>
- 在web.xml文件中配置監(jiān)聽器
由于dubbo需要監(jiān)聽器,監(jiān)控其服務(wù)動態(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)聽器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>
- 修改pom.xml文件中的打包類型為jar包,之后執(zhí)行 maven 的clean和install,將服務(wù)提供者打包到maven倉庫
(由于沒有使用注冊中心,打包到倉庫是為了讓服務(wù)消費者能夠從倉庫中依賴當前的服務(wù)提供者的接口)
<packaging>jar</packaging>
打包完成之后將打包類型還原成war包,因為我們需要啟動服務(wù)提供者,為消費者提供服務(wù)。
<packaging>war</packaging>
創(chuàng)建服務(wù)消費者
- 通過maven創(chuàng)建一個webapp應(yīng)用:dubbo-direct-consumer
結(jié)構(gòu)如下圖:
- pom文件中加入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-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ù)提供者依賴--><dependency><groupId>org.example</groupId><artifactId>dubbo-direct-provider</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
- 創(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 = "測試";model.addAttribute("word", word);return "hello";}
}
- 在resources下創(chuàng)建服務(wù)消費者的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)用名稱--><dubbo:application name="dubbo-direct-consumer"/><!--配置dubbo服務(wù)--><!--id:遠程服務(wù)接口的bean idinterface:遠程服務(wù)接口的全名registry:由于沒有使用注冊中心,所以值依舊是"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>
- 由于我們要使用jsp頁面輸出測試值,需要配置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ū)動--><mvc:annotation-driven/><!--配置視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean>
</beans>
- 在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><!--配置容器啟動加載配置文件--><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>
- 創(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 ,啟動服務(wù)
注意事項:
配置tomcat時,兩個服務(wù)的HTTP port和JMX port不能一樣,否則端口會被占用。
啟動時注意啟動順序,先啟動服務(wù)提供者,再啟動服務(wù)消費者
dubbo-direct-provider
dubbo-direct-consumer
啟動服務(wù)消費者之后出現(xiàn)如下所示,表明調(diào)用成功
dubbo直連案例
使用dubbo最基本的項目結(jié)構(gòu):
- 接口工程:業(yè)務(wù)接口和實體類
- 服務(wù)提供者工程:業(yè)務(wù)接口的實現(xiàn)類
- 服務(wù)消費者工程:消費業(yè)務(wù)接口
代碼實現(xiàn)
- 新建一個maven java工程,用于提供業(yè)務(wù)接口和實體類
結(jié)構(gòu)如下圖所示:
- 在dubbo-interface中創(chuàng)建一個接口和一個實體類,用于定義接口和相關(guān)實體類
接口為:SomeService.java
實體類為: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 +'}';}
}
- 創(chuàng)建一個服務(wù)提供者工程:dubbo-provider
項目結(jié)構(gòu)如下:
- pom.xml文件中加入相關(guān)依賴:
<?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依賴--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.3</version></dependency><!--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><!--接口jar包--><dependency><groupId>com.guo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
- 在當前工程中實現(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;}
}
- 在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é)議和端口號--><dubbo:protocol name="dubbo" port="20880"/><!--暴露公有接口--><dubbo:service interface="com.guo.service.SomeService"ref="someServiceImpl"registry="N/A"/><!--聲明接口實現(xiàn)類--><bean id="someServiceImpl" class="com.guo.service.SomeServiceImpl"/>
</beans>
- 在web.xml配置文件中配置監(jiān)聽器
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)聽器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>
- 創(chuàng)建服務(wù)消費者工程
結(jié)構(gòu)如下:
- 在pom.xml文件中加入依賴
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>
- 在消費者工程中創(chuàng)建SomeController.java,用于消費服務(wù),給頁面返回用戶數(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測試");model.addAttribute("hello", hello);return "hello";}@RequestMapping("/user")public String userInfo(Model model) {User user = someService.userInfo();model.addAttribute("user", user);return "user";}
}
- 配置消費者工程的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>
- 在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ū)動--><mvc:annotation-driven/><!--配置試圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean>
</beans>
- 配置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>
- 部署工程:先啟動服務(wù)提供者,再啟動服務(wù)消費者工程
得到如圖所示:
zookeeper注冊中心
本案例使用zookeeper作為注冊中心
zookeeper注冊中心簡介:
Zookeeper 是 Apache Hadoop 的子項目,是一個樹型的目錄服務(wù),支持變更推送,適合作為 Dubbo 服務(wù)的注冊中心,工業(yè)強度較高,可用于生產(chǎn)環(huán)境,并推薦使用 。
zookeeper樹形結(jié)構(gòu)圖如下:
結(jié)合dubbo的流程說明:
- 服務(wù)提供者啟動時: 向 /dubbo/com.foo.BarService/providers 目錄下寫入自己的 URL 地址
- 服務(wù)消費者啟動時: 訂閱 /dubbo/com.foo.BarService/providers 目錄下的提供者 URL 地址。并向 /dubbo/com.foo.BarService/consumers 目錄下寫入自己的 URL 地址
- 監(jiān)控中心啟動時: 訂閱 /dubbo/com.foo.BarService 目錄下的所有提供者和消費者 URL 地址。
zookeeper結(jié)合dubbo實現(xiàn)的功能
- 當提供者出現(xiàn)斷電等異常停機時,注冊中心能自動刪除提供者信息
- 當注冊中心重啟時,能自動恢復(fù)注冊數(shù)據(jù),以及訂閱請求
- 當會話過期時,能自動恢復(fù)注冊數(shù)據(jù),以及訂閱請求
- 當設(shè)置 <dubbo:registry check=“false” /> 時,記錄失敗注冊和訂閱請求,后臺定時重試
- 可通過 <dubbo:registry username=“admin” password=“1234” /> 設(shè)置 zookeeper 登錄信息
- 可通過 <dubbo:registry group=“dubbo” /> 設(shè)置 zookeeper 的根節(jié)點,不配置將使用默認的根節(jié)點。
- 支持 * 號通配符 <dubbo:reference group="" version="" />,可訂閱服務(wù)的所有分組和所有版本的提供者
dubbo中使用zookeeper注冊中心
在使用zookeeper時需要安裝zookeeper:
zookeeper安裝包下載:https://zookeeper.apache.org/
注意:zookeeper的linux安裝包和windows安裝包為同一個。
解壓下載的安裝包,進入conf 目錄,復(fù)制zoo_sample.cfg
,并改名為:zoo.cfg
使用編輯器打開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以后的版本啟動時會占用8080端口,在實際的開發(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根目錄下新建
data
和log
這兩個文件夾,
如圖所示:
修改后的配置文件如下
# 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
修改完成之后,進入bin目錄點擊zkServer.cmd
,啟動服務(wù)
啟動完畢之后,啟動客戶端zkCli.cmd
檢測是否啟動成功,
客戶端顯示:Welcome to ZooKeeper! 說明啟動成功,啟動成功的客戶端顯示如下:
至此,zookeeper的成功安裝,成功啟動
注:zookeeper的服務(wù)端口號為2181
后面的zookeeper配置會用到此端口號,用于將服務(wù)注冊到zookeeper.
dubbo+zookeeper案例(重要)
(重點案例,生產(chǎn)環(huán)境下使用的dubbo模式)
- 新建公用的接口工程,此工程為普通maven的jar工程,結(jié)構(gòu)如下:
在工程中新建一個接口類和一個實體類:
接口類:SomeService.java
實體類:User.java
由于接口中的方法和參數(shù)有可能需要用到實體對象,一般會選擇將業(yè)務(wù)所需要的實體類放在接口工程中,同時可以提供給其他業(yè)務(wù)實現(xiàn)工程使用。
代碼如下:
接口類:SomeService.java
package com.guo.service;import com.guo.pojo.User;public interface SomeService {public String hello(String msg);public User userInfo();}
實體類: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 +'}';}
}
- 新建服務(wù)提供者工程:用于實現(xiàn)接口工程中的業(yè)務(wù)功能。
結(jié)構(gòu)如圖所示:
- 在當前工程的
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-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依賴--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.3</version></dependency><!--spring-context依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.15.RELEASE</version></dependency><!--spring-webmvc依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.15.RELEASE</version></dependency><!--zookeeper注冊中心客戶端依賴--><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客戶端說明:
- Dubbo 支持 zkclient 和 curator 兩種 Zookeeper 客戶端實現(xiàn)
- 從 2.2.0 版本開始缺省為 zkclient 實現(xiàn),以提升 zookeeper 客戶端的健壯性。zkclient 是 Datameer 開源的一個 Zookeeper 客戶端實現(xiàn)。
- 注意:在2.7.x的版本中已經(jīng)移除了zkclient的實現(xiàn),如果要使用zkclient客戶端,需要自行拓展
- 由于接口工程和業(yè)務(wù)實現(xiàn)提供者工程為兩個工程,所以需要引入接口工程java包:
<!--接口工程jar包--><dependency><groupId>com.guo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency>
- 本案例使用的dubbo版本2.7.3,所以使用curator客戶端
- 創(chuàng)建接口業(yè)務(wù)實現(xiàn)類:
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;}
}
- 在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é)議以及端口號--><dubbo:protocol name="dubbo" port="20880"/><!--配置注冊中心--><dubbo:registry address="zookeeper://localhost:2181" client="curator"/><!--配置需要暴露的接口--><dubbo:service interface="com.guo.service.SomeService" ref="someService"/><!--配置接口實現(xiàn)類--><bean id="someService" class="com.guo.service.SomeServiceImpl"/>
</beans>
- 在web.xml文件中配置監(jiān)聽器
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)聽器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>
- 創(chuàng)建服務(wù)消費者工程,結(jié)構(gòu)如圖所示:
- 在pom.xml文件中引入依賴
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依賴--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.3</version></dependency><!--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><!--zookeeper注冊中心客戶端依賴--><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ù)接口依賴--><dependency><groupId>com.guo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>
- 創(chuàng)建消費者controller,用于調(diào)用服務(wù)提供者的業(yè)務(wù)實現(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-測試");model.addAttribute("hello", hello);return "hello";}@RequestMapping("/user")public String getUser(Model model) {User user = someService.userInfo();model.addAttribute("user", user);return "user";}
}
- 在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ù)消費者--><dubbo:application name="dubbo-zk-consumer"/><!--配置注冊中心--><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ū)動--><mvc:annotation-driven/><!--配置試圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean>
</beans>
- 配置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>
- 創(chuàng)建jsp文件用于視圖展現(xiàn),顯示我們的測試數(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>
至此,整個dubbo-zookeeper項目案例整合完畢,只需配置tomcat啟動服務(wù)
注意:
- 在啟動項目之前先啟動zookeeper服務(wù)端
- 啟動項目的時候需要先啟動服務(wù)提供者工程,再啟動服務(wù)消費者工程
- 更多的dubbo配置請參考dubbo官網(wǎng)的
[參考手冊]
dubbo官網(wǎng):https://dubbo.apache.org/zh/
參考手冊:https://dubbo.apache.org/zh/docs/v2.7/user/references/