dubbo的具體使用

dubbo的具體使用:css

dubbo簡介:前端

1.1.    什麼是dubbo

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

1-二、使用dubbo的優勢:mysql

       使用dubbo。使用rpc協議進行遠程調用,直接使用socket通訊。傳輸效率高,而且能夠統計出系統之間的調用關係、調用次數。git

 

1-二、Webservice:效率不高基於soap協議。項目中不推薦使用。github

 

Dubbo就是資源調度和治理中心的管理工具。web

  • 單一應用架構
    • 當網站流量很小時,只需一個應用,將全部功能都部署在一塊兒,以減小部署節點和成本。
    • 此時,用於簡化增刪改查工做量的 數據訪問框架(ORM) 是關鍵。
  • 垂直應用架構
    • 當訪問量逐漸增大,單一應用增長機器帶來的加速度愈來愈小,將應用拆成互不相干的幾個應用,以提高效率。
    • 此時,用於加速前端頁面開發的 Web框架(MVC) 是關鍵。
  • 分佈式服務架構
    • 當垂直應用愈來愈多,應用之間交互不可避免,將核心業務抽取出來,做爲獨立的服務,逐漸造成穩定的服務中心,使前端應用能更快速的響應多變的市場需求。
    • 此時,用於提升業務複用及整合的 分佈式服務框架(RPC) 是關鍵。
  • 流動計算架構
    • 當服務愈來愈多,容量的評估,小服務資源的浪費等問題逐漸顯現,此時需增長一個調度中心基於訪問壓力實時管理集羣容量,提升集羣利用率。
    • 此時,用於提升機器利用率的 資源調度和治理中心(SOA) 是關鍵。

 

2.1  : Dubbo的架構


節點角色說明:redis

  • Provider: 暴露服務的服務提供方(dubbo)。
  • Consumer: 調用遠程服務的服務消費方(dubbo)。
  • Registry: 服務註冊與發現的註冊中心(zookeeper)。
  • Monitor: 統計服務的調用次調和調用時間的監控中心(dubbo-admin)。
  • Container: 服務運行容器。

 

 

調用關係說明:算法

  • 0. 服務容器負責啓動,加載,運行服務提供者。
  • 1. 服務提供者在啓動時,向註冊中心註冊本身提供的服務。
  • 2. 服務消費者在啓動時,向註冊中心訂閱本身所需的服務。
  • 3. 註冊中心返回服務提供者地址列表給消費者,若是有變動,註冊中心將基於長鏈接推送變動數據給消費者。
  • 4. 服務消費者,從提供者地址列表中,基於軟負載均衡算法,選一臺提供者進行調用,若是調用失敗,再選另外一臺調用。
  • 5. 服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計數據到監控中心。

 使用:spring

            Dubbo採用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置便可,Dubbo基於Spring的Schema擴展進行加載。

 3-1代碼實現:

 代碼下載:     https://pan.baidu.com/s/1_WXqOJPT0vuhz7g0HzlvyQ

 1:搭建maven結構:

