記一次基於spring4.3,xml配置文件的舊項目dubbo微服務化改造

目錄

背景

dubbo官方手冊

改造過程

添加dubbo依賴

添加dubbo配置文件

配置api層 

運行&查看服務

測試 


背景

昨天接到一個任務,把一個項目改造下,實現微服務化。

之前有用過基於SpringBoot+Dubbo的微服務,基於dubbo的註解來實現。這次改造,原理上和之前的相通,但實際操作未必一樣。

今天對改造完的項目進行了測試,沒有問題,在此記錄分享一下。

如果實現微服務,目前流行的SpringCloud,但如果不是一個全新的項目,而是對舊項目進行改造,把項目升級爲SpringCloud不太現實,那技術選型還是Dubbo合適。

dubbo官方手冊

Dubbo中文手冊:http://dubbo.apache.org/zh-cn/docs/user/preface/background.html

改造過程

添加dubbo依賴

這個項目的構建工具用的事gradle,需要的依賴如下:

compile 'dubbo:dubbo-registry-nacos:2.6.8'
compile 'dubbo:dubbo:2.6.8'
compile 'com.alibaba.nacos:nacos-client:1.1.3'
compile 'io.netty:netty-all:4.1.32.Final'

如果是maven構建,請在maven的pom文件中添加相關依賴座標。

添加dubbo配置文件

dubbo分爲服務提供端和消費端。這個項目是一個服務端提供端,所以添加配置文件spring-dubbo-provider.xml(配置文件的命名最好可以見名知意,比如不要寫spring-dubbo.xml這樣的名字,分不清是服務端還是消費端) 

spring-dubbo-provider.xml內容如下(涉及到敏感信息部分,比如ip會用xxx表示),配置中心爲nacos:

<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
	http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> 
	<!-- 提供方應用信息,用於計算依賴關係 -->
	<dubbo:application name="mdm" owner="578376895" version="3.0">
		<dubbo:parameter key="qos.enable" value="false" />
		<dubbo:parameter key="qos.accept.foreign.ip" value="false" />
		<!-- <dubbo:parameter key="qos.port" value="33333" /> -->
	</dubbo:application>
	<!-- 使用nacos暴露服務地址 -->
	<dubbo:registry protocol="nacos" address="192.168.xxx.111:8848,192.168.xxx.222:8848,192.168.xxx.333:8848" />

	<!-- 用dubbo協議在20880端口暴露服務,注意這裏針對性能不同,可採用多協議配置 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<dubbo:provider id="payload" payload="21747220" />
	
	<!-- 聲明需要暴露的服務 -->
	<dubbo:service interface="com.xxx.xxx.service.basics.BaseDeptWardService" ref="baseDeptWardServiceImpl" timeout="5000" version="1.0" />
	
	
	<!-- 。。。其他要暴露的服務在此省略 -->
	
        <!-- 相同服務接口,不同實現,用group區分 -->
	<dubbo:service interface="com.xxx.xxx.service.sync.MDMService" group="MDMServiceImpl" ref="MDMServiceImpl" timeout="5000" version="1.0" />
	
	<dubbo:service interface="com.xxx.xxx.service.sync.MDMService" group="MDMExistServiceImpl" ref="MDMExistServiceImpl" timeout="5000" version="1.0" />
	
	<dubbo:service interface="com.xxx.xxx.service.template.TemplateService" ref="templateServiceImpl" timeout="5000" version="1.0" />
	
</beans>

 

需要注意的是:上邊的配置文件中dubbo:service的ref屬性對應的springbean的name,要確保該ref對應的springbean存在,在我的項目中,ref的bean都是基於spring的@Service註解配置的,也就是舊項目的service層的接口實現。

配置api層 

在dubbo官方手冊中,推薦api的單獨抽取出來

因爲是一箇舊項目,把所有的API(service層的接口部分 )抽取出來,改動較大,有可能會影響線上生產環境。

解決辦法是:利用構建工具gradle/maven把API導出爲單獨的jar包,gradle配置參考如下:

//暴露service層Api之demo
task service_Jar(type: Jar){
    baseName="mdm-service"
    //appendix=SERVICE_JAR_VERSION
//    classifier=_CLASSIFIER
    from(sourceSets.main.output){
        include 'com/xx/xxx/service/**/*Service.class'
    }
}

 運行&查看服務

在nacos中查看服務管理->服務列表,服務已暴露成功:

 

測試 

建立一個消費端進行測試,消費端配置如下:

<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
	http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> 
	<!-- 提供方應用信息,用於計算依賴關係 -->
	<dubbo:application name="mdm" owner="578376895" version="3.0">
		<dubbo:parameter key="qos.enable" value="false" />
		<dubbo:parameter key="qos.accept.foreign.ip" value="false" />
		<!-- <dubbo:parameter key="qos.port" value="33333" /> -->
	</dubbo:application>
	<!-- 使用nacos暴露服務地址 -->
	<dubbo:registry protocol="nacos" address="192.168.xxx.111:8848,192.168.xxx.222:8848,192.168.111.333:8848" />

	<dubbo:reference id="baseDeptWardService" interface="com.xxx.xxx.service.basics.BaseDeptWardService" version="1.0"/>
	
	
</beans>

 建立測試類

/**  
* <p>Title: T</p>  
* <p>Description: </p>  
* @author wangzhj 
* @date 2020年5月15日  
*/
 
 @Controller
public class T {

     @Autowired
     BaseDeptWardService baseDeptWardService;
     
     @RequestMapping(value = {"/test.html"})
     public void t(HttpServletResponse resp) throws IOException {
         
         List  l= baseDeptWardService.listDeptWard();
         resp.getWriter().write(baseDeptWardService+","+l.size());
         
     }
}

測試通過表示上邊改造的服務端可用。 

 

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