前幾天換工作,到新公司用到了阿里的Dubbo,花了兩天的時間去學(xué)習(xí),在網(wǎng)上找了很多教程,感覺都不是太詳細(xì),所以動手搭了下環(huán)境,寫了這篇XX。
有關(guān)dubbo的介紹就不多說了,請查閱官方文檔:http://dubbo.io/
簡介
本次環(huán)境搭建用到的工具:IDEA,maven,zookeeper。
首先介紹下項目的大概情況,首先是一個maven父工程DubboTest,下面有三個子工程DubboConsumer,DubboProvider,DubboCommon。
- DubboTest:總的項目,父工程,需要的依賴都在這里配置。
- DubboConsumer:非web項目,dubbo的消費方。
- DubboProvider:非web項目,dubbo服務(wù)提供方。
- DubboCommon:非web項目,打成Jar包,是DubboConsumer和-
DubboProvider共享的包,里面定義的是公用的接口。
以上相關(guān)的概念就不多做解釋了。
項目代碼:https://github.com/dachengxi/DubboTest
搭建
- 打開IDEA,New Project,選中Maven項目,不要勾選Create from archetype,點擊next,填寫GroupId等信息,然后再填寫其他的相關(guān)信息,這個工程命名DubboTest,是父項目。
- 進入項目之后,選擇新建模塊,分別簡歷三個子項目,過程與上面類似。分別命名為DubboConsumer,DubboProvider,DubboCommon。有關(guān)POM文件內(nèi)容請參考項目代碼中的內(nèi)容,這里不再貼出來。(主要是因為簡書這粘貼太XX)。
- 項目搭建完成之后,就可以開始寫代碼了。
首先在DubboCommon項目中編寫公共的接口,代碼如下:
package dubbo.common.hello.service;
/**
* Created by cheng.xi on 15/4/12.
*/
public interface HelloService {
public void sayHello();
}
接著寫DubboProvider項目的接口實現(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中啟動服務(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();
}
}
}
最后編寫DubboConsumer下的調(diào)用代碼,此處使用單元測試的方式調(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ù)啟動,調(diào)用!");
helloService.sayHello();
}
}
- 上面代碼已經(jīng)寫好,其中需要用的幾個配置文件如下所示。
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>
- 至此項目中的代碼編寫已經(jīng)完成,下一步是安裝和啟動zookeeper,有關(guān)過程請自己研究下。
- 啟動好了zookeeper之后,首先運行DubboProvider中的那個main方法,然后運行DubboConsumer中的test()方法。