您的位置:首页 > 其它

dubbo学习教程(一):完成入门第一个hello world程序【基于zookeeper】

2016-03-15 15:59 926 查看
一、为何使用dubbo服务来实现功能开发?

随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。

单一应用架构
当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键。
垂直应用架构
当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
此时,用于加速前端页面开发的 Web框架(MVC) 是关键。
分布式服务架构
当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。
流动计算架构
当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
此时,用于提高机器利用率的 资源调度和治理中心(SOA) 是关键。

更多关于dubbo相关介绍,请详见dubbo官方网站http://dubbo.io/Home-zh.htm


二、hello world程序的创建过程:

1、

首先下载zookeeper服务器,因为这是注册中心,客户端和服务端都需要基于zookeeper开发。(如何下载百度一下就好了http://jingyan.baidu.com/article/6181c3e094266d152ef153da.html

2、zookeeper的安装及启动过程:

下载成功后解压到当前目录,然后复制conf文件夹下的zoo_sample.cfg并改名为zoo.cfg,里边的配置默认就好了,如果修改的话,根据个人需求而定。

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance #
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1


在bin文件夹下点击 zkServer.cmd 启动zookeeper,在命令窗口中没有报错,到这启动zookeeper服务成功。

3、服务端:

Myeclipse中创建一个web项目,在src下创建一个服务端接口:


package demo1;

public interface DemoService {
String sayHello(String name);
}


服务端实现类:

package demo1_impl;

import demo1.DemoService;

public class DemoServiceImpl implements DemoService{

@Override
public String sayHello(String name) {
// TODO Auto-generated method stub
return "hello,dubbo; " + name;

}

}


src下创建一个application.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframewo
c4f3
rk.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 ">

<!-- 具体的实现bean -->
<bean id="demoService"
class="demo1_impl.DemoServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo_demo1"/>
<!-- 使用multicast广播注册中心暴露服务地址
<dubbo:registry address="multicast://224.5.6.7:1234" />-->
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="demo1.DemoService"
ref="demoService" />
</beans>


创建一个启动类

package startDemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
context.start();

System.out.println("Start-------------------liupeng");

System.in.read(); //按任意键停止
}
}


执行启动类,后台出现打印效果即为服务端启动服务成功。

4、客户端:

同服务端一样创建一个接口 ,包名和服务端一样;

package demo1;

public interface DemoService {
String sayHello(String name);
}


src下创建一个application.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 ">
<!-- consumer application name -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- registry address, used for consumer to discover services -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:consumer timeout="5000" />
<!-- which service to consume? -->
<dubbo:reference id="demoService"
interface="demo1.DemoService" />
</beans>


书写一个测试类,来调用服务端的方法

package test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo1.DemoService;

public class TestDemo {

public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

DemoService demoService = (DemoService) context.getBean("demoService");

String hello = "";

try {
hello = demoService.sayHello("  dubbo测试程序:  i love qiao !!" );
} catch (Exception e) {
e.printStackTrace();

System.out.print("调取zookeeper服务出错!");

}
System.out.println(hello);
}

}


此时,后台正常输出以下效果即为成功。

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
hello,dubbo;   dubbo测试程序:  i love qiao !!


注意:如果出现地址端口被占用的错误,请查看myeclipse中debug窗口是不是有程序正在执行。

补充:所需要的jar包:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: