开发中有种需求是给移动端提供接口,本来是可以不用拆分的,直接在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的。