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 ); // 显示调用结果    }}
消费者:

原创粉丝点击