前幾天換工作,到新公司用到了阿里的Dubbo耕餐,花了兩天的時(shí)間去學(xué)習(xí)凡傅,在網(wǎng)上找了很多教程,感覺(jué)都不是太詳細(xì)肠缔,所以動(dòng)手搭了下環(huán)境夏跷,寫(xiě)了這篇XX。
有關(guān)dubbo的介紹就不多說(shuō)了明未,請(qǐng)查閱官方文檔:http://dubbo.io/
簡(jiǎn)介
本次環(huán)境搭建用到的工具:IDEA槽华,maven,zookeeper趟妥。
首先介紹下項(xiàng)目的大概情況猫态,首先是一個(gè)maven父工程DubboTest,下面有三個(gè)子工程DubboConsumer披摄,DubboProvider亲雪,DubboCommon。
- DubboTest:總的項(xiàng)目疚膊,父工程义辕,需要的依賴(lài)都在這里配置。
- DubboConsumer:非web項(xiàng)目寓盗,dubbo的消費(fèi)方灌砖。
- DubboProvider:非web項(xiàng)目,dubbo服務(wù)提供方傀蚌。
- DubboCommon:非web項(xiàng)目基显,打成Jar包,是DubboConsumer和-
DubboProvider共享的包善炫,里面定義的是公用的接口撩幽。
以上相關(guān)的概念就不多做解釋了。
項(xiàng)目代碼:https://github.com/dachengxi/DubboTest
搭建
- 打開(kāi)IDEA销部,New Project摸航,選中Maven項(xiàng)目制跟,不要勾選Create from archetype舅桩,點(diǎn)擊next,填寫(xiě)GroupId等信息雨膨,然后再填寫(xiě)其他的相關(guān)信息擂涛,這個(gè)工程命名DubboTest,是父項(xiàng)目。
- 進(jìn)入項(xiàng)目之后撒妈,選擇新建模塊恢暖,分別簡(jiǎn)歷三個(gè)子項(xiàng)目,過(guò)程與上面類(lèi)似狰右。分別命名為DubboConsumer杰捂,DubboProvider,DubboCommon棋蚌。有關(guān)POM文件內(nèi)容請(qǐng)參考項(xiàng)目代碼中的內(nèi)容嫁佳,這里不再貼出來(lái)。(主要是因?yàn)楹?jiǎn)書(shū)這粘貼太XX)谷暮。
- 項(xiàng)目搭建完成之后蒿往,就可以開(kāi)始寫(xiě)代碼了。
首先在DubboCommon項(xiàng)目中編寫(xiě)公共的接口湿弦,代碼如下:
package dubbo.common.hello.service;
/**
* Created by cheng.xi on 15/4/12.
*/
public interface HelloService {
public void sayHello();
}
接著寫(xiě)DubboProvider項(xiàng)目的接口實(shí)現(xiàn)瓤漏,代碼如下:
package dubbo.provider.hello.service.impl;
import dubbo.common.hello.service.HelloService;
/**
* Created by cmcc on 15/4/12.
*/
public class HelloServiceImpl implements HelloService {
@Override
public void sayHello() {
System.out.println("這里是Provider");
System.out.println("HelloWorld Provider!");
}
}
下面是DubboProvider中啟動(dòng)服務(wù)的代碼:
package dubbo.provider.hello.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* Created by cheng.xi on 15/4/12.
*/
public class StartProvider {
public static void main(String[] args){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"});
context.start();
System.out.println("這里是dubbo-provider服務(wù)颊埃,按任意鍵退出");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
最后編寫(xiě)DubboConsumer下的調(diào)用代碼蔬充,此處使用單元測(cè)試的方式調(diào)用,代碼如下:
package dubbo.consumer.hello.main;
import dubbo.common.hello.service.HelloService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by cheng.xi on 15/4/12.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/dubbo-consumer.xml")
public class StartConsumer {
@Autowired
private HelloService helloService;
@Test
public void test(){
System.out.println("dubbo-consumer服務(wù)啟動(dòng)班利,調(diào)用娃惯!");
helloService.sayHello();
}
}
- 上面代碼已經(jīng)寫(xiě)好,其中需要用的幾個(gè)配置文件如下所示肥败。
dubbo-consumer.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: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="dubbo-consumer" />
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<dubbo:reference id="helloService" interface="dubbo.common.hello.service.HelloService" />
</beans>
dubbo-provider.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: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="dubbo-provider" />
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<bean id="helloService" class="dubbo.provider.hello.service.impl.HelloServiceImpl" />
<dubbo:service interface="dubbo.common.hello.service.HelloService" ref="helloService" />
</beans>
- 至此項(xiàng)目中的代碼編寫(xiě)已經(jīng)完成趾浅,下一步是安裝和啟動(dòng)zookeeper,有關(guān)過(guò)程請(qǐng)自己研究下馒稍。
- 啟動(dòng)好了zookeeper之后皿哨,首先運(yùn)行DubboProvider中的那個(gè)main方法,然后運(yùn)行DubboConsumer中的test()方法纽谒。