dubbo的註解配置問題:dubbo的服務端service註解和spring的service、有衝突

最近在學習,dubbo。看了dubbo的xml配置後,表示好麻煩,服務端和消費端都要寫一遍:類似下面的配置:(我用的spring 4.1.3.RELEASE 和dubbo2.5.3

<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-4.0.xsd 
      http://code.alibabatech.com/schema/dubbo
      http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
       
    <!-- 服務提供方 -->
    <!-- 第一:服務提供方啓名稱  計算機要用名稱 -->
    <dubbo:application name="service-product"/>
    <!-- 第二:到註冊中心註冊地址  連接zookeeper -->
<!--    <dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->
    <!--<dubbo:registry address="192.168.72.130:2181" protocol="zookeeper"/>-->
   <!--開發時優化,讓消費者直接連接服務提供方。 生產環境要放開這段用上面的-->
   <dubbo:registry address="N/A"/>
    <!-- 第三:自定義端口號   默認端口是20880-->
    <dubbo:protocol host="127.0.0.1" port="20880"/>
    <!-- 第四:指定暴露的接口  -->
    <dubbo:service interface="cn.mytest.core.service.TestTbService" ref="testTbService"/>

</beans>


<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-4.0.xsd 
		
		http://code.alibabatech.com/schema/dubbo        
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
		
		
	 <!-- 服務消息方 -->
	 <!-- 第一:服務消費方啓名稱  計算機要用名稱 -->
	 <dubbo:application name="mytest-console"/>
	 <!-- 第二:到註冊中心註冊地址  連接zookeeper -->
<!-- 	 <dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->
	 <!--<dubbo:registry address="192.168.72.130:2181" protocol="zookeeper"/>-->


	 <!-- 第三:調用接口  -->
	 <!--<dubbo:reference interface="cn.mytest.core.service.TestTbService" id="testTbService"/>-->

	<!--全局設置超時時間 10分鐘-->
	 <dubbo:consumer timeout="600000"/>

	<!--開發時優化,讓消費者直接連接服務提供方。 生產環境要放開這段用上面的-->
	<dubbo:registry address="N/A" />
	<dubbo:protocol port="20080"/>

	<!--check=false 服務消費方不檢查服務提供方-->
	<dubbo:reference interface="cn.mytest.core.service.TestTbService" id="testTbService"
					 url="dubbo://127.0.0.1:20880" check="false"/>

</beans>
只有幾個服務就還好,如果服務很多,估計配置文件會特別大,也不好維護。

所以想看看註解的寫法:根據官方的教程:https://dubbo.gitbooks.io/dubbo-user-book/configuration/annotation.html
學着做了個。結果踩了很多坑,這裏記錄一下

1.服務端的把上面 
 <dubbo:service interface="cn.mytest.core.service.TestTbService" ref="testTbService"/>
替換成:
<!-- 掃描註解包路徑,多個包用逗號分隔,不填pacakge表示當前ApplicationContext中所有的類 -->
<dubbo:annotation package="cn.mytest.core.service"/>
然後在service的實現類名頭上加:註解 @com.alibaba.dubbo.config.annotation.Service
最終結果如下:
import cn.itcast.core.dao.TestTbDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.core.pojo.TestTb;

/**
 * 測試事務
 */
@Component("testTbService")
@com.alibaba.dubbo.config.annotation.Service
@Transactional
public class TestTbServiceImpl implements TestTbService {

   @Autowired
   private TestTbDao testTbDao;
   @Override
   public void insertTestTb(TestTb testTb) {
      testTbDao.insertTestTb(testTb);
//    throw new RuntimeException();
   }
}



這裏注意如果 你用的是spring的 service註解,就很容易掃包就掃不出來,服務端根本沒有註冊這個 service。
目前我踩過的坑有如下幾種 dubbo不能正常註冊service。
(1).用了spring的service註解:放在 dubbo的service註解上面時:     (後來再測試一遍,有莫名奇妙的可以了??)
@org.springframework.stereotype.Service("testTbService")
@com.alibaba.dubbo.config.annotation.Service
public class TestTbServiceImpl implements TestTbService {..}

(2).同時用了spring的事務註解 和serveice註解,這時什麼順序都不管用:。(後來再測試一遍,有莫名奇妙的可以了??)

@org.springframework.stereotype.Service("testTbService")
@com.alibaba.dubbo.config.annotation.Service
@Transactional
public class TestTbServiceImpl implements TestTbService {..}

後來發現 用spring的 @Component註解 時這種奇怪的問題纔得到解決。
如下:
@Component("testTbService")
@org.springframework.stereotype.Service("testTbService")
@com.alibaba.dubbo.config.annotation.Service
@Transactional
public class TestTbServiceImpl implements TestTbService {..}


2.消費端的註解配置: (ps我目前用的這個方法來配置消費端,service注入不進去,一直報service的空指針? 還是測試不通過,所以目前還是用的xml配置的方式 orz..)
把上面的
<dubbo:reference interface="cn.mytest.core.service.product.BrandService" id="brandService"
             url="dubbo://127.0.0.1:20880" check="false"/>
替換成:

<!-- 掃描註解包路徑,多個包用逗號分隔,不填pacakge表示掃描當前ApplicationContext中所有的類 -->
<!-- 代替上 掃描註解包路徑,多個包用逗號分隔,不填pacakge表示當前ApplicationContext中所有的類 -->
<dubbo:annotation package="cn.mytest.core.*"/>

然後調用時在  注入的service 成員變量頭上  加上: @com.alibaba.dubbo.config.annotation.Reference

這裏需要注意的有四點:
1. 此處應該掃控制器了,而不是service。
2. dubbo的掃包package的寫法,他不像 spring裏的掃包是  base-package,所以寫完後要加上*,表示這個包裏所有的內容。要不然就只掃這個包了。。
3. 一定要讓dubbo的掃包在 spring的前面,要不然 會報空指針的錯,因爲spring先掃了控制器之後,dubbo就再也注入不進去任何值了。
4. spring的 那個自動裝載註解就不需要了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章