Dubbo初体验:远程服务调用和管控台的搭建
2017-09-06 15:49
501 查看
Dubbo是一个阿里巴巴的分布式服务框架。虽然在很久以前阿里停止更新了,但是还是有很多公司在使用它。Dubbo致力于提供高性能和透明化的RPC远程服务调用方案以及SOA服务治理方案。通过他我们可以非常容易地通过Dubbo来构建分布式服务。哦,对了。好像最近阿里又开始更新了。
Dubbo的架构
调用关系说明:
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
这张图和这段话你可能已经见过无数次了,但是我还是把它从官网复制了过来。原因就是这张图能很简洁的说么Dubbo的调用关系。如果你还没有使用过Dubbo,那这张图可以很快帮你理顺调用关系。
Hello Dubbo
我们在使用过程中一般是采用XML配置,原因就是简单方便。在使用之前我们需要搭建Dubbo的注册中心。官方推荐的注册中心是zookeeper。如果你还没有搭建好zookeeper,那么可以看看我之前写的小白从头到脚搭建zookeeper集群的过程。1.Dubbo依赖
如果你使用的是Maven项目,在使用Dubbo的地方加入下面的依赖即可:
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency>
2.生产者
首先展示生产者的接口,很简单的两个方法。
public interface SampleService { String sayHello(String name); }
生产者的实现类:
public class SampleServiceImpl implements SampleService { public String sayHello(String name) { return "Hello " + name; } }
User类的话就是name、age、sex三个属性。
接下来就是生产者的Spring配置:
<?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 "> <!-- 具体的实现bean --> <bean id="sampleService" class="top.catalinali.sample.provider.impl.SampleServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="sample-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.71.121:2181?backup=192.168.71.122:2181,192.168.71.123:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 写操作可以设置retries=0 避免重复调用SOA服务 --> <dubbo:service retries="0" interface="top.catalinali.sample.provider.SampleService" ref="sampleService" /> </beans>
3.消费者
首先消费者需要一个跟生产一模一样的接口类
public interface SampleService { String sayHello(String name); }
消费者配置
<?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="sample-consumer" /> <dubbo:registry address="zookeeper://192.168.71.121:2181?backup=192.168.71.122:2181,192.168.71.123:2181" /> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 --> <dubbo:reference id="sampleService" check="false" interface="top.catalinali.sample.provider.SampleService" /> </beans>
Dubbo启动
首先我们先启动生产者启动:public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "sample-provider.xml" }); context.start(); System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟 } }
启动消费者
public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "sample-consumer.xml" }); context.start(); SampleService sampleService = (SampleService) context.getBean("sampleService"); String hello = sampleService.sayHello("tom"); System.out.println(hello); System.in.read(); } }
结果
项目结构图
Dubbo的管控台
Dubbo管控台搭建其实很容易,我们只需要一个在linux上的tomcat跑Dubbo即可,需要的环境:apache-tomcat-7.0.29.tar.gz
dubbo-admin-2.5.4.war
1.安装tomcat
使用
tar -zxvf apache-tomcat-7.0.29.tar.gz解压tar包
2.解压dubbo的war包
使用
unzip dubbo-admin-2.5.3.war -d dubbo-admin解压dubbo-admin-2.5.4.war到tomcat目录下的webapps下。如果提示无unzip命令,使用yum安装
yum install -y unzip zip此命令
注意:
如果zookeeper注册中心不在此服务器上,则需要打开dubbo-admin/WEB-INF/dubbo.properties文件,将红框改注册中心的ip地址。
3.运行tomcat
①进入tomcat下的bin目录下,执行
./startup.sh脚本。
②使用
tail -f ../logs/catalina.out查看日志。
③进入http://192.168.71.121:8080/dubbo-admin/就可以看到管控台了(IP地址改成你的服务器ip)
也可以看到我们刚才所运行的服务
本文作者: catalinaLi
本文链接: http://catalinali.top/2017/helloDubbo/
版权声明: 原创文章,有问题请评论中留言。非商业转载请注明作者及出处。
相关文章推荐
- 使用cxf框架搭建基于Spring远程调用Web服务
- spring cloud搭建微服务second-fiberhome(三):consul结合config以及bus实现配置文件通过RabbitMQ动态调用
- SpringCloud : 两个微服务进程之间通信(远程调用)
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 搭建PXE网络实现远程装机服务
- 远程调用服务(RPC)和消息(Message Queue)对比及其适用/不适用场合
- 分布式服务架构之java远程调用技术浅析
- 一步步学会Thrift服务搭建和调用
- Spring Cloud Feign作为HTTP客户端调用远程HTTP服务,feign熔断器
- Netty4 + Thrift压缩二进制编解码 10W TPS(1K的复杂POJO对象)的跨节点远程服务调用
- java远程调用中的RMI构建远程服务
- Android系统篇之----Binder机制和远程服务调用机制分析
- 远程调用服务(RPC)和消息(Message Queue)对比及其适用/不适用场合
- tomcat+webservice实现简单的web服务远程调用接口
- 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景
- 2017年2月20日Dubbo远程调用服务框架部署
- 基于CenOS搭建VNC远程桌面服务
- QC远程调用QTP出现RPC服务不可用问题的解决思路
- SpringCloud(第 012 篇)电影微服务接入 Feign 进行客户端负载均衡,通过 FeignClient 调用远程 Http 微服务
- Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用