dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务或Rpc服务
1.下载,编译,运行demo
  1).安装zookeeper
    下载:http://apache.claz.org/zookeeper/
    配置  conf/zoo.cfg
  tickTime=2000
  initLimit=10
  syncLimit=5
  dataDir=D:/java/zookeeper-3.4.6/data
  dataLogDir=D:/java/zookeeper-3.4.6/log
  clientPort=2181
  #server.1=localhost:2287:3387

启动

  bin/zkServer.cmd 启用zookeeper
  2).下载dubbo
  git clone https://github.com/dangdangdotcom/dubbox
  mvn install -Dmaven.test.skip=true 跳过测试编译
  3).运行demo
  运行服务提供方,zk里多出了一个dubbo的节点,服务全注册
  \dubbox\dubbo-demo\dubbo-demo-provider\src\test\java\com\alibaba\dubbo\demo\provider\DemoProvider.java
  运行服务消费方调用测试
  \dubbox\dubbo-demo\dubbo-demo-consumer\src\test\java\com\alibaba\dubbo\demo\consumer\DemoConsumer.java
  rest调用
  \dubbox\dubbo-demo\dubbo-demo-consumer\src\test\java\com\alibaba\dubbo\demo\consumer\RestClient.java
  浏览器访问
  http://localhost:8888/services/users/100.xml 或 http://localhost:8888/services/users/100.json

2.简单demo
 1).公用对象和接口
  User     

  1. package cn.com.hello;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class User implements Serializable {
  6. private static final long serialVersionUID = 1L;
  7. private int age;
  8. private String name;
  9. private String sex;
  10.  
  11. public User() {
  12. super();
  13. }
  14.  
  15. public User(int age, String name, String sex) {
  16. super();
  17. this.age = age;
  18. this.name = name;
  19. this.sex = sex;
  20. }
  21.  
  22. public int getAge() {
  23. return age;
  24. }
  25.  
  26. public void setAge(int age) {
  27. this.age = age;
  28. }
  29.  
  30. public String getName() {
  31. return name;
  32. }
  33.  
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37.  
  38. public String getSex() {
  39. return sex;
  40. }
  41.  
  42. public void setSex(String sex) {
  43. this.sex = sex;
  44. }
  45.  
  46. }

  UserService

  1. package cn.com.hello;
  2.  
  3. import java.util.List;
  4.  
  5. public interface UserService {
  6.  
  7. String sayHello(String name);
  8.  
  9. public List<User> getUsers();
  10.  
  11. }

2).生产者Provider
  实现UserServiceImpl

  1. package cn.com.hello.provider.impl;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.alibaba.dubbo.config.annotation.Service;
  7.  
  8. import cn.com.hello.UserService;
  9. import cn.com.hello.User;
  10.  
  11. //@Service
  12. public class UserServiceImpl implements UserService {
  13.  
  14. public String sayHello(String name) {
  15. return "Hello " + name;
  16. }
  17.  
  18. public List<User> getUsers() {
  19. List<User> list = new ArrayList<User>();
  20. User u1 = new User();
  21. u1.setName("jack");
  22. u1.setAge(20);
  23. u1.setSex("m");
  24.  
  25. User u2 = new User();
  26. u2.setName("tom");
  27. u2.setAge(21);
  28. u2.setSex("m");
  29.  
  30. User u3 = new User();
  31. u3.setName("rose");
  32. u3.setAge(19);
  33. u3.setSex("w");
  34.  
  35. list.add(u1);
  36. list.add(u2);
  37. list.add(u3);
  38. return list;
  39. }
  40. }

  配置dubbo-provider.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://code.alibabatech.com/schema/dubbo
  7. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  8.  
  9. <!-- 提供方应用信息,用于计算依赖关系 -->
  10. <dubbo:application name="provider-helloworld" />
  11.  
  12. <!-- 使用 multicast广播注册中心暴露服务地址 -->
  13. <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>
  14.  
  15. <!-- 用dubbo协议在20880端口暴露服务 -->
  16. <dubbo:protocol name="dubbo" server="grizzly" port="20880" />
  17.  
  18. <!-- 声明需要暴露的服务接口 -->
  19. <dubbo:service interface="cn.com.hello.UserService" ref="userService" />
  20.  
  21. <!-- 和本地bean一样实现服务 -->
  22. <bean id="userService" class="cn.com.hello.provider.impl.UserServiceImpl" />
  23.  
  24. </beans>

  启动 Provider

  1. package cn.com.hello.provider;
  2.  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4.  
  5. public class Provider {
  6.  
  7. public static void main(String[] args) throws Exception {
  8. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "dubbo-provider.xml" });
  9. context.start();
  10. System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
  11. }
  12. }

