Dubbo的简单配置
-
新增一个父项目,并在父项目中添加依赖
<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>
-
在当前父项目中添加子模块DubboInferface, 并新增一个IHelloInterface的接口
package com.qfedu.dubbo; public interface IHelloInterface { String sayHello(String name); }
-
在当前父项目中再添加子模块DubboProvider
-
在当前子模块的pom文件中添加DubboInterface接口模块的依赖
<dependency> <groupId>com.qfedu</groupId> <artifactId>DubboInterface</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
-
新增接口子模块中接口的实现类
package com.qfedu.dubbo; public class HelloClass implements IHelloInterface { @Override public String sayHello(String name) { return "hello " + name; } }
-
在当前子模块的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来创建一个HelloClass的实例对象 --> <bean id="myDubbo" class="com.qfedu.dubbo.HelloClass" /> <!-- 定义一个dubbo的应用程序的名称,将来会在dubbo的管理中心查看 --> <dubbo:application name="DubboProvider" /> <!-- 配置dubbo协议,dubbo,hession, RMI --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 将当前应用注册到zookeeper注册中心服务器之上 --> <dubbo:registry address="zookeeper://ip:2181"></dubbo:registry> <!-- 将当前服务的接口暴露出去,以供消费方来消费 --> <dubbo:service interface="com.qfedu.dubbo.IHelloInterface" ref="myDubbo" /> </beans>
-
在当前子模块中新增一个测试类将当前的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"); // 死循环保证"长连接" while (true){} } }
-
-
新增子模块DubboConsumer
-
在当前子模块中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"> <!-- 配置当前应用的名称,给监控中心来使用 --> <dubbo:application name="DubboConsumer" /> <!-- 设置注册中心,从该注册中心去消费服务 --> <dubbo:registry address="zookeeper://ip:2181" /> <!-- 获取服务提供方所提供的接口数据 --> <dubbo:reference interface="com.qfedu.dubbo.IHelloInterface" id="myDubbo" /> </beans>
-
在当前子模块中新增测试类加载spring-dubbo的配置信息,并测试数据是否获取
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节点对应的对象 hello = ac.getBean("myDubbo", IHelloInterface.class); // 调用mydubbo对象中的方法,完成测试 String content = hello.sayHello("zhangsan"); // 打印测试结果 System.out.println(content); //while (true){} } }
-
-
启动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
-
运行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
-
至此dubbo的简单的配置完成