dubbo概述
Dubbo 是一個分佈式服務框架,阿里巴巴開源項目,被國內電商及互聯網項目中使用。
Dubbo 致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。簡單的說,Dubbo就是個服務框架,如果沒有分佈式的需求,其實是不需要用的,只有在分佈式的時候,纔有Dubbo這樣的分佈式服務框架的需求,並且本質上是服務調用,是一個遠程服務調用的分佈式框架。
節點角色說明:
• Provider: 暴露服務的服務提供方。
• Consumer: 調用遠程服務的服務消費方。
• Registry: 服務註冊與發現的註冊中心。
• Monitor: 統計服務的調用次調和調用時間的監控中心。
• Container: 服務運行容器。
調用關係說明:
• 0. 服務容器負責啓動,加載,運行服務提供者。
• 1. 服務提供者在啓動時,向註冊中心註冊自己提供的服務。
• 2. 服務消費者在啓動時,向註冊中心訂閱自己所需的服務。
• 3. 註冊中心返回服務提供者地址列表給消費者,如果有變更,註冊中心將基於長連接推
送變更數據給消費者。
• 4. 服務消費者,從提供者地址列表中,基於軟負載均衡算法,選一臺提供者進行調用,
如果調用失敗,再選另一臺調用。
• 5. 服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計
數據到監控中心。
Zookeeper 介紹
官方推薦使用 zookeeper 註冊中心。註冊中心負責服務地址的註冊與查找,相當於目錄服務,服務提供者和消費者只在啓動時與註冊中心交互,註冊中心不轉發請求,壓力較小。
Zookeeper 是 Apacahe Hadoop 的子項目,是一個樹型的目錄服務,支持變更推送,適合作爲Dubbo 服務的註冊中心,工業強度較高,可用於生產環境。
idea搭建項目流程
創建如上圖所示的項目結構目錄,其中record_manager_web和record_service爲web項目,父工程爲pom工程,其他項目默認爲jar工程
在父工程的pom文件中添加依賴信息和版本號
數據訪問模塊(record_dao)的依賴
對於自己創建的項目只依賴pojo
通用實體類模塊(pojo)不需要依賴信息
服務接口模塊僅需要依賴pojo
服務模塊需要依賴自己的有common、dao和interface其中pojo已經被dao依賴,所以不需要重複依賴
manager_web模塊需要依賴自己項目的common和interface,服務(service)通過遠程調用實現注入
common項目只需要依賴本項目所需的依賴
詳細依賴信息見詳細依賴
配置dubbo遠程調用相關
在類上加註解都是基於dubbo的註解
dao項目的配置
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/youlexuandb?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
log4j.properties配置
# global level
log4j.rootLogger = error,stdout
#MyBatis logging configuration
# self package level
log4j.logger.com.offcn=debug
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
# standard ouput for black, System.err is red
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%d{HH\:mm\:ss} %5p %c - %m%n
my-batis.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 設置數據庫方言類型:oracle, mysql -->
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
spring-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 數據庫連接池 -->
<bean id="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>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-cfg.xml" />
<property name="mapperLocations" value="classpath:com/weilinyang/mapper/*Mapper.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.weilinyang.mapper" />
</bean>
</beans>
service服務項目的配置
web.xml配置:在classpath要加*
classpath後邊的*表示將本項目中依賴項目的以spring-開頭的xml文件進行加載,如service依賴了dao,就會把dao項目中的spring-dao.xml加載進去
spring-service-application.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--掃描包-->
<context:component-scan base-package="com.weilinyang"/>
<!--配置事務-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="crudAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="list*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.weilinyang.service..*.*(..))"
id="pointcut" />
<aop:advisor advice-ref="crudAdvice" pointcut-ref="pointcut" />
</aop:config>
</beans>
spring-service-dubbo.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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:protocol name="dubbo" port="20881"/>
<!-- 提供方應用信息,用於計算依賴關係 -->
<dubbo:application name="record_service" />
<!-- 向zk註冊服務 -->
<dubbo:registry address="zookeeper://192.168.2.123:2181" />
<!-- 掃描帶service註解的類 -->
<dubbo:annotation package="com.weilinyang.service.impl" />
</beans>
manager_web服務項目的配置
manager_web項目是web服務,主要對外訪問不涉及業務實現,不用配置application.xml,僅需配置spring-mvc.xml
web.xml的配置:classpath後不加*,web項目不依賴本地服務,是通過dubbo遠程調用
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 避免spring查找WEB-INF/applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value><!--classpath後不加*,web項目不依賴本地服務,是通過dubbo遠程調用-->
</context-param>
<!-- springMVC攔截 -->
<servlet>
<servlet-name>spMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- springMVC編碼過濾 -->
<filter>
<filter-name>charset</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>charset</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
spring-dubbo.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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:protocol name="dubbo" port="20881"/>
<!-- 提供方應用信息,用於計算依賴關係 -->
<dubbo:application name="record_manager_web" />
<!-- 向zk註冊服務 -->
<dubbo:registry address="zookeeper://192.168.2.123:2181" />
<!-- 掃描帶controller註解的類 -->
<dubbo:annotation package="com.weilinyang.manager.controller" />
</beans>
spring-mvc.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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 註解包掃描 -->
<context:component-scan base-package="com.weilinyang" />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="features">
<array>
<value>WriteMapNullValue</value>
<value>WriteDateUseDateFormat</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 靜態資源處理 -->
<mvc:default-servlet-handler />
</beans>