3).消费者Consumer

  配置 dubbo-consumer.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  4. 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">
  5.  
  6. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
  7. <dubbo:application name="consumer-helloworld" />
  8.  
  9. <!-- 使用multicast广播注册中心暴露发现服务地址 -->
  10. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  11.  
  12. <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
  13. <dubbo:reference id="userService" interface="cn.com.hello.UserService" />
  14.  
  15. </beans>

  启动 Consumer

  1. package cn.com.hello.consumer;
  2. import java.util.List;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import cn.com.hello.UserService;
  5. public class Consumer {
  6. public static void main(String[] args) throws Exception {
  7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "dubbo-consumer.xml" });
  8. context.start();
  9.  
  10. UserService userService = (UserService) context.getBean("userService");
  11. String hello = userService.sayHello("guoxue");
  12. System.out.println(hello);
  13.  
  14. List list = userService.getUsers();
  15. if (list != null && list.size() > 0) {
  16. for (int i = 0; i < list.size(); i++) {
  17. System.out.println(list.get(i));
  18. }
  19. }
  20. System.in.read();
  21. }
  22.  
  23. }

4).依赖jar

  pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>cn.com.hello</groupId>
  5. <artifactId>dubbo-hello</artifactId>
  6. <packaging>jar</packaging>
  7. <version>1.0.0</version>
  8. <properties>
  9. <spring_version>4.1.6.RELEASE</spring_version>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-context</artifactId>
  15. <version>${spring_version}</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework</groupId>
  19. <artifactId>spring-core</artifactId>
  20. <version>${spring_version}</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-test</artifactId>
  25. <version>${spring_version}</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-beans</artifactId>
  30. <version>${spring_version}</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-aop</artifactId>
  35. <version>${spring_version}</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework</groupId>
  39. <artifactId>spring-context-support</artifactId>
  40. <version>${spring_version}</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.javassist</groupId>
  44. <artifactId>javassist</artifactId>
  45. <version>3.20.0-GA</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>grizzly</groupId>
  49. <artifactId>grizzly</artifactId>
  50. <version>1.0.28</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.eclipse.jetty</groupId>
  54. <artifactId>jetty-server</artifactId>
  55. <version>9.3.5.v20151012</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.apache.curator</groupId>
  59. <artifactId>curator-framework</artifactId>
  60. <version>2.9.0</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.apache.cxf</groupId>
  64. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  65. <version>3.1.4</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.apache.thrift</groupId>
  69. <artifactId>libthrift</artifactId>
  70. <version>0.9.3</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>com.alibaba</groupId>
  74. <artifactId>dubbo</artifactId>
  75. <version>2.8.4</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.apache.zookeeper</groupId>
  79. <artifactId>zookeeper</artifactId>
  80. <version>3.4.6</version>
  81. <exclusions>
  82. <exclusion>
  83. <groupId>org.slf4j</groupId>
  84. <artifactId>slf4j-log4j12</artifactId>
  85. </exclusion>
  86. </exclusions>
  87. </dependency>
  88. <dependency>
  89. <groupId>com.github.sgroschupf</groupId>
  90. <artifactId>zkclient</artifactId>
  91. <version>0.1</version>
  92. </dependency>
  93. <!-- log -->
  94. <dependency>
  95. <groupId>log4j</groupId>
  96. <artifactId>log4j</artifactId>
  97. <version>1.2.14</version>
  98. </dependency>
  99. <dependency>
  100. <groupId>commons-logging</groupId>
  101. <artifactId>commons-logging</artifactId>
  102. <version>1.2</version>
  103. </dependency>
  104. <dependency>
  105. <groupId>org.apache.logging.log4j</groupId>
  106. <artifactId>log4j-api</artifactId>
  107. <version>2.6.1</version>
  108. </dependency>
  109. <dependency>
  110. <groupId>org.apache.logging.log4j</groupId>
  111. <artifactId>log4j-core</artifactId>
  112. <version>2.6.1</version>
  113. </dependency>
  114. <!-- log end -->
  115.  
  116. <!-- TODO move versions into properties -->
  117. <dependency>
  118. <groupId>javax.ws.rs</groupId>
  119. <artifactId>javax.ws.rs-api</artifactId>
  120. <version>2.0</version>
  121. </dependency>
  122. <dependency>
  123. <groupId>org.jboss.resteasy</groupId>
  124. <artifactId>resteasy-jaxrs</artifactId>
  125. <version>3.0.7.Final</version>
  126. </dependency>
  127. <dependency>
  128. <groupId>org.jboss.resteasy</groupId>
  129. <artifactId>resteasy-client</artifactId>
  130. <version>3.0.7.Final</version>
  131. </dependency>
  132. <dependency>
  133. <groupId>org.jboss.resteasy</groupId>
  134. <artifactId>resteasy-netty</artifactId>
  135. <version>3.0.7.Final</version>
  136. </dependency>
  137. <dependency>
  138. <groupId>org.jboss.resteasy</groupId>
  139. <artifactId>resteasy-jdk-http</artifactId>
  140. <version>3.0.7.Final</version>
  141. </dependency>
  142. <dependency>
  143. <groupId>org.jboss.resteasy</groupId>
  144. <artifactId>resteasy-jackson-provider</artifactId>
  145. <version>3.0.7.Final</version>
  146. </dependency>
  147. <dependency>
  148. <groupId>org.jboss.resteasy</groupId>
  149. <artifactId>resteasy-jaxb-provider</artifactId>
  150. <version>3.0.7.Final</version>
  151. </dependency>
  152.  
  153. <dependency>
  154. <groupId>org.glassfish.grizzly</groupId>
  155. <artifactId>grizzly-core</artifactId>
  156. <version>2.1.4</version>
  157. </dependency>
  158. </dependencies>
  159. <build>
  160. <finalName>dubbo-hello</finalName>
  161. </build>
  162. </project>

