基于dubbo的hessian协议远程调用实例
来源:互联网 发布:淘宝二手钢琴怎么样 编辑:程序博客网 时间:2024/06/05 04:02
最近在学习dubbo,捣鼓了两天,踩了许多坑,写出这个实例,记录一下
1.构建工具
使用gradle构建,build文件如下
group 'com.worstEzreal'version '1.0'apply plugin: 'java'apply plugin: 'war'sourceCompatibility = 1.8repositories { mavenLocal()}ext { springVersion = "4.3.6.RELEASE"}dependencies { compile( //provider 'com.worstEzreal:dubboDemo-api:1.0',//(服务提供者项目需注释此行) //spring配置 "org.springframework:spring-core:${springVersion}", "org.springframework:spring-beans:${springVersion}", "org.springframework:spring-context:${springVersion}", "org.springframework:spring-context-support:${springVersion}", "org.springframework:spring-tx:${springVersion}", "org.springframework:spring-web:${springVersion}", "org.springframework:spring-webmvc:${springVersion}", "org.springframework:spring-jdbc:${springVersion}", "org.springframework:spring-expression:${springVersion}", "org.springframework:spring-aop:${springVersion}", "org.springframework:spring-oxm:${springVersion}", //dubbo "org.javassist:javassist:3.21.0-GA", "io.netty:netty-all:4.1.15.Final", "com.101tec:zkclient:0.10", "org.apache.zookeeper:zookeeper:3.4.10", "com.caucho:hessian:4.0.7", //mybatis "org.mybatis:mybatis-spring:1.3.0", "org.mybatis:mybatis:3.4.1", //校验 "org.hibernate:hibernate-validator:4.2.0.Final", //日志 "ch.qos.logback:logback-classic:1.0.1", //json "com.fasterxml.jackson.core:jackson-annotations:2.8.10", "com.fasterxml.jackson.core:jackson-core:2.8.10", "com.fasterxml.jackson.core:jackson-databind:2.8.10", "com.alibaba:fastjson:1.2.38" ) compile("com.alibaba:dubbo:2.5.3"){ exclude(module: 'log4j') exclude(module: 'spring') } testCompile( "org.springframework:spring-test:${springVersion}", "org.hamcrest:hamcrest-core:1.3", "junit:junit:4.12", "com.jayway.jsonpath:json-path:2.2.0" ) providedCompile( 'javax.servlet:javax.servlet-api:3.0.1', "mysql:mysql-connector-java:5.1.32", "com.alibaba:druid:1.0.11" )}因为构成的服务使用者是基于SSM框架的,所以顺便导入了SSM的包
2.步骤
2.1 创建服务提供者项目
服务提供者通过tomcat启动,提供服务的接口
这里我把要打包的接口定义和接口实现分成两个模块,就写了个hello world,主要是dubbo的spring配置
<dubbo:application name="dubbo-hessian-provider"/> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/> <dubbo:protocol name="hessian" port="8787" server="servlet" contextpath="dubboProvider"/> <dubbo:service protocol="hessian" interface="com.worstEzreal.dubbo.service.IHelloService" ref="helloService" timeout="100000" path="dubbo/hello"/> <bean id="helloService" class="com.worstEzreal.dubbo.api.impl.HelloServiceImpl"/>其中使用了hessian协议和zookeeper做注册中心,还有就是web.xml也有点不同
<servlet> <servlet-name>dubboServlet</servlet-name> <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dubboServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>2.2 把服务接口打包
我这里是在idea下用gradle,需引用maven插件,直接install就会安装到本地仓库
2.3 启动zookeeper服务
为了方便直接在windows下安装了zookeeper,可以参考这篇文章
http://blog.csdn.net/morning99/article/details/40426133
2.4 创建消费者项目
这里写了一个基于ssm的restful风格的消费者项目,导入暴露接口的包后,进行dubbo配置
<dubbo:application name="dubbo-hessian-consumer"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference interface="com.worstEzreal.dubbo.service.IHelloService" id="helloService" timeout="100000" />其他配置为寻常ssm项目配置
2.5 单元测试
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:spring-context.xml")public class TestHello { @Test public void test(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath*:spring-context.xml"}); IHelloService helloService = (IHelloService)context.getBean("helloService"); System.out.println(helloService.hello("worstEzreal")); }}2.6 URL访问
也可以通过URL访问接口
@Autowired private IHelloService helloService; @Autowired private CityService cityService; @RequestMapping("city/{id}") public Result hello(@PathVariable int id){ String cityName = cityService.getCityById(id).getName(); String helloCity = helloService.hello(cityName); return new Result("success","",helloCity); }效果如图:
3.项目代码
https://github.com/worstEzreal/dubbo_demo
阅读全文
1 0
- 基于dubbo的hessian协议远程调用实例
- 基于Dubbo的Hessian协议实现远程调用
- 基于Dubbo的Hessian协议实现远程调用
- 基于Dubbo的Hessian协议实现远程调用
- 基于Dubbo的动态远程调用
- 基于Dubbo的动态远程调用
- 基于Dubbo的动态远程调用
- 基于 Hessian 轻量级远程调用的原理及示例
- Spring 与Hessian 整合的简单远程调用实例
- Hessian远程调用及序列化协议
- Hessian远程调用及序列化协议
- hessian远程调用的使用
- Hessian远程调用框架进阶教程(一) Hessian介绍JAVA使用Hessian进行编程实例
- 基于Hessian的高性能远程对象调用的服务器端和客户端的Demo
- dubbo-hessian协议http请求
- Spring2.5远程调用Hessian的例子
- Spring的Hessian,Burlap,HttpInvoker远程调用
- Hessian实现远程调用的简单案例
- 点击spinner 相同位置 触发响应
- android学习之实现屏幕自动旋转
- 设计模式
- Num.2 枚举(穷举)算法
- 基于Dragonboard410c的智能音箱(五)
- 基于dubbo的hessian协议远程调用实例
- 几种常见的算法比较
- 好用的js图标库
- ConvertToNodeSpace和convertToWorldSpace函数的应用
- java基础之垃圾回收机制
- python 打包 及运行后 解决:'gbk' codec can't encode character '\xa0'
- 简单Makefile
- java字符串转为数组
- 用于统计日常QQ聊天中会用到几个不同的汉字和每个汉字出现频率的Qt小程序