將專案拆分搭建dubbo專案(二)dubbo專案搭建例項與應用
dubbo專案的配置
根據上一篇文章,我們已經建立好兩專案一個xxx-common一個xxx-service並建立好相應的目錄結構
開始配置配置檔案內容
一、配置pom.xml 檔案,
(1)payment-common的pom.xml檔案配置
配置上使用的父專案或者建立上jar包依賴即可
(2)payment-service的pom.xml檔案配置
配置上除了配置父專案以外還要配置對payment-common專案的依賴因為service要使用到common中的實體和介面工具類等。還有很重要的一個配置打包
<?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.dxl.payment</groupId> <artifactId>payment-service</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.uz.dxt</groupId> <artifactId>manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>com.dxl.payment</groupId> <artifactId>payment-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.dxl.util</groupId> <artifactId>common-util</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build><!--打包配置 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.dxl.payment.main.Application</mainClass><!--配置啟動專案的類--> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
二、配置payment-service專案的resources中的配置檔案
一般的ssm專案配置檔案
1、配置applicationContext.xml 檔案
特殊地方引入applicationContext_dubbo_zookeeper.xml
<import resource="applicationContext_dubbo_zookeeper.xml" />
2、配置jdbc.properties
3、mybatis.xml
4、log4j.properties
5、spring-mybatis-transaction.xml
dubbo atomikos分散式資料庫事務元件相關配置檔案 在applicationContext.xml中引用
1 applicationContext_dubbo_zookeeper.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:application name="Adminuser" /> <!-- 使用zookeeper註冊中心暴露服務地址 --> <!-- 測試 --> <dubbo:registry protocol="zookeeper" address="192.168.10.14:2181,192.168.10.17:2181,192.168.10.18:2181" /> <!--<dubbo:registry protocol="zookeeper" address="192.168.10.4:2181" />--> <!-- 生產 --> <!--<dubbo:registry protocol="zookeeper" address="10.27.78.162:2181,10.25.140.22:2181,10.27.81.154:2181" />--> <!-- 用dubbo協議在20880埠暴露服務 --> <dubbo:protocol name="dubbo" port="20891" /> <!-- 掃描註解包路徑,多個包用逗號分隔,不填pacakge表示掃描當前ApplicationContext中所有的類 暴露給外部使用的介面--> <!-- <dubbo:annotation package="com.call.service.Impl"/> --> <dubbo:service interface="com.dxl.payment.service.payment.TRepaymentService" ref="tRepaymentService" timeout="300000"/> </beans>
<dubbo:service interface="com.dxl.payment.service.payment.TRepaymentService" ref="tRepaymentService" timeout="300000"/>
這句話是代表暴露給外部的介面。interface是payment-common專案中定義的介面
ref是payment-service專案介面實現類的的名稱在@Service("tRepaymentService")中宣告的名稱
示例:
payment-common的介面
package com.dxl.payment.service.payment;
import com.dxl.payment.model.payment.TRepayment;
import com.dxl.payment.service.base.BaseService;
import java.util.List;
public interface TRepaymentService extends BaseService<TRepayment> {
public List<TRepayment> selectAllRepayment(TRepayment tRepayment);
}
payment-service的介面實現類
package com.dxl.payment.service.payment.impl;
import com.dxl.payment.dao.payment.TRepaymentMapper;
import com.dxl.payment.model.payment.TRepayment;
import com.dxl.payment.service.TipsService;
import com.dxl.payment.service.base.impl.BaseServiceImpl;
import com.dxl.payment.service.payment.TRepaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;
@Service("tRepaymentService")
public class TRepaymentServiceImpl extends BaseServiceImpl<TRepayment>
implements TRepaymentService, TipsService<TRepayment> {
@Autowired
private TRepaymentMapper tRepaymentMapper;
private Format fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public boolean setTips(TRepayment t) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeTips(TRepayment t) {
// TODO Auto-generated method stub
return false;
}
@Override
public List<TRepayment> selectAllRepayment(TRepayment tRepayment) {
return tRepaymentMapper.selectAllRepayment(tRepayment);
}
}
dao層與sql正常寫即可
在applicationContext_dubbo_zookeeper.xml中暴露出介面資訊
<dubbo:service interface="com.dxl.payment.service.payment.TRepaymentService" ref="tRepaymentService" timeout="300000"/>
payment-common與payment-service相當於生產者。而其他web專案呼叫相當於消費者
使用dubbo專案的方法:
1首先必須確認dubbo專案已經暴露好介面
2在要呼叫dubbo專案的專案中配置spring-dubbo.xml
這裡演示在其他專案的serivce中呼叫dubbo,由於web本身就對自身專案的service具有依賴關係所以此處將dubbo的配置檔案放在service中。如果不想放在service可以直接在web中配置spring-dubbo.xml檔案
spring-dubbo.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="callphone-service123123"/>
<!--<dubbo:registry protocol="zookeeper" address="192.168.10.14:2181,192.168.10.17:2181,192.168.10.18:2181" check="false"/>-->
<!--<dubbo:registry protocol="zookeeper" address="192.168.10.4:2181" check="false"/>-->
<!-- <dubbo:registry protocol="zookeeper" address="192.168.10.14:2181,192.168.10.17:2181,192.168.10.18:2181" check="false"/> -->
<!--<dubbo:registry protocol="zookeeper" address="192.168.10.4:2181" check="false"/> -->
<!-- 測試 -->
<!--<dubbo:registry protocol="zookeeper" address="192.168.10.4:2181" check="false"/>-->
<dubbo:registry protocol="zookeeper" address="192.168.10.14:2181,192.168.10.17:2181,192.168.10.18:2181" check="false"/>
<!--<!– 生產 –>-->
<!--<dubbo:registry protocol="zookeeper" address="10.27.78.162:2181,10.25.140.22:2181,10.27.81.154:2181" check="false"/>-->
<!-- 要獲取到的dubbo暴露的介面配置-->
<dubbo:reference id="tRepaymentService" interface="com.dxl.payment.service.payment.TRepaymentService" check="false"/>
</beans>
配置好之後,將payment-common專案編譯成jar包,並新增到要使用的專案中示例此處將jar包新增到service專案中和web專案張
測試時可直接加入專案,如果要釋出生產,需要將dubbo的payment-common專案jar包放到私庫中引用,這樣就確保了其他專案只能檢視到common中暴露的介面而看不到具體的實現編碼,從而使專案和邏輯分離。獨立維護。
spring-dubbo.xml中配置的
<dubbo:reference id="tRepaymentService" interface="com.dxl.payment.service.payment.TRepaymentService" check="false"/>
是dubbo的消費者。配置此處可以使呼叫的payment-service使用到jdbc資料庫等。
3測試
編寫controller方法
@RequestMapping("/testPaymentDubbo")
@ResponseBody
public void testPaymentDubbo(){
TRepayment tRepayment=new TRepayment();
tRepayment.setCompanyCode("ceshilvsuo");
List<TRepayment> list=tRepaymentService.selectAllRepayment(tRepayment);
for (TRepayment t:list) {
System.out.println(t.toString());
}
}
自己測試時手動啟動payment-service中的main層的application類的main方法啟動dubbo生產者服務
注意執行時要保證payment-service專案和payment-common專案都編譯install一遍之後
dubbo生產者已經啟動了
可以用postman直接測試介面是否可用,dubug檢視執行。
測試通過即成功完成