dubbo服务化使用实例
来源:互联网 发布:福缘充值软件 编辑:程序博客网 时间:2024/06/07 03:12
Dubbo的节点角色官方说明:
Provider: 暴露服务的服务提供方
Consumer: 调用远程服务的服务消费方
Registry: 服务注册与发现的注册中心
Monitor: 统计服务的调用次调和调用时间的监控中心
Container: 服务运行容器
Dubbo调用关系图:
Dubbo调用关系说明:
0: 服务容器负责启动,加载,运行服务提供者。
1:服务提供者在启动时,向注册中心注册自己提供的服务。
2:服务消费者在启动时,向注册中心订阅自己所需的服务。
3:注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4:服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5:服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
#################实例使用###############
(1) 定义一个远程服务接口
(2) provider发布远程服务到注册中心
(3) consumer自动发现远程服务并完成服务调用
一 定义一个远程服务接口
该接口需单独打包,在服务提供方和消费方共享。
eg:
package wusc.edu.facade.user.service;/** * 定义一个远程服务接口,该接口需单独打包,在服务提供方和消费方共享 */public interface DemoService {String sayHello(String name);}二 Provider实现
服务提供方实现接口,其实现对与服务消费方是隐藏的。
eg:
package wusc.edu.facade.user.service.impl;import wusc.edu.facade.user.service.DemoService;/** * 服务提供方实现接口,其实现对与服务消费方是隐藏的。 */public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; }}
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:application name="gw-service-user" /><!-- 使用zookeeper注册中心暴露服务地址 --><dubbo:registry protocol="zookeeper" address="192.168.106.128:2181" /><!-- 用dubbo协议在20880端口暴露服务 --><dubbo:protocol name="dubbo" port="20880" /><!-- 用户服务接口 --><dubbo:service interface="wusc.edu.facade.user.service.DemoService" ref="demoService" /></beans>加载spring的配置文件:
package wusc.edu.test;import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-provider.xml"); context.start(); System.in.read(); // 按任意键退出 }}
dubbo-admin管控台服务,如果消费者未启动时,会显示无消费者。
提供者:
三 Consumer实现
通过spring配置引用远程服务,dubbo-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="edu-web-boss" /><!-- 使用zookeeper注册中心暴露服务地址 --><!-- 注册中心地址 --><dubbo:registry protocol="zookeeper" address="192.168.106.128:2181" /><!-- 用户服务接口 --><dubbo:reference interface="wusc.edu.facade.user.service.DemoService" id="demoService" check="false" /></beans>加载spring配置,并调用远程服务:
package wusc.edu.web.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import wusc.edu.facade.user.service.DemoService;public class Consumer {public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/spring-consumer.xml"}); context.start(); DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理 String hello = demoService.sayHello("world"); // 执行远程方法 System.out.println( hello ); // 显示调用结果 }}消费者:
阅读全文
0 0
- dubbo服务化使用实例
- dubbo rpc服务使用
- Dubbo > Dubbo服务化改造
- dubbo 服务化
- 分布式服务框架Dubbo入门实例
- 分布式服务框架Dubbo入门实例
- dubbo 序列化实例
- spring+zookeeper+dubbo使用实例
- dubbo分布式服务化框架的使用经验
- dubbo实例 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
- dubbo 使用 学习二(spring+dubbo+zookeeper单机服务)
- dubbo服务化实施整理
- SOA 服务化 服务治理 dubbo
- 分布式服务框架Dubbo的使用
- 分布式服务框架Dubbo使用小结
- Dubbo分布式服务框架的使用
- 使用dubbo的main方法启动服务
- 使用dubbo时服务被禁止原因
- 【Redis缓存机制】9.快照持久化和AOF持久化
- 二叉树序列化和反序列化
- 【Redis缓存机制】10.主从模式
- HDU
- 设置div根据浏览器自适应高度
- dubbo服务化使用实例
- 我是一个进程
- instanceof与typeof(写一个方法判断数据类型),prototype与_proto_
- 你确定了解连续赋值运算嘛?
- 代码大全
- js的一道经典题目
- js知识整理(一)
- React Native按需加载 手Q狼人杀探索之路
- 我是一个线程