基于dubbo的hessian协议远程调用实例

最近在学习dubbo,捣鼓了两天,踩了许多坑,写出这个实例,记录一下


1.构建工具

使用gradle构建,build文件如下

group 'com.worstEzreal'
version '1.0'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值