Spring Mvc 集成dubbo实现分布式

开发中有种需求是给移动端提供接口,本来是可以不用拆分的,直接在web基础上写接口也是可以的,但是考虑到不易维护,同时web是有session的 要登录等不便之处。于是乎就单独起一个项目用于提供接口,这种做法也是很常用的做法。

先介绍下dubbo

Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。 

废话不多说,直接上代码:


在原有的web项目(提供方)下的dubbo.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false">

	<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
	<dubbo:application name="catering-web" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry id="zkServer" address="zookeeper://192.168.1.6:2181" />

	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo"/>

	<!-- 商品接口 -->
	<dubbo:service ref="basicyitemmanager"
		interface="com.catering.system.manager.basic.IBasicItemManager" version="1.0.0"/>
		
	<!-- 商品规格接口 -->
	<dubbo:service ref="basicItemSkuManager"
		interface="com.catering.system.manager.basic.IBasicItemSkuManager" version="1.0.0"/>
		
	<!-- 商品类目接口 -->
	<dubbo:service ref="itemCategoryManager"
		interface="com.catering.system.manager.assistant.IItemCategoryManager" version="1.0.0"/>
		
		
	<!-- 采购申请单接口 -->
	<dubbo:service ref="purchasereqManager"
		interface="com.catering.system.manager.scm.IScmSmPurchasereqManager" version="1.0.0"/>
	
	<!-- 采购申请单详情接口 -->
	<dubbo:service ref="purchasereqDetailManager"
		interface="com.catering.system.manager.scm.IScmSmPurchaseReqDetailManager" version="1.0.0"/>
	
</beans>

提供方只要是向zookeeper注册中心暴露服务。这里 IBasicItemManager 是接口,在其实现类上要用@Service 注解 注册成Spring Bean 提供方的Service ref="id"  这个id就是注解的名称 ,如下图  

同时这个id也是消费方dubbo.xml里面引入的id,在调用的时候用过Spring 注入的时候也应该想同。

openAPI项目(消费方)的下的dubbo.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false">


	<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->

	<dubbo:application name="catering-openapi" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry id="zkClient" address="zookeeper://192.168.1.6:2181" />
	
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo"/>
	
	<!-- 商品接口 -->
	<dubbo:reference id="basicyitemmanager" interface="com.catering.system.manager.basic.IBasicItemManager" timeout="6000"
		check="false" version="1.0.0"/>
		
	<!-- 商品规格接口 -->
	<dubbo:reference id="basicItemSkuManager" interface="com.catering.system.manager.basic.IBasicItemSkuManager" timeout="6000"
		check="false" version="1.0.0"/>
		
	<!-- 商品类目接口 -->
	<dubbo:reference id="itemCategoryManager" interface="com.catering.system.manager.assistant.IItemCategoryManager" timeout="6000"
		check="false" version="1.0.0"/>
		
	<!-- 采购申请单接口 -->
	<dubbo:reference id="purchasereqManager"
		interface="com.catering.system.manager.scm.IScmSmPurchasereqManager" timeout="6000" check="false" version="1.0.0"/>
	
	<!-- 采购申请单详情接口 -->
	<dubbo:reference id="purchasereqDetailManager"
		interface="com.catering.system.manager.scm.IScmSmPurchaseReqDetailManager"  timeout="6000" check="false"  version="1.0.0"/>
		
</beans>


那么该怎么用这个暴露出来的接口呢,


@Controller
@RequestMapping("/index")
public class IndexController {
	
	private final static Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Autowired
	private IBasicItemManager basicyitemmanager;
	
	@RequestMapping("/query/{id}")
	@ResponseBody
	public void index(HttpServletRequest request,HttpServletResponse response,@PathVariable("id") String id){
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			if(StringUtils.isBlank(id)){
				result.put("code",0);
				result.put("message", "id 是空值");
			}else{
				BasicItem bi = basicyitemmanager.selectByPrimaryKey(id);
				JSONObject json = (JSONObject) JSONObject.toJSON(bi);
				result.put("code",1);
				result.put("message", "success");
				result.put("data", json);
			}
		} catch (Exception e) {
			logger.error(e.getMessage() ,e);
			result.put("code",0);
			result.put("message", "系统异常");
		}
		RespUtil.renderJson(response, result);
	}
}


