dubbo使用记录

一.引入相关包

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.9</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>

因为需要 zookeeper 做集群管理,需要 zookeeper的客户端包 zkclient 因为这里面的图片上传服务是用的 hessian协议通信的,需要用到 hessian包

二 .配置

<?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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="pz998-app" />

<!-- 使用zookeeper广播注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="${dubbo.address}" />

<dubbo:reference id="uploadService"
interface="com.jztey.filecenter.service.UploadService" />

</beans>

注意 com.jztey.filecenter.service.UploadService 配置的接口包路径要与服务端一致

三.调用

@RequestMapping(value = "/uploadFile")
@ResponseBody
public Object uploadFile(HttpServletRequest request) throws IOException {
System.out.println("开始上传......");
ResponseVoRpc resp = new ResponseVoRpc();

try{
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
StringBuffer stringBuf = new StringBuffer();
for (Iterator it = multipartRequest.getFileNames(); it.hasNext();) {
String key = (String) it.next();
MultipartFile imgFile = multipartRequest.getFile(key);
byte[] data = imgFile.getBytes();
String path = uploadService.upload(data, "", new HashMap<String, String>());
System.out.println("返回地址:"+path);
stringBuf.append(commonSelectService.getApiJkdaRoot()+"/"+path);
}

Map<String, Object> result = new HashMap<String, Object>();
result.put("fileAbsolutePath",stringBuf.toString());
resp.setCode(ResponseVoRpc.CODE_COMMON_SUCCESS);
resp.setDatas(result);
}catch(Exception e){
e.printStackTrace();
resp.setCode(ResponseVoRpc.CODE_COMMON_FAILED);
resp.setCodeMsg("上传异常");
}
return resp;
}

 

----
spring mvc+tomcat源码分析视频(链接复制在浏览器打开)

https://study.163.com/course/courseMain.htm?share=2&shareId=480000001919582&courseId=1209399899&_trace_c_p_k2_=6d81bc445e9c462ab8d6345e40f6b0bf

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值