注:下面使用dubbo依赖的是zookeeper注册中心,这里没有详细的介绍。在配置之前,请自行准备好zookeeper环境。

    后续如果写zookeeper的配置会补放链接

添加Gradle依赖

    compile group: 'com.alibaba', name: 'dubbo', version: '2.5.10'//dubbo
compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.3.3'//zookeeper
compile group: 'com.github.sgroschupf', name: 'zkclient', version: '0.1'//zkclient

服务端provider

目录结构

实体类

//这里实体对象实现了Serializable接口,dubbo规定,在远程调用实体对象时必须要实现Serializable接口以保证实体对象能够被序列化,如不实现会报错
public class Student implements Serializable {
private String name;
private int age; public Student(String name, int age) {
this.name = name;
this.age = age;
} public Student() {
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}

service方法

public interface TestService {

    //返回字符串测试
String hello(String word);
//返回实体对象测试,注意实体类要实现 Serializable 接口
List<Student> selectAllStudent(); }

实现service方法

//给service起个名字 别人调用提供接口方法时就是来实现本实现类的方法,和xml配置文件中所对应,让spring IOC 注入所管理
@Service("testService")
public class TestServiceImpl implements TestService {
@Autowired
private GoodsStoreMapper goodsStoreMapper; @Override
public String hello(String word) {
return "提供者:"+word;
} @Override
public List<Student> selectAllStudent() {
System.out.println("------被调用了------");
List<Student> list = new ArrayList<Student>();
list.add(new Student("张三" , 1));
list.add(new Student("李四" , 2));
list.add(new Student("王五" , 3));
return list;
} }

applicationContext.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"> <!-- 提供方应用信息,用于计算依赖关系-->
<!--name给当前服务起一个名字-->
<dubbo:application name="springBootProvider"></dubbo:application>
<!--protocol指定注册中心类型 这里用的是zookeeper-->
<!--address注册中心地址 ip地址端口号-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务-->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口-->
<dubbo:service interface="cn.appsys.service.TestService" ref="testService" /> </beans>

启动类

@SpringBootApplication
//读取配置文件
@ImportResource(locations = {"classpath:config/applicationContext.xml"})
public class Start
{
public static void main(String[] args) {
SpringApplication.run(Start.class , args);
}
}

运行服务端,首先先启动zookeeper注册中心

启动启动类,注意zookeeper的变化

  这里看到 在配置文件中外放的接口已经被注册进来了,调用方想调用就要通过此节点来调用服务。

客户端customer

目录结构

  客户端 实体 方法都和服务端的保持一致

  注意:方法所在的包也要和服务端保持一致,接口名都需要和服务端保持一致

applicationContext.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="springBootConsumer"></dubbo:application>
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!--包名 接口名 必须要和服务提供方保持一致-->
<dubbo:reference id="testService" interface="cn.appsys.service.TestService" ></dubbo:reference> </beans>

启动测试类

@SpringBootApplication
//读取配置文件
@ImportResource(locations = {"classpath:config/applicationContext.xml"})
public class Start
{
public static void main(String[] args)
{
//SpringApplication.run返回一个ApplicationContext对象
ApplicationContext ac = SpringApplication.run(Start.class , args);
//通过ApplicationContext对象的getBean获取实现方法
TestService testService = ac.getBean(TestService.class);
//调用方法
String s = testService.hello("aaaaaaaa");
System.out.println(s); List<Student> stus = testService.selectAllStudent();
for (Student stu : stus)
{
System.out.println(stu.getName());
}
}
}

启动运行

  调用成功,再看一下服务端的变化

  成功!

这里在放上如果不在服务端实体类不实现Serializable接口的报错信息

Exception in thread "main" com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method selectAllStudent in the service cn.appsys.service.TestService. Tried 3 times of the providers [192.168.1.104:20880] (1/1) from the registry 127.0.0.1:2181 on the consumer 192.168.1.104 using the dubbo version 2.0.1. Last error is: Failed to invoke remote method: selectAllStudent, provider: dubbo://192.168.1.104:20880/cn.appsys.service.TestService?anyhost=true&application=springBootConsumer&check=false&dubbo=2.0.1&generic=false&interface=cn.appsys.service.TestService&methods=hello,login,selectAllStudent&pid=8928&register.ip=192.168.1.104&remote.timestamp=1533373429375&side=consumer&timestamp=1533373564236, cause: Failed to send response: Response [id=3, version=2.0.0, status=20, event=false, error=null, result=RpcResult [result=[cn.appsys.entity.Student@45a35c25, cn.appsys.entity.Student@419cd049, cn.appsys.entity.Student@49e561e], exception=null]], cause: java.lang.IllegalStateException: Serialized class cn.appsys.entity.Student must implement java.io.Serializable
java.lang.IllegalStateException: Serialized class cn.appsys.entity.Student must implement java.io.Serializable

dubbo在远程调用时注意事项