直接使用@Autowired注解就可以了,就相当于在本项目中使用这个服务。

然后把dubbo.xml配置到web.xml里面就可以了

	<!-- Spring监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 防止Spring内存溢出监听器 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:dubbo.xml
		</param-value>
	</context-param>

,spring mvc的配置就懒得帖了。然后把zookeeper 启动起来就可以了,这样服务就发布到注册中心就能调用了


就可以访问到了。

因为我是向移动端提供rest接口的,用dubbox 会更好一点 dubbox实现了rest接口,而dubbo是没有的。以后再补上dubbox的。

dubbo开源地址

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spring Cloud 和 Dubbo 都是用于构建分布式系统的框架,但它们的设计理念和功能不同。Spring Cloud 是基于 Spring Boot 的微服务框架,提供了一系列的组件和工具,用于构建和管理分布式系统。Dubbo 是一个高性能的 RPC 框架,主要用于构建大规模的分布式系统。Spring Cloud 更加注重开发者的易用性和灵活性,而 Dubbo 更加注重性能和可靠性。 ### 回答2: Spring Cloud和Dubbo都是用于构建分布式系统的开源框架,但在设计理念、技术架构和目标用户等方面有一些区别。 首先,Spring Cloud是由Spring官方团队推出的,基于Spring Boot搭建的分布式系统解决方案。它通过各种组件提供类似于微服务架构中的服务注册与发现、负载均衡、服务调用、断路器、容灾、配置管理等功能,以简化分布式系统的开发和管理。 而Dubbo是阿里巴巴开源的RPC(远程过程调用)框架,提供了高性能和可伸缩的服务调用能力。Dubbo支持多种协议、负载均衡策略和容错机制,能够满足复杂的分布式系统需求。 其次,在技术架构方面,Spring Cloud基于Spring Boot和Spring Cloud Netflix等一系列子项目构建,使用Spring框架的全家桶。Spring Cloud提供了丰富的特性和模块,让开发者可以根据自己的需要选择所需的功能组件。 Dubbo则提供了服务注册与发现、远程调用、负载均衡、容错和配置管理等核心功能,对于构建分布式系统中的服务治理有着很好的支持。 最后,目标用户方面,Spring Cloud更注重的是解决大量中小型企业在构建分布式系统时遇到的问题,提供了更加易用的解决方案。而Dubbo则更适用于大型企业级系统,能够满足更高的性能和可扩展性要求。 综上所述,Spring Cloud和Dubbo都是用于构建分布式系统的优秀框架,各自有自己的特色和适用场景。具体选择哪个框架需要根据项目需求、技术栈和团队经验来进行评估权衡。 ### 回答3: Spring Cloud和Dubbo都是目前比较流行的分布式微服务框架。虽然它们的目标都是实现微服务架构,但在设计思路和功能上存在一些区别。 首先,Spring Cloud是基于Spring Boot构建的,因此可以无缝集成和使用Spring生态圈中的各种组件和工具。它提供了一系列的解决方案,如服务注册与发现、负载均衡、断路器、分布式配置等,可以快速搭建微服务架构;而Dubbo则基于Java的RPC协议进行通信,需要使用Dubbo提供的特定注解和配置,相对于Spring Cloud更为简洁。 其次,Spring Cloud倡导使用RESTful API作为微服务之间的通信方式,这种方式更加灵活和容易理解,适合于互联网场景。而Dubbo使用的是基于阿里巴巴的Hessian协议,默认提供了面向接口的RPC通信方式,适用于传统的大规模企业级应用。 此外,Spring Cloud提供了一套完整的生态系统,可以与其他Spring项目(如Spring MVCSpring Data等)无缝集成,降低了开发和维护的成本。相比之下,Dubbo生态系统相对较小,需要单独进行开发和集成。 综上所述,Spring Cloud相对于Dubbo而言更加灵活、功能更为全面,适用于构建复杂的互联网应用;而Dubbo则更加轻量、简洁,适合于传统的企业级应用。选择使用哪个框架要根据具体的业务需求和技术背景来决定。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值