1.安装Zookeeper

  a.下载Zookeeper后解压

  b.进入根目录下的conf文件夹,将zoo_sample.cfg改成bak文件,并复制一个修改为zoo.cfg,修改相关配置

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#此处设置data和log的路径
dataDir=D:/JavaTools/zookeeper/server1/zookeeper-3.4.11/zooData
dataLogDir=D:/JavaTools/zookeeper/server1/zookeeper-3.4.11/zooLog
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

  c.进入bin目录,双击zkServer.cmd启动Zookeeper(关闭窗口则停止)

2.创建服务端

  a.导入依赖

  <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

  b.创建服务接口

public interface DubboServer {
public String hello(String name);
}

  c.创建接口实现类

public class DubboServerImp implements DubboServer {
@Override
public String hello(String name) {
return "hello"+name;
}
}

  d.创建spring-dubbo-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管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="user_provider"/>
<!-- 使用zookeeper注册中心注册服务 -->
<dubbo:registry address="zookeeper://localhost:2181"/> <dubbo:protocol name="dubbo" port="20880"/> <!-- <dubbo:monitor protocol="registry"/> -->
<!-- 暴露的服务接口 -->
<dubbo:service interface="com.wode.server.DubboServer"
ref="dubboServer" timeout="5000" retries="2" /> <bean id="dubboServer" class="com.wode.server.imp.DubboServerImp" /> </beans>

3.创建客户端

  a.引入相同的依赖

    <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

  b.复制粘贴服务端的接口

public interface DubboServer {
public String hello(String name);
}

  c.创建spring-dubbo-client.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="user_consumer" /> <dubbo:registry address="zookeeper://localhost:2181"/>
<!-- <dubbo:monitor protocol="registry"/> --> <!-- 注册中心配置 -->
<dubbo:reference id="dubboServer" interface="com.wode.server.DubboServer" timeout="10000" check="false" /> </beans>

  d.调用服务

@Controller
public class DubboTestController { @Resource
DubboServer server; //根据查询信息
@RequestMapping("/getMsg")
public @ResponseBody String getMsg(HttpSession session, HttpServletRequest request,HttpServletResponse response){
String keyWord = request.getParameter("keyWord");
//调用服务
String result = server.hello(keyWord);
return result;
} }

注意:服务端和客户端的接口名称务必保持一致,连包名都要一致,不然会报Please check registry access list (whitelist/blacklist)的异常

RPC远程调用——Dubbo的更多相关文章

  1. 測试JSON RPC远程调用(JSONclient)

    #include <string> #include <iostream> #include <curl/curl.h> /* 标题:JSonclient Auth ...

  2. 使用Socket&反射&Java流操作进行方法的远程调用(模拟RPC远程调用)

    写在前面 阅读本文首先得具备基本的Socket.反射.Java流操作的基本API使用知识:否则本文你可能看不懂... 服务端的端口监听 进行远程调用,那就必须得有客户端和服务端.服务端负责提供服务,客 ...

  3. 从0到1:全面理解RPC远程调用

    上一篇关于 WSGI 的硬核长文,不知道有多少同学,能够从头看到尾的,不管你们有没有看得很过瘾,反正我是写得很爽,总有一种将一样知识吃透了的错觉. 今天我又给自己挖坑了,打算将 rpc 远程调用的知识 ...

  4. Openstack Nova 源码分析 — RPC 远程调用过程

    目录 目录 Nova Project Services Project 的程序入口 setuppy Nova中RPC远程过程调用 nova-compute RPC API的实现 novacompute ...

  5. dubbo集成zookeeper rpc远程调用

    注:下面使用dubbo依赖的是zookeeper注册中心,这里没有详细的介绍.在配置之前,请自行准备好zookeeper环境. 后续如果写zookeeper的配置会补放链接 添加Gradle依赖 co ...

  6. rpc远程调用开发

    RPC即远程过程调用,适用于集群管理,集群节点就是RPCServer,而我们发起远程调用的web服务器就是RPCClient.所以是少数rpcClient(可能一个)对多个RPCServer(集群节点 ...

  7. Docker+Dubbo+Zookeeper实现RPC远程调用

    Docker+Dubbo+Zookeeper 1.安装Docker 1.1卸载旧版本的Docker //如果Docker处于与运行状态 未运行可跳过 [root@MrADiao ~]# systemc ...

  8. 详解RPC远程调用和消息队列MQ的区别

    PC(Remote Procedure Call)远程过程调用,主要解决远程通信间的问题,不需要了解底层网络的通信机制. RPC框架 知名度较高的有Thrift(FB的).dubbo(阿里的). RP ...

  9. go语言net包rpc远程调用的使用

    一.基于http的RPC 服务端: package main; import ( "net/rpc" "net/http" "log" ) ...

随机推荐

  1. 帝国cms 不能正常显示最新文章

    后台能正常刷新,但前台就是不能正常显示, 把网站从c盘换到d盘,好了,原来是权限的问题

  2. <Android基础>(三) UI开发 Part 2 ListView

    ListView 1)ListView的简单用法 2)定制ListView界面 3)提升ListView的运行效率 4)ListView的点击事件 3.5 ListView 3.5.1 ListVie ...

  3. Python网络编程(3)——SocketServer模块与简单并发服务器

    主要类型 该模块有四个比较主要的类,其中常用的是 TCPServer 和 UDPServer. 1. TCPServer 2. UDPServer 3. UnixStreamServer,类似于TCP ...

  4. linux虚拟机 在install yum时提示无法获得锁 var/lib/dekg/lock时该如何解决?

    起因 在新虚拟机中使用yum命令时提示要安装,安装过程中提示出错. 问题 在sudo apt install yum时提示下列错误该如何解决? E: 无法获得锁 /var/lib/dpkg/lock ...

  5. pgsql 并行相关配置

  6. Java9 接口细谈

    java9对接口进行了改进,允许在接口中定义默认方法和类方法并且都支持方法的实现.同时添加了一种私有方法,私有方法也可提供方法实现. 注:下面语法只有在Java8以上的版本才允许在接口定义默认方法.类 ...

  7. 2017-12-19python全栈9期第四天第二节之列表的增删改查之切片

    #!/user/bin/python# -*- coding:utf-8 -*-li = ['zd',[1,3,4,5,6],'ls','ww','zl']l1 = li[0]print(l1)l2 ...

  8. 金融量化分析【day112】:量化平台的使用-下单函数

    order - 按股数下单 1.代码 # 导入函数库 import jqdata #初始化函数,设定基准等等 def initialize(context): set_benchmark('00030 ...

  9. Regularity criteria for NSE 6: $u_3,\p_3u_1,\p_3u_2$

    In [Zujin Zhang, Jinlu Li, Zheng-an Yao, A remark on the global regularity criterion for the 3D Navi ...

  10. Windows系统盘符错乱导致桌面无法加载。

    问题如下 : 同事有台笔记本更换SSD硬盘,IT职员帮他将新硬盘分好区后再将系统完整Ghost过来,然后装到笔记本上.理论上直接就可以使用了!但结果开机后登陆用户桌面无法显示,屏幕黑屏什么都没有. 问 ...