  1)序列化
    我们所有需要用来传输数据的实体一定要实现序列化,不然一定会报错
  2)业务注入不进来
    例如我们在Controller中注入了一个业务,@Controller使用的是Spring注解,@Reference使用的是Dubbo,如果Spring先进行扫描,那么业务一定是注入不进去的。如所有我们dubbo也要扫描controller。
  3)超时设置
    根据业务来设定不同的超时配置,如果一个服务很庞大处理的时间相对来说时间会比较长,可以会一直引起超时错误。

dubbo集成zookeeper rpc远程调用的更多相关文章

  1. ZooKeeper伪分布集群安装及使用 RMI+ZooKeeper实现远程调用框架

    使用 RMI + ZooKeeper 实现远程调用框架,包括ZooKeeper伪集群安装和代码实现两部分.  一.ZooKeeper伪集群安装: 1>获取ZooKeeper安装包 下载地址:ht ...

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

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

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

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

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

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

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

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

  6. rpc远程调用开发

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

  7. 使用 RMI + ZooKeeper 实现远程调用框架

    目录[-] 1 发布 RMI 服务1.1 定义一个 RMI 接口1.2 编写 RMI 接口的实现类1.3 通过 JNDI 发布 RMI 服务2 调用 RMI 服务3 RMI 服务的局限性4 使用 Zo ...

  8. Spring集成RMI实现远程调用

    前提: 1.开发工具: jdk tomcat ecplise,开发工具的使用本篇不做介绍. 2.需具备以下知识:javase servelt web rmi spring maven 一.关于RMI ...

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

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

随机推荐

  1. js控制input框输入数字时,累计求和

    input框输入数字时,自动开始计算累加 <div class="form-group"> <label for="inputPassword3&quo ...

  2. 自己制作一个简单的操作系统二[CherryOS]

    自己制作一个简单的操作系统二[CherryOS] 我的上一篇博客 自己制作一个简单的操作系统一[环境搭建], 详细介绍了制作所需的前期准备工作 一. 一点说明 这个操作系统只是第一步, 仅仅是开机显示 ...

  3. tomcat下配置jndi数据源c3p0

    Tomcat下通过JNDI配置数据源,使用c3p0连接池 首先在打开tomcat找到在conf文件下,找到server.xml 在server.xml文件中找到标签 在下面添加如下配置 <Res ...

  4. Centos 7 配置tomcat服务器

    1.首先查看当前系统版本 uname -a 2.安装之前查看系统是否安装了java rpm -qa |grep java rpm -qa |grep jdk rpm -qa |grep gcj 如果没 ...

  5. 使用JSP+Servlet+Jdbc+Echatrs实现对豆瓣电影Top250的展示

    使用JSP+Servlet+Jdbc+Echatrs实现对豆瓣电影Top250的展示 写在前面: 有的小伙伴,会吐槽啦,你这个标题有点长的啊.哈哈 ,好像是的!不过,这个也是本次案例中使用到的关键技术 ...

  6. js数组的增删改查

    array 数组的增删改: push 从数组后面推入一个元素或多个元素 var arr = [1,2,3]; ​ // 返回:修改后数组的长度 arr.push(4,5,6); pop 删除数组最后一 ...

  7. 【SQL server基础】SQL视图加密,永久隐藏视图定义的文本

    SQL可以对视图进行加密.也就是,可永久隐藏视图定义的文本. 注意   此操作不可逆.加密视图后,无法再修改它,因为无法再看到视图定义.如果需要修改加密视图,则必须删除它并重新创建另一个视图. 示例代 ...

  8. GitHub项目徽标

    前言 GitHub徽标,GitHub Badge,你也可以叫它徽章.就是在项目README中经常看到的那些表明构建状态或者版本等信息的小图标.就像这样: 这些好看的小图标不仅简洁美观,而且包含了清晰易 ...

  9. Kubernetes 系列(七):持久化存储StorageClass

    前面的课程中我们学习了 PV 和 PVC 的使用方法,但是前面的 PV 都是静态的,什么意思?就是我要使用的一个 PVC 的话就必须手动去创建一个 PV,我们也说过这种方式在很大程度上并不能满足我们的 ...

  10. git 查看日志记录

    1.git log 如果日志特别多的话,在git bash中,按向下键来查看更多,按q键退出查看日志. 2.git show 查看最近一次commit内容,也可以后面加commit号,单独查看此次版本 ...