天天看點

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

1.引言

最近在學習,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 inter 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 inter id="testTbService"/>-->

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

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

	<!--check=false 服務消費方不檢查服務提供方-->
	<dubbo:reference inter id="testTbService"
					 url="dubbo://127.0.0.1:20880" check="false"/>
</beans>
           

隻有幾個服務就還好,如果服務很多,估計配置檔案會特别大,也不好維護。

是以想看看注解的寫法:根據官方的教程:https://dubbo.gitbooks.io/dubbo-user-book/configuration/annotation.html

學着做了個。結果踩了很多坑,這裡記錄一下

2.配置替換為注解

  • 1.服務端的把上面
<dubbo:service inter 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。

3.踩坑記錄

目前我踩過的坑有如下幾種 dubbo不能正常注冊service。

(1).用了spring的service注解:放在 dubbo的service注解上面時: (後來再測試一遍,有莫名奇妙的可以了??)

(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…)

把上面的

替換成:

<!-- 掃描注解包路徑,多個包用逗号分隔,不填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的 那個自動裝載注解就不需要了

繼續閱讀