商城專案-dubbo,框架整合,dubbo釋出和引用服務
DUBBO:資源排程和治理中心的管理工具
節點角色說明:
· Provider:暴露服務的服務提供方。
· Consumer:呼叫遠端服務的服務消費方。
· Registry:服務註冊與發現的註冊中心。
· Monitor:統計服務的呼叫次調和呼叫時間的監控中心。
· Container:服務執行容器。
呼叫關係說明:
· 0.服務容器負責啟動,載入,執行服務提供者。
· 1.服務提供者在啟動時,向註冊中心註冊自己提供的服務。
· 2.服務消費者在啟動時,向註冊中心訂閱自己所需的服務。
· 3.
· 4.服務消費者,從提供者地址列表中,基於軟負載均衡演算法,選一臺提供者進行呼叫,如果呼叫失敗,再選另一臺呼叫。
· 5.服務消費者和提供者,在記憶體中累計呼叫次數和呼叫時間,定時每分鐘傳送一次統計資料到監控中心。
註冊中心
註冊中心負責服務地址的註冊與查詢,相當於目錄服務,服務提供者和消費者只在啟動時與註冊中心互動,註冊中心不轉發請求,壓力較小。使用dubbo-2.3.3以上版本,建議使用zookeeper註冊中心。
Zookeeper是Apacahe Hadoop的子專案,是一個樹型的目錄服務,支援變更推送,適合作為Dubbo服務的註冊中心,工業強度較高,可用於生產環境,並推薦使用
Zookeeper的安裝:
第一步:安裝jdk
第二步:解壓縮zookeeper壓縮包
第三步:將conf資料夾下zoo_sample.cfg複製一份,改名為zoo.cfg
第四步:修改配置dataDir屬性,指定一個真實目錄
第五步:
啟動zookeeper:bin/zkServer.sh start
關閉zookeeper:bin/zkServer.sh stop
檢視zookeeper狀態:bin/zkServer.sh status
框架整合
1.1. 新增dubbo的依賴
加入dubbo相關的jar包。服務層、表現層都新增。
<!-- dubbo相關 -->
<
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<!-- 排除依賴-->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>1.1. 整合思路
1、Dao層:
mybatis整合spring,通過spring管理SqlSessionFactory、mapper代理物件。需要mybatis和spring的整合包。
2、Service層:
所有的service實現類都放到spring容器中管理。由spring建立資料庫連線池,並有spring管理實務。釋出dubbo服務
3、表現層:
Springmvc框架,由springmvc管理controller。引用dubbo服務
1.1. Dao整合
1.1.1. 建立SqlMapConfig.xml配置檔案
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEconfiguration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration> |
1.1.1. Spring整合mybatis
建立applicationContext-dao.xml
<beansxmlns="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: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://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!--資料庫連線池 --> <!--載入配置檔案 --> <context:property-placeholderlocation="classpath:properties/*.properties"/> <!--資料庫連線池 --> <beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url"value="${jdbc.url}"/> <property name="username"value="${jdbc.username}"/> <property name="password"value="${jdbc.password}"/> <property name="driverClassName"value="${jdbc.driver}"/> <property name="maxActive"value="10"/> <property name="minIdle"value="5"/> </bean> <!--讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 資料庫連線池 交由sqlSessionFactoryBean管理--> <property name="dataSource"ref="dataSource"/> <!-- 載入mybatis的全域性配置檔案 交由sqlSessionFactoryBean管理--> <property name="configLocation"value="classpath:mybatis/SqlMapConfig.xml"/> </bean> <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">Spring管理 <property name="basePackage"value="com.taotao.mapper"/> </bean> </beans> |
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8 jdbc.username=root jdbc.password=root |
備註:
Druid是目前最好的資料庫連線池,在功能、效能、擴充套件性方面,都超過其他資料庫連線池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。
Druid已經在阿里巴巴部署了超過600個應用,經過多年多生產環境大規模部署的嚴苛考驗。
1.2. Service整合
1.2.1. 管理Service
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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-scanbase-package="com.taotao.service"></context:component-scan> <!-- 使用dubbo釋出服務 --> <!-- 提供方應用資訊,用於計算依賴關係 --> <dubbo:applicationname="taotao-manager"/> <dubbo:registry protocol="zookeeper" address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183" /> <!-- 用dubbo協議在20880埠暴露服務 --> <dubbo:protocolname="dubbo"port="20880"/> <!-- 宣告需要暴露的服務介面 --> <dubbo:serviceinterface="com.taotao.service.ItemService"ref="itemServiceImpl"/> </beans> |
事務管理
建立applicationContext-trans.xml
<beansxmlns="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: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://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!--事務管理器 --> <beanid="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 資料來源 --> <property name="dataSource"ref="dataSource"/> </bean> <!--通知 --> <tx:adviceid="txAdvice"transaction-manager="transactionManager"> <tx:attributes> <!-- 傳播行為 --> <tx:method name="save*"propagation="REQUIRED"/> <tx:method name="insert*"propagation="REQUIRED"/> <tx:method name="add*"propagation="REQUIRED"/> <tx:method name="create*"propagation="REQUIRED"/> <tx:method name="delete*"propagation="REQUIRED"/> <tx:method name="update*"propagation="REQUIRED"/> <tx:method name="find*"propagation="SUPPORTS"read-only="true"/> <tx:method name="select*"propagation="SUPPORTS"read-only="true"/> <tx:method name="get*"propagation="SUPPORTS"read-only="true"/> </tx:attributes> </tx:advice> <!--切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.taotao.service.*.*(..))"/> </aop:config> </beans> |
Web.xml
<?xmlversion="1.0"encoding="UTF-8"?> <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"version="2.5"> <display-name>taotao-manager</display-name> <!--載入spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> |
表現層整合
Springmvc.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scanbase-package="com.taotao.controller"/> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"value="/WEB-INF/jsp/"/> <property name="suffix"value=".jsp"/> </bean> <!-- 引用dubbo服務 --> <dubbo:applicationname="taotao-manager-web"/> <dubbo:registry protocol="zookeeper" address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"/> <dubbo:referenceinterface="com.taotao.service.ItemService"id="itemService"/> </beans> |
web.xml
<?xmlversion="1.0"encoding="UTF-8"?> <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"version="2.5"> <display-name>taotao-manager-web</display-name> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> <!--解決post亂碼 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--springmvc的前端控制器 --> <servlet> <servlet-name>taotao-manager</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation,springmvc的配置檔案預設在:WEB-INF/servlet的name+"-servlet.xml" --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>taotao-manager</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> |