记一次基于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());
         
     }
}

测试通过表示上边改造的服务端可用。 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值