dubbo实践
最近公司准备重构内部服务模块,准备使用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
dubbo实践的更多相关文章
- Dubbo实践(六)Spring加载Bean流程
根据上一小节对于spring扩展schema的介绍,大概可以猜到dubbo中相关的内容是如何实现的. 再来回顾Dubbo实践(一)中定义的dubbo-provider.xml: <?xml ve ...
- Dubbo实践(五)扩展Spring Schema
先回顾Dubbo实践(一)中定义的dubbo-provider.xml: <?xml version="1.0" encoding="UTF-8"?> ...
- 阿里技术专家详解 Dubbo 实践,演进及未来规划
Dubbo 整体介绍 Dubbo 是一款高性能,轻量级的 Java RPC 框架.虽然它是以 Java 语言来出名的,但是现在我们生态里面已经有 Go.Python.PHP.Node.JS 等等语言. ...
- Dubbo实践笔记
注意的地方 默认情况下,cluster=failover.retries=2,意为失败重试两次,不包含原生调用.如需配置不重试,需设置retries=-1,或者使用failfast(快速失败)模式 如 ...
- Dubbo实践(十七)telnet
telnet的介绍可以参看<java网络编程3>中有一段介绍telnet,我们可以理解为,telnet命令是通过socket协议与服务器端通信.Dubbo提供了telnet命令去查看服务功 ...
- Dubbo实践(十六)集群容错
Dubbo作为一个分布式的服务治理框架,提供了集群部署,路由,软负载均衡及容错机制.下图描述了Dubbo调用过程中的对于集群,负载等的调用关系: 集群 Cluster 将Directory中的多个In ...
- Dubbo实践(十五)消费者引用服务
Refer取得invoker的过程 <!-- 指定了哪种的注册中心,是基于zookeeper协议的,指定了注册中心的地址以及端口号 --> <dubbo:registry proto ...
- Dubbo实践(十四)生产者发布服务
Export发布服务流程 Dubbo协议向注册中心发布服务:当服务提供方,向dubbo协议的注册中心发布服务的时候,是如何获取,创建注册中心的,如何注册以及订阅服务的,下面我们来分析其流程. 看如下配 ...
- Dubbo实践(十三)Export
Spring在启动Dubbo服务端应用时,会实例化ServiceBean<T>并设置配置属性,然后调用export方法: @SuppressWarnings({"unchecke ...
随机推荐
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- css的初始化样式
一切为了敏捷开发: http://blog.sina.com.cn/s/blog_71ed1b870101a52w.html CSS初始化示例代码 /* css reset www.admin1000 ...
- 星云opencv总结
- Spring缓存机制的理解
在spring缓存机制中,包括了两个方面的缓存操作:1.缓存某个方法返回的结果:2.在某个方法执行前或后清空缓存. 下面写两个类来模拟Spring的缓存机制: package com.sin90lzc ...
- Android Studio中的CmakeList NDK配置
Android Studio2.2之后直接可以在创建工程时添加NDK支持了,添加之后,main文件夹下会多出一个native-lib.cpp这个文件,如果只为了一个简单的NDK接口,貌似这就结束了.直 ...
- 页内多个input全选不干扰且只用一段代码。
//html内容 <body> <div id="d1"> <input type="checkbox" class=" ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- vue组件
分享出来让思路更成熟. 首先组件是 Vue.js 最强大的功能之一. 可以减少很多的工作量,提高工作效率. 编写一个可复用性的组件,虽然官网上也有.... 编写可复用性的vue组件 具备一下的几个要求 ...
- easyui datagrid 悬浮事件
easyui的单元格提示窗体 鼠标悬浮事件 function findAllPreven() { var infoname = $('#area').val(); areadatagrid=$('# ...
- TThread.CreateAnonymousThread() 匿名线程对象的应用
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...