floor-parent --   pom.xml的配置:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0"
  3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <modelVersion>4.0.0</modelVersion>
  6 
  7     <groupId>com.wfd360.com</groupId>
  8     <artifactId>floor-parent</artifactId>
  9     <version>1.0-SNAPSHOT</version>
 10     <modules>
 11         <module>floor-common</module>
 12         <module>floor-manager</module>
 13     </modules>
 14 
 15     <packaging>pom</packaging>
 16     <!-- 集中定義依賴版本號 -->
 17     <properties>
 18         <junit.version>4.12</junit.version>
 19         <spring.version>4.2.4.RELEASE</spring.version>
 20         <mybatis.version>3.2.8</mybatis.version>
 21         <mybatis.spring.version>1.2.2</mybatis.spring.version>
 22         <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
 23         <mysql.version>5.1.32</mysql.version>
 24         <slf4j.version>1.6.4</slf4j.version>
 25         <jackson.version>2.4.2</jackson.version>
 26         <druid.version>1.0.9</druid.version>
 27         <httpclient.version>4.3.5</httpclient.version>
 28         <jstl.version>1.2</jstl.version>
 29         <servlet-api.version>2.5</servlet-api.version>
 30         <jsp-api.version>2.0</jsp-api.version>
 31         <joda-time.version>2.5</joda-time.version>
 32         <commons-lang3.version>3.3.2</commons-lang3.version>
 33         <commons-io.version>1.3.2</commons-io.version>
 34         <commons-net.version>3.3</commons-net.version>
 35         <pagehelper.version>3.4.2-fix</pagehelper.version>
 36         <jsqlparser.version>0.9.1</jsqlparser.version>
 37         <commons-fileupload.version>1.3.1</commons-fileupload.version>
 38         <jedis.version>2.7.2</jedis.version>
 39         <solrj.version>4.10.3</solrj.version>
 40         <dubbo.version>2.5.3</dubbo.version>
 41         <zookeeper.version>3.4.7</zookeeper.version>
 42         <zkclient.version>0.1</zkclient.version>
 43         <activemq.version>5.11.2</activemq.version>
 44         <freemarker.version>2.3.23</freemarker.version>
 45         <quartz.version>2.2.2</quartz.version>
 46     </properties>
 47     <dependencyManagement>
 48         <dependencies>
 49             <!-- 時間操做組件 -->
 50             <dependency>
 51                 <groupId>joda-time</groupId>
 52                 <artifactId>joda-time</artifactId>
 53                 <version>${joda-time.version}</version>
 54             </dependency>
 55             <!-- Apache工具組件 -->
 56             <dependency>
 57                 <groupId>org.apache.commons</groupId>
 58                 <artifactId>commons-lang3</artifactId>
 59                 <version>${commons-lang3.version}</version>
 60             </dependency>
 61             <dependency>
 62                 <groupId>org.apache.commons</groupId>
 63                 <artifactId>commons-io</artifactId>
 64                 <version>${commons-io.version}</version>
 65             </dependency>
 66             <dependency>
 67                 <groupId>commons-net</groupId>
 68                 <artifactId>commons-net</artifactId>
 69                 <version>${commons-net.version}</version>
 70             </dependency>
 71             <!-- Jackson Json處理工具包 -->
 72             <dependency>
 73                 <groupId>com.fasterxml.jackson.core</groupId>
 74                 <artifactId>jackson-databind</artifactId>
 75                 <version>${jackson.version}</version>
 76             </dependency>
 77             <!-- httpclient -->
 78             <dependency>
 79                 <groupId>org.apache.httpcomponents</groupId>
 80                 <artifactId>httpclient</artifactId>
 81                 <version>${httpclient.version}</version>
 82             </dependency>
 83             <!-- quartz任務調度框架 -->
 84             <dependency>
 85                 <groupId>org.quartz-scheduler</groupId>
 86                 <artifactId>quartz</artifactId>
 87                 <version>${quartz.version}</version>
 88             </dependency>
 89             <!-- 單元測試 -->
 90             <dependency>
 91                 <groupId>junit</groupId>
 92                 <artifactId>junit</artifactId>
 93                 <version>${junit.version}</version>
 94                 <scope>test</scope>
 95             </dependency>
 96             <!-- 日誌處理 -->
 97             <dependency>
 98                 <groupId>org.slf4j</groupId>
 99                 <artifactId>slf4j-log4j12</artifactId>
