您的位置:首页 > 其它

dubbo+zookeeper实现服务远程调用

2017-01-04 21:45 417 查看

dubbo场景:

           随着网站应用规模的扩大,服务越来越多,服务间依赖关系变得错综复杂,服务的调用量越来越大,服务的容量问题暴露出来,dubbo在这种情况下诞生。

dubbo是什么:

           是一种分布式服务框架,解决上面面对的问题,先看dubbo架构图:



节点角色说明:

Provider: 暴露服务的服务提供方。

Consumer: 调用远程服务的服务消费方。

Registry: 服务注册与发现的注册中心。

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

Container: 服务运行容器。

调用关系说明:

0. 服务容器负责启动,加载,运行服务提供者。

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

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

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

4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

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

搭建zookeeper注册中心

搭建dubbo服务端,客户端,admin管理控制台(待整理)

创建及使用dubbo服务过程

dubbo提供者:

web工程pom文件下引入主要包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>

定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
package com.alibaba.dubbo.demo;

public interface DemoService {

String sayHello(String name);

}
在服务提供方实现接口:(对服务消费方隐藏实现)
package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService {

public String sayHello(String name) {
return "Hello " + name;
}

}
用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"> 
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app"  />

<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />

<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />

<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />

</beans>
加载Spring配置:
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://dubbo.io/provider.xml"});
context.start();

System.in.read(); // 按任意键退出
}

}


说明:

   dubbo:registry 标签一些属性的说明:

      1)register是否向此注册中心注册服务,如果设为false,将只订阅,不注册。

      2)check注册中心不存在时,是否报错。

      3)subscribe是否向此注册中心订阅服务,如果设为false,将只注册,不订阅。

      4)timeout注册中心请求超时时间(毫秒)。

      5)address可以Zookeeper集群配置,地址可以多个以逗号隔开等。

  dubbo:service标签的一些属性说明:

     1)interface服务接口的路径

     2)ref引用对应的实现类的Bean的ID

     3)registry向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A

     4)register 默认true ,该协议的服务是否注册到注册中心。

dubbo消费者

通过Spring配置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="consumer-of-helloworld-app" />

<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />

<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />

</beans>
加载Spring配置,并调用远程服务:(也可以使用IoC注入)
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.demo.DemoService;

public class Consumer {

public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://dubbo.io/consumer.xml"});
context.start();

DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("world"); // 执行远程方法

System.out.println( hello ); // 显示调用结果
}

}

说明:

   dubbo:reference 的一些属性的说明:

      1)interface调用的服务接口

      2)check 启动时检查提供者是否存在,true报错,false忽略

      3)registry 从指定注册中心注册获取服务列表,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔

      4)loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: