因爲本項目用的是soa架構,所以必須須要兩個系統之間進行通訊,目前的解決辦法有三種(本人認爲)css
Dubbo是阿里巴巴公司開源的一個高性能優秀的服務框架,使得應用可經過高性能的 RPC 實現服務的輸出和輸入功能,能夠和 Spring框架無縫集成。
Dubbo是一款高性能、輕量級的開源Java RPC框架,它提供了三大核心能力:面向接口的遠程方法調用,智能容錯和負載均衡,以及服務自動註冊和發現。
重要的是能夠和spring無縫集成,也就是說在spring裏能夠不添加任何jar包就可使用dubbo了。
下面這個圖就是dubbo官網上的圖
節點角色說明:
Provider: 暴露服務的服務提供方。
Consumer: 調用遠程服務的服務消費方。
Registry: 服務註冊與發現的註冊中心。
Monitor: 統計服務的調用次調和調用時間的監控中心。
Container: 服務運行容器。
在本項目中
provider服務的提供方是咱們的service
consumer服務的消費方爲controller層,也就是web層。java
既然說到dubbo了,那麼service層暴露了本身的接口,Web層想要鏈接這個接口該如何實現呢?
首先咱們確定須要在配置文件中引入interface層,讓該接口加載到spring容器中(IOC),這樣方便咱們使用。服務層和Web層的都有了,那咱們該如何把他們鏈接起來呢,在這裏dubbo官網推薦使用zookeeper,(至關於一箇中介,服務層至關於房主,我這裏有房,須要去中介那裏說一聲在什麼位置,而Web層至關於租客,租客想要租房去中介瞭解位置,而後就能夠準確的到達房主的屋子。)mysql
動物園裏有好多的動物,遊客能夠根據動物園管理員提供的嚮導圖到不一樣的場館觀賞各類類型的動物,而不是像走在原始叢林裏,心驚膽顫的被動物所觀賞。爲了讓各類不一樣的動物呆在它們應該呆的地方,而不是相互串門,或是相互廝殺,就須要動物園管理員按照動物的各類習性加以分類和管理,這樣咱們才能更加放心安全的觀賞動物。
其實zookeeper的功能不少,可是這裏咱們只用到了它的分佈式系統的功能。linux
安裝環境:
Linux:centos6.4
Jdk:1.7以上版本
安裝前提條件:Zookeeper是java開發的能夠運行在windows、linux環境。須要先安裝jdk。
安裝步驟:
第一步:安裝jdk
第二步:把zookeeper的壓縮包上傳到linux系統。
第三步:解壓縮壓縮包
tar -zxvf zookeeper-3.4.6.tar.gz
第四步:進入zookeeper-3.4.6目錄,建立data文件夾。
第五步:把zoo_sample.cfg更名爲zoo.cfg
[root@localhost conf]# mv zoo_sample.cfg zoo.cfg
第六步:修改data屬性:dataDir=/root/zookeeper-3.4.6/data
第七步:啓動zookeeper
[root@localhost bin]# ./zkServer.sh start
關閉:[root@localhost bin]# ./zkServer.sh stop
查看狀態:[root@localhost bin]# ./zkServer.sh statusgit
注意:須要關閉防火牆。
service iptables stop
永久關閉修改配置開機不啓動防火牆:
chkconfig iptables off
若是不能成功啓動zookeeper,須要刪除data目錄下的zookeeper_server.pid文件。github
服務層的resource目錄下application-service.xml
這裏我把zookeeper安裝到虛擬機(192.168.25.110)上了web
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <context:component-scan base-package="cn.tsu.e3mall.service"/> <!-- 使用dubbo發佈服務 --> <!-- 提供方應用信息,用於計算依賴關係 --> <dubbo:application name="e3mall-manger" /> <dubbo:registry protocol="zookeeper" address="192.168.25.110:2181" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="cn.tsu.e3mall.service.ItemService" ref="itemServiceImpl" /> <dubbo:service interface="cn.tsu.e3mall.service.ItemCatService" ref="itemCatServiceImpl" /> </beans>
這裏我建立了兩個service服務接口,ref=「XXXXImpl」指的是該服務的實現類。
都是留controller層調用的。而XXXXImpl是經過component-scan(<context:component-scan base-package=「cn.tsu.e3mall.service」/>)進行掃描自動註冊到spring容器中。spring
<!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="cn.tsu.e3mall.service.ItemService" ref="itemServiceImpl" /> <dubbo:service interface="cn.tsu.e3mall.service.ItemCatService" ref="itemCatServiceImpl" />
Web層的spring下的springmvc.xmlsql
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!-- 掃描自動註冊cn.tsu.e3mall.controller下的包 --> <context:component-scan base-package="cn.tsu.e3mall.controller" /> <!-- 加載classpath目錄下client.properties配置文件 --> <context:property-placeholder location="classpath:client.properties"/> <!-- 定義文件上傳解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設定默認編碼 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 設定文件上傳的最大值5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> </bean> <mvc:annotation-driven /> <!-- 對靜態資源放行 --> <!-- <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/js/" mapping="/js/**" /> --> <mvc:default-servlet-handler/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 引用dubbo服務 --> <dubbo:application name="e3mall-manger-web"/> <dubbo:registry protocol="zookeeper" address="192.168.25.110:2181"/> <dubbo:reference interface="cn.tsu.e3mall.service.ItemService" id="itemService" timeout="300000"/> <dubbo:reference interface="cn.tsu.e3mall.service.ItemCatService" id="itemCatService" timeout="300000"/> <dubbo:reference interface="cn.tsu.content.e3mall.service.ContentCatService" id="contentCatService" timeout="300000"/> <dubbo:reference interface="cn.tsu.content.e3mall.service.ContentService" id="contentService" timeout="300000"/> <dubbo:reference interface="cn.tsu.search.e3mall.service.SearchService" id="searchService" timeout="300000"/> </beans>
項目中的代碼以下:數據庫
@Override public EasyUiDateGirdResult findTbItems(Integer page, Integer rows) { //獲取第page頁,rows條內容 PageHelper.startPage(page, rows); //緊跟着的第一個select方法會被分頁 TbItemExample example = new TbItemExample(); List<TbItem> list = TbItemMapper.selectByExample(example); //PageInfo對象裏包含了list的各類屬性進行賦值到pageinfo中 PageInfo<TbItem> pageInfo = new PageInfo<>(list); //建立返回對象並賦值 EasyUiDateGirdResult easy = new EasyUiDateGirdResult(); easy.setTotal(pageInfo.getTotal()); easy.setRows(list); return easy; }
第一步:在Mybatis配置xml中配置攔截器插件: <plugins> <!-- com.github.pagehelper爲PageHelper類所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫--> <property name="dialect" value="mysql"/> </plugin> </plugins> 第二步:在代碼中使用 一、設置分頁信息: //獲取第1頁,10條內容,默認查詢總數count PageHelper.startPage(1, 10); //緊跟着的第一個select方法會被分頁 List<Country> list = countryMapper.selectIf(1); 二、取分頁信息 //分頁後,實際返回的結果list類型是Page<E>,若是想取出分頁信息,須要強制轉換爲Page<E>, Page<Country> listCountry = (Page<Country>)list; listCountry.getTotal(); 三、取分頁信息的第二種方法 //獲取第1頁,10條內容,默認查詢總數count PageHelper.startPage(1, 10); List<Country> list = countryMapper.selectAll(); //用PageInfo對結果進行包裝 PageInfo page = new PageInfo(list);
項目中代碼用的是第二種方法
clean install -DskipTests