100                 <version>${slf4j.version}</version>
101             </dependency>
102             <!-- Mybatis -->
103             <dependency>
104                 <groupId>org.mybatis</groupId>
105                 <artifactId>mybatis</artifactId>
106                 <version>${mybatis.version}</version>
107             </dependency>
108             <dependency>
109                 <groupId>org.mybatis</groupId>
110                 <artifactId>mybatis-spring</artifactId>
111                 <version>${mybatis.spring.version}</version>
112             </dependency>
113             <dependency>
114                 <groupId>com.github.miemiedev</groupId>
115                 <artifactId>mybatis-paginator</artifactId>
116                 <version>${mybatis.paginator.version}</version>
117             </dependency>
118             <dependency>
119                 <groupId>com.github.pagehelper</groupId>
120                 <artifactId>pagehelper</artifactId>
121                 <version>${pagehelper.version}</version>
122             </dependency>
123             <!-- MySql -->
124             <dependency>
125                 <groupId>mysql</groupId>
126                 <artifactId>mysql-connector-java</artifactId>
127                 <version>${mysql.version}</version>
128             </dependency>
129             <!-- 鏈接池 -->
130             <dependency>
131                 <groupId>com.alibaba</groupId>
132                 <artifactId>druid</artifactId>
133                 <version>${druid.version}</version>
134             </dependency>
135             <!-- Spring -->
136             <dependency>
137                 <groupId>org.springframework</groupId>
138                 <artifactId>spring-context</artifactId>
139                 <version>${spring.version}</version>
140             </dependency>
141             <dependency>
142                 <groupId>org.springframework</groupId>
143                 <artifactId>spring-beans</artifactId>
144                 <version>${spring.version}</version>
145             </dependency>
146             <dependency>
147                 <groupId>org.springframework</groupId>
148                 <artifactId>spring-webmvc</artifactId>
149                 <version>${spring.version}</version>
150             </dependency>
151             <dependency>
152                 <groupId>org.springframework</groupId>
153                 <artifactId>spring-jdbc</artifactId>
154                 <version>${spring.version}</version>
155             </dependency>
156             <dependency>
157                 <groupId>org.springframework</groupId>
158                 <artifactId>spring-aspects</artifactId>
159                 <version>${spring.version}</version>
160             </dependency>
161             <dependency>
162                 <groupId>org.springframework</groupId>
163                 <artifactId>spring-jms</artifactId>
164                 <version>${spring.version}</version>
165             </dependency>
166             <dependency>
167                 <groupId>org.springframework</groupId>
168                 <artifactId>spring-context-support</artifactId>
169                 <version>${spring.version}</version>
170             </dependency>
171             <!-- JSP相關 -->
172             <dependency>
173                 <groupId>jstl</groupId>
174                 <artifactId>jstl</artifactId>
175                 <version>${jstl.version}</version>
176             </dependency>
177             <dependency>
178                 <groupId>javax.servlet</groupId>
179                 <artifactId>servlet-api</artifactId>
180                 <version>${servlet-api.version}</version>
181                 <scope>provided</scope>
182             </dependency>
183             <dependency>
184                 <groupId>javax.servlet</groupId>
185                 <artifactId>jsp-api</artifactId>
186                 <version>${jsp-api.version}</version>
187                 <scope>provided</scope>
188             </dependency>
189             <!-- 文件上傳組件 -->
190             <dependency>
191                 <groupId>commons-fileupload</groupId>
192                 <artifactId>commons-fileupload</artifactId>
193                 <version>${commons-fileupload.version}</version>
194             </dependency>
195             <!-- Redis客戶端 -->
196             <dependency>
197                 <groupId>redis.clients</groupId>
198                 <artifactId>jedis</artifactId>
199                 <version>${jedis.version}</version>
200             </dependency>
201             <!-- solr客戶端 -->
202             <dependency>
203                 <groupId>org.apache.solr</groupId>
204                 <artifactId>solr-solrj</artifactId>
205                 <version>${solrj.version}</version>
206             </dependency>
207             <!-- dubbo相關 -->
208             <dependency>
209                 <groupId>com.alibaba</groupId>
210                 <artifactId>dubbo</artifactId>
211                 <version>${dubbo.version}</version>
212             </dependency>
213             <dependency>
214                 <groupId>org.apache.zookeeper</groupId>
215                 <artifactId>zookeeper</artifactId>
216                 <version>${zookeeper.version}</version>
217             </dependency>
218             <dependency>
219                 <groupId>com.github.sgroschupf</groupId>
220                 <artifactId>zkclient</artifactId>
221                 <version>${zkclient.version}</version>
222             </dependency>
223             <dependency>
224                 <groupId>org.apache.activemq</groupId>
225                 <artifactId>activemq-all</artifactId>
226                 <version>${activemq.version}</version>
227             </dependency>
228             <dependency>
229                 <groupId>org.freemarker</groupId>
230                 <artifactId>freemarker</artifactId>
231                 <version>${freemarker.version}</version>
232             </dependency>
233 
234         </dependencies>
235     </dependencyManagement>
236 
237     <build>
238         <finalName>${project.artifactId}</finalName>
239 
240         <plugins>
241             <!-- 資源文件拷貝插件 -->
242           <!--  <plugin>
243                 <groupId>org.apache.maven.plugins</groupId>
244                 <artifactId>maven-resources-plugin</artifactId>
245                 <version>2.7</version>
246                 <configuration>
247                     <encoding>UTF-8</encoding>
248                 </configuration>
249             </plugin>-->
250             <!-- java編譯插件 -->
251           <plugin>
252                 <groupId>org.apache.maven.plugins</groupId>
253                 <artifactId>maven-compiler-plugin</artifactId>
254                 <version>3.2</version>
255                 <configuration>
256                     <source>1.7</source>
257                     <target>1.7</target>
258                     <encoding>UTF-8</encoding>
259                 </configuration>
260             </plugin>
261         </plugins>
262        <!-- <pluginManagement>
263             <plugins>
264                 &lt;!&ndash; 配置Tomcat插件 &ndash;&gt;
265                 <plugin>
266                     <groupId>org.apache.tomcat.maven</groupId>
267                     <artifactId>tomcat7-maven-plugin</artifactId>
268                     <version>2.2</version>
269                 </plugin>
270             </plugins>
271         </pluginManagement>-->
272     </build>
273 
274 
275 </project>
View Code

建立須要發佈的接口:

 

 floor-Service :暴露接口:

 

接口暴露 配置文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 8     http://www.springframework.org/schema/context
 9     http://www.springframework.org/schema/context/spring-context-4.2.xsd
