最近公司准备重构内部服务模块,准备使用dubbo,故研究一下。
官方文档:http://alibaba.github.io/dubbo-doc-static/Home-zh.htm
1. 用maven创建一个项目(父模块),将src目录删除:
mvn archetype:generate -DgroupId=com.winter.dubbo -DartifactId=dubbo-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=internal
2. 修改pom.xml文件,将packaging设为pom。并增加以下依赖:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.9</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> <exclusions> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.netflix.curator</groupId> <artifactId>curator-framework</artifactId> <version>1.1.16</version> </dependency>
3.创建子模块:provider、customer、api
mvn archetype:generate -DgroupId=com.winter.dubbo -DartifactId=provider -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=internal
mvn archetype:generate -DgroupId=com.winter.dubbo -DartifactId=customer -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=internal
mvn archetype:generate -DgroupId=com.winter.dubbo -DartifactId=api -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=internal
将provider、customer、api模块移到父模块目录下,同时在父模块下的pom.xml文件中增加模块依赖:
<modules> <module>provider</module> <module>customer</module> <module>api</module></modules>
分别修改provider、customer、api模块的pom.xml文件,增加一下内容:
<parent> <groupId>com.winter.dubbo</groupId> <artifactId>dubbo-parent</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent>
由于provider、customer模块都依赖api模块,记着在它们的pom.xml增加api的依赖:
<dependency> <groupId>com.winter.dubbo</groupId> <artifactId>api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
最终形成的目录结构
4. 在api模块中创建相关服务接口
package com.winter.dubbo.api; public interface DemoService { String sayHello(String name); }
5. 在provider模块中实现相关服务
package com.winter.dubbo.provider; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; } }
6. 在provider模块中创建服务提供者、以及服务的实现
package com.winter.dubbo.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ProviderServer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"}); context.start(); System.out.println("请按任意键退出"); System.in.read(); // 按任意键退出 } }
package com.winter.dubbo.provider; import com.winter.dubbo.api.DemoService; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; } }
7. 在customer模块中创建消费者
package com.winter.dubbo.customer; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"customer.xml"}); context.start(); } }
package com.winter.dubbo.customer; import com.alibaba.dubbo.config.annotation.Reference; import com.winter.dubbo.api.DemoService; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component public class TestClient { @Reference private DemoService demoService; @PostConstruct public void run() { String res = demoService.sayHello("Dubbo"); System.out.println( res ); // 显示调用结果 } }
8. 在provider模块的resources下创建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-service" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.winter.dubbo.api.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.winter.dubbo.provider.DemoServiceImpl" />
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="com.winter.dubbo"/>
</beans>
9. 在customer模块的resources下创建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" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.winter.dubbo.api.DemoService" /> <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 --> <dubbo:annotation package="com.winter.dubbo"/> </beans>
10. 测试
先保证zookeeper已经启动,在通过ProviderServer.java启动服务,最后运行customer模块下的Application.java来测试,你就能看到经典的“Hello Dubbo”。
11.详细代码请参考:https://git.oschina.net/winter550/dubbo-demo