5).日志

dubbo_分布式Rpc服务的更多相关文章

  1. 基于netty轻量的高性能分布式RPC服务框架forest<下篇>

    基于netty轻量的高性能分布式RPC服务框架forest<上篇> 文章已经简单介绍了forest的快速入门,本文旨在介绍forest用户指南. 基本介绍 Forest是一套基于java开 ...

  2. 基于netty轻量的高性能分布式RPC服务框架forest<上篇>

    工作几年,用过不不少RPC框架,也算是读过一些RPC源码.之前也撸过几次RPC框架,但是不断的被自己否定,最近终于又撸了一个,希望能够不断迭代出自己喜欢的样子. 顺便也记录一下撸RPC的过程,一来作为 ...

  3. RSF 分布式 RPC 服务框架的分层设计

    RSF 是个什么东西? 一个高可用.高性能.轻量级的分布式服务框架.支持容灾.负载均衡.集群.一个典型的应用场景是,将同一个服务部署在多个Server上提供 request.response 消息通知 ...

  4. 基于开源Dubbo分布式RPC服务框架的部署整合

    一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...

  5. 【Rpc】基于开源Dubbo分布式RPC服务框架的部署整合

    一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...

  6. 一个轻量级分布式RPC框架--NettyRpc

    1.背景 最近在搜索Netty和Zookeeper方面的文章时,看到了这篇文章<轻量级分布式 RPC 框架>,作者用Zookeeper.Netty和Spring写了一个轻量级的分布式RPC ...

  7. 轻量级分布式 RPC 框架

    @import url(/css/cuteeditor.css); 源码地址:http://git.oschina.net/huangyong/rpc RPC,即 Remote Procedure C ...

  8. 【转】轻量级分布式 RPC 框架

    第一步:编写服务接口 第二步:编写服务接口的实现类 第三步:配置服务端 第四步:启动服务器并发布服务 第五步:实现服务注册 第六步:实现 RPC 服务器 第七步:配置客户端 第八步:实现服务发现 第九 ...

  9. 轻量级分布式RPC框架

    随笔- 139  文章- 0  评论- 387  一个轻量级分布式RPC框架--NettyRpc   1.背景 最近在搜索Netty和Zookeeper方面的文章时,看到了这篇文章<轻量级分布式 ...

随机推荐

  1. Miller-Rabin与Pollard-Rho备忘

    Miller-Rabin素性测试算法: 根据费马小定理当p为素数时成立,所以如果存在一个a使x不满足此定理,则x必然不为素数. 但这是充分条件而不是必要条件,所以对于每个a,可能存在满足定理的x,这时 ...

  2. 洛谷 P3803 多项式乘法

    题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: 第一行2个正整数n,m. 接下来一行n+1 ...

  3. POJ 3068 "Shortest" pair of paths(费用流)

    [题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...

  4. 【莫队算法】【权值分块】bzoj3809 Gty的二逼妹子序列

    如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; int ...

  5. python3开发进阶-Django框架的详解

    一.MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分: 模型(Model).视图(View)和控制器(Con ...

  6. Word中对象显示不完整

    选中上下文字后,右键没有段落,如果是图片的话是有的,那么我们可以点击菜单栏中段落的右下三角,在那设置单倍行距.

  7. ASP.NET MVC生命周期介绍(转)

    本文以IIS7中asp.net应用程序生命周期为例,介绍了asp.net mvc的生命周期. asp.net应用程序管道处理用户请求时特别强调"时机",对asp.net生命周期的了 ...

  8. nodeJs+socket.io

    1.先安装npm和node 2.安装socket.io npm install socket.io 3.html <!DOCTYPE html> <html lang="e ...

  9. linux过滤ip地址

    一.系统版本 [root@zabbix-server tmp]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 二.用awk ...

  10. jbpm4(参数设置)

    1.processDefinition.getDescription() <process name="task_test_2" xmlns="http://jbp ...