10       http://code.alibabatech.com/schema/dubbo
11       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
12 
13     <!-- 配置包掃描器 -->
14     <context:component-scan base-package="com.wfd360.service"/>
15     <!-- 使用dubbo發佈服務 -->
16     <!-- 提供方應用信息,用於計算依賴關係 -->
17     <dubbo:application name="floor-manager"/>
18     <dubbo:registry protocol="zookeeper"
19                     address="127.0.0.1:2181"/>
20     <!-- 用dubbo協議在20880端口暴露服務 -->
21     <dubbo:protocol name="dubbo" port="20880"/>
22     <!-- 聲明須要暴露的服務接口 -->
23     <dubbo:service interface="com.wfd360.service.ItemService" ref="itemServiceImpl" timeout="600000"/>
24     <dubbo:service interface="com.wfd360.service.ItemCatService" ref="itemCatServiceImpl" timeout="600000"/>
25 </beans>
View Code

 

floor-web :消費者調用被暴露的接口:

pom。xml配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>floor-parent</artifactId>
 7         <groupId>com.wfd360.com</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11      <packaging>war</packaging>
12     <artifactId>floor-web</artifactId>
13     <dependencies>
14         <dependency>
15             <groupId>com.wfd360.com</groupId>
16             <artifactId>floor-interface</artifactId>
17             <version>1.0-SNAPSHOT</version>
18         </dependency>
19         <!-- Spring -->
20         <dependency>
21             <groupId>org.springframework</groupId>
22             <artifactId>spring-context</artifactId>
23         </dependency>
24         <dependency>
25             <groupId>org.springframework</groupId>
26             <artifactId>spring-beans</artifactId>
27         </dependency>
28         <dependency>
29             <groupId>org.springframework</groupId>
30             <artifactId>spring-webmvc</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework</groupId>
34             <artifactId>spring-jdbc</artifactId>
35         </dependency>
36         <dependency>
37             <groupId>org.springframework</groupId>
38             <artifactId>spring-aspects</artifactId>
39         </dependency>
40         <dependency>
41             <groupId>org.springframework</groupId>
42             <artifactId>spring-jms</artifactId>
43         </dependency>
44         <dependency>
45             <groupId>org.springframework</groupId>
46             <artifactId>spring-context-support</artifactId>
47         </dependency>
48         <!-- JSP相關 -->
49         <dependency>
50             <groupId>jstl</groupId>
51             <artifactId>jstl</artifactId>
52         </dependency>
53         <dependency>
54             <groupId>javax.servlet</groupId>
55             <artifactId>servlet-api</artifactId>
56             <scope>provided</scope>
57         </dependency>
58         <dependency>
59             <groupId>javax.servlet</groupId>
60             <artifactId>jsp-api</artifactId>
61             <scope>provided</scope>
62         </dependency>
63 
64         <!-- dubbo相關 -->
65         <dependency>
66             <groupId>com.alibaba</groupId>
67             <artifactId>dubbo</artifactId>
68             <exclusions>
69                 <exclusion>
70                     <groupId>org.springframework</groupId>
71                     <artifactId>spring</artifactId>
72                 </exclusion>
73                 <exclusion>
74                     <groupId>org.jboss.netty</groupId>
75                     <artifactId>netty</artifactId>
76                 </exclusion>
77             </exclusions>
78         </dependency>
79         <dependency>
80             <groupId>org.apache.zookeeper</groupId>
81             <artifactId>zookeeper</artifactId>
82         </dependency>
83         <dependency>
84             <groupId>com.github.sgroschupf</groupId>
85             <artifactId>zkclient</artifactId>
86         </dependency>
87 
88     </dependencies>
89 
90 </project>
View Code

 

spring -mvc 配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 9 
10     <context:component-scan base-package="com.wfd360.controller" />
11     <mvc:annotation-driven />
12     <bean
13         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14         <property name="prefix" value="/WEB-INF/jsp/" />
15         <property name="suffix" value=".jsp" />
16     </bean>
17     <!--本地靜態文件-->
18     <mvc:resources mapping="/css/**" location="/css/"/>
19     <mvc:resources mapping="/js/**" location="/js/"/>
20 
21     <!-- 引用dubbo服務 -->
22     <dubbo:application name="floor-web"/>
23     <dubbo:registry protocol="zookeeper" address="192.168.139.128:2181"/>
24     <dubbo:reference interface="com.wfd360.service.ItemService" id="itemService" />
25     <dubbo:reference interface="com.wfd360.service.ItemCatService" id="itemCatService" />
26 </beans>
View Code

 

 

注意:服務的發佈和消費都須要保證zookeeper處於開啓的狀態。

相關文章
相關標籤/搜索