dubbo结合zookeeper的使用

dubbo是分布式管理框架,主要用于管理分布式系统.zookeeper是分布式协调的治理工具。

dubbo是什么?

一款分布式服务框架高性能和透明化的RPC远程服务调用方案SOA服务治理方案

dubbo架构图

Provider:服务提供方

Consumer:服务消费者

Registry:注册中心

Monitor:统计服务调用次数和调用时间的监控中心

调用流程:

0 . 启动服务

1. 服务提供者启动的时候, 向注册中心提供自己的服务

2.服务消费者启动的时候,向注册中心订阅自己的服务

3.注册中心将服务提供者地址列表返回给服务消费者,如果有变更,注册中心将基于长变更变更数据返回给消费者

4.服务消费者,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选择另外一台服务提供者进行调用.

5.服务消费者和服务提供者, 在内容中累计调用次数和调用时间, 定时每分钟发送一次统计数据到监控台中心.

zookeeper注册中心

上述途中所或的Register就是注册中心,dubbo官方推荐的注册中心是zookeeper

服务提供方: 在注册中心发布服务

服务消费方: 在注册中心订阅服务

dubbo+zookeeper+SSM框架的配合使用

1.创建maven工程

dubbo : dubbo父类工程, 主要是用来管理所有的jar版本

dubbo-api : dubbo工程所有的接口

dubbo-consumer : dubbo 消费者

dubbo-provider : dubbo提供者 , 所有公共接口的实现

dubbo-consumer-user : dubbo 消费用户

2. 创建服务方

2.1 服务方接口


package com.provider; public interface DemoService { String sayHello(String name);}

2.2服务方接口实现类


package com.provider; import org.springframework.stereotype.Service; @Service(value="demoService")public class DemoServiceImpl implements DemoService{ public String sayHello(String name) { return "Hello Dubbo,Hello " + name; } }

2.3 applicationContext.xml


<beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" > <component-scan> <import> /<beans>

2.4 dubbo-provider.xml


<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"> <application> <registry> <protocal> /<protocal> /<beans>