1. 程式人生 > >基於dubbo的hessian協議遠端呼叫例項

基於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,可以參考這篇文章

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.專案程式碼