1.新增一個父項目赏表,并在父項目中添加依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.8</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2.在當前父項目中添加子模塊DubboInferface, 并新增一個IHelloInterface的接口
package com.qfedu.dubbo;
public interface IHelloInterface {
String sayHello(String name);
}
3.在當前父項目中再添加子模塊DubboProvider
(1)在當前子模塊的pom文件中添加DubboInterface接口模塊的依賴
<dependency>
<groupId>com.qfedu</groupId>
<artifactId>DubboInterface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
(2)新增接口子模塊中接口的實現(xiàn)類
package com.qfedu.dubbo;
public class HelloClass implements IHelloInterface {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
(3)在當前子模塊的resources下新增spring-dubbo的配置文件
<?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">
<!--
使用bean來創(chuàng)建一個HelloClass的實例對象
-->
<bean id="myDubbo" class="com.qfedu.dubbo.HelloClass" />
<!--
定義一個dubbo的應(yīng)用程序的名稱参滴,將來會在dubbo的管理中心查看
-->
<dubbo:application name="DubboProvider" />
<!--
配置dubbo協(xié)議,dubbo缆镣,hession搓茬, RMI
-->
<dubbo:protocol name="dubbo" port="20880" />
<!--
將當前應(yīng)用注冊到zookeeper注冊中心服務(wù)器之上
-->
<dubbo:registry address="zookeeper://ip:2181"></dubbo:registry>
<!--
將當前服務(wù)的接口暴露出去犹赖,以供消費方來消費
-->
<dubbo:service interface="com.qfedu.dubbo.IHelloInterface" ref="myDubbo" />
</beans>
(4)在當前子模塊中新增一個測試類將當前的spring-dubbo配置文件加載起來
package com.qfedu.dubbo;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestProvider {
@Test
public void testProvider(){
// 加載spring-dubbo的配置文件
new ClassPathXmlApplicationContext("classpath:spring-dubbo.xml");
// 提示信息
System.out.println("provider is ready");
// 死循環(huán)保證"長連接"
while (true){}
}
}
4.新增子模塊DubboConsumer
(1)在當前子模塊中resources下新增spring-dubbo的配置文件
<?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">
<!--
配置當前應(yīng)用的名稱,給監(jiān)控中心來使用
-->
<dubbo:application name="DubboConsumer" />
<!--
設(shè)置注冊中心卷仑,從該注冊中心去消費服務(wù)
-->
<dubbo:registry address="zookeeper://ip:2181" />
<!--
獲取服務(wù)提供方所提供的接口數(shù)據(jù)
-->
<dubbo:reference interface="com.qfedu.dubbo.IHelloInterface" id="myDubbo" />
</beans>
(2)在當前子模塊中新增測試類加載spring-dubbo的配置信息峻村,并測試數(shù)據(jù)是否獲取
package com.qfedu.dubbo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestConsumer {
IHelloInterface hello;
@Test
public void testConsumer(){
// 加載spring-dubbo的配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-dubbo.xml");
// 通過id拿到spring-dubbo中定義的myDubbo節(jié)點對應(yīng)的對象
hello = ac.getBean("myDubbo", IHelloInterface.class);
// 調(diào)用mydubbo對象中的方法,完成測試
String content = hello.sayHello("zhangsan");
// 打印測試結(jié)果
System.out.println(content);
//while (true){}
}
}
5.啟動zookeeper之后锡凝,運行provide測試類進行測試
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
provider is ready
6.運行consumer的測試類
com.qfedu.dubbo.TestConsumer,testConsumer
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
hello zhangsan
7.至此dubbo的簡單的配置完成