ITKeyword - 技术文章推荐分享

首页 > 阿里巴巴 Dubbo框架demo

阿里巴巴 Dubbo框架demo

标签: zookeeper, Dubbo,

相关推荐:阿里巴巴分布式SOA框架Dubbo简介

一、 Dubbo背景介绍随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,需一个治理系统确保架构有条不紊的演进 单一应用架构当网站流量很小时,只需一个应用,将所有功能都部署在一起

三个module: service:定义接口 provider:定义接口实现 consumer:模拟接口调用

1:service只是象征性的定义了一个接口: package com.tch.test.dubbo.service;public interface DemoService {

String sayHello(String name);}

2:provider: pom.xml:

<dependency>

<groupId>com.tch.test.dubbo</groupId>

<artifactId>service</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>dubbo</artifactId>

<version>2.8.4</version>

</dependency>

<dependency>

<groupId>com.101tec</groupId>

<artifactId>zkclient</artifactId>

<version>0.7</version>

</dependency>

还有一个接口的简单实现: package com.tch.test.dubbo.provider.serviceimpl;import com.tch.test.dubbo.service.DemoService;public class DemoServiceImpl implements DemoService {

public String sayHello(String name) {

return "Hello " + name;

}}

然后是spring配置文件applicationContext.xml,里面保护了dubbo的配置,我这里使用到了本地localhost的zookeeper,端口是默认的2181,如果不用zookeeper的话,直接将 <dubbo:registry address="zookeeper://localhost:2181" check="false"></dubbo:registry> 这句话注释掉,将 <dubbo:registry address="multicast://224.5.6.7:1234" /> 放开即可。至于为什么建议zookeeper部署奇数台,是因为根据zookeeper的leader选举算法,由于zookeeper挂掉一半的机器集群就不可用,所以部署4台和3台的集群都是在挂掉2台后集群不可用,所以使用奇数台机器,不浪费资源 : <?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:registry address="zookeeper://localhost:2181" check="false"></dubbo:registry> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.tch.test.dubbo.service.DemoService"

ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.tch.test.dubbo.provider.serviceimpl.DemoServiceImpl" /></beans>

然后是provider的测试类: package com.tch.test.dubbo.provider.test;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context = null;

try {

context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });

context.start();

context.registerShutdownHook();

System.in.read();

} catch (Exception e) {

e.printStackTrace();

}finally{

context.close();

}

}}

3:consumer: pom.xml和provider一样:

<dependency>

<groupId>com.tch.test.dubbo</groupId>

<artifactId>service</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>dubbo</artifactId>

<version>2.8.4</version>

</dependency>

<dependency>

<groupId>com.101tec</groupId>

<artifactId>zkclient</artifactId>

<version>0.7</version>

</dependency>

然后是spring配置文件applicationContext.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" /> --> <dubbo:registry address="zookeeper://localhost:2181" check="false"></dubbo:registry> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService"

interface="com.tch.test.dubbo.service.DemoService" /&g

相关推荐:Dubbo框架结合Zookeeper注册中心使用初探

【参考文章】http://www.cnblogs.com/Javame/p/3632473.html (灰常感谢这篇启蒙文章!!)【文章有错,修改见删除线处。。】一、基本概念【Dubbo】Dubbo是一个来源于阿里巴巴的分布式服务框架,本质上是一个服务调用的东西,主要在分布式

t;</beans>

最后是consumer的测试类: package com.tch.test.dubbo.consumer.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.tch.test.dubbo.service.DemoService;public class Consumer {

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context = null;

try {

context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });

context.start();

context.registerShutdownHook();

DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理

String hello = demoService.sayHello("world"); // 执行远程方法

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

context.stop();

} catch (Exception e) {

e.printStackTrace();

}finally{

if(context != null){

context.close();

}

}

}}

接下来,先运行Provider.java,在运行Consumer.java即可看到调用结果(当前前提是zookeeper是启动的啦): Hello world

关于zookeeper client的简单demo: 添加依赖:

<dependency>

<groupId>com.101tec</groupId>

<artifactId>zkclient</artifactId>

<version>0.7</version>

</dependency>

一方面对zookeeper进行修改等操作,一方面进行监测: package com.tch.zookeeper.test;import java.util.UUID;import org.I0Itec.zkclient.ZkClient;import org.apache.zookeeper.CreateMode;public class ZookeeperOperator {

private ZkClient zkClient;

public ZkClient getZkClient() {

return zkClient;

}

public void setZkClient(ZkClient zkClient) {

this.zkClient = zkClient;

}

/**

* 函数入口

* @param args

*/

public static void main( String[] args ) {

ZookeeperOperator bootStrap=new ZookeeperOperator();

bootStrap.initialize();

try {

Thread.sleep(100000000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

/**

* 初始化zookeeper

*/

public void initialize() {

String connectionString="localhost:2181";

int connectionTimeout=50000;

zkClient=new ZkClient(connectionString, connectionTimeout);

if(!zkClient.exists("/root1")) {

zkClient.create("/root1", new Long(System.currentTimeMillis()), CreateMode.EPHEMERAL);

}

new Thread(new RootNodeChangeThread()).start();

}

/**

* 每20s改变一次 'root1'节点的数据

* @author yang

*

*/

private class RootNodeChangeThread implements Runnable{

public void run() {

while(true) {

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

//ignore

}

String uuidStr=UUID.randomUUID().toString();

System.out.println(">>>>>>>>>> 产生随机的 uuid string,'uuidStr'===>"+uuidStr);

zkClient.writeData("/root1", uuidStr);

}

}

}}

package com.tch.zookeeper.test.slave;import org.I0Itec.zkclient.IZkDataListener;import org.I0Itec.zkclient.ZkClient;public class ZookeeperMonitor {

private ZkClient zkClient;

public ZkClient getZkClient() {

return zkClient;

}

public void setZkClient(ZkClient zkClient) {

this.zkClient = zkClient;

}

/**

* 初始化zookeeper

*/

public void initialize() {

String connectionString="localhost:2181";

int connectionTimeout=500000;

zkClient=new ZkClient(connectionString, connectionTimeout);

new Thread(new Runnable() {

public void run() {

zkClient.subscribeDataChanges("/root1", new IZkDataListener() {

public void handleDataDeleted(String dataPath) throws Exception {

System.out.println("the node 'dataPath'===>");

}

public void handleDataChange(String dataPath, Object data) throws Exception {

System.out.println("the node 'dataPath'===>"+dataPath+", data has changed.it's data is "+String.valueOf(data));

}

});

}

}).start();

}

/**

* 函数入口

* @param args

*/

public static void main( String[] args ) {

ZookeeperMonitor bootStrap=new ZookeeperMonitor();

bootStrap.initialize();

try {

Thread.sleep(100000000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}} 通过运行结果即可明白 

相关推荐:阿里巴巴Dubbo实现概要分析

1.Dubbo概述Dubbo是阿里巴巴开源出来的一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及作为SOA服务治理的方案。它的核心功能包括:#remoting:远程通讯基础,提供对多种NIO框架抽象封装,包括“同步转异步”和“请求-响应”模

??三个module:?service:定义接口?provider:定义接口实现?consumer:模拟接口调用??1:service只是象征性的定义了一个接口:?package com.tch.test.dubbo.service;...

------分隔线----------------------------