dubbo使用


这里是一篇写的很好的文章:

http://doc.okbase.net/congcong68/archive/112508.html

Dubbo与Zookeeper、SpringMVC整合使用

第一步:在Linux上安装Zookeeper

       Zookeeper作为Dubbo服务的注册中心Dubbo原先基于数据库的注册中心,没采用Zookeeper

Zookeeper一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心,Dubbo能与Zookeeper做到集群部署,

当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息

当提供者重启时,能自动恢复注册数据,以及订阅请求。


我们先在linux上安装Zookeeper,我们安装最简单的单点,集群比较麻烦。

    

(1)下载Zookeeper-3.4.6.tar.gz  地址http://www.apache.org/dist/zookeeper/

    

(2) 我们放到Linux下的一个文件夹,然后解压: 

      #tar zxvf zookeeper-3.4.6.tar.gz


3然后在对应的zookeeper-3.4.6/conf 下有一个文件

zoo_sample.cfg的这个文件里面配置了监听客户端连接的端口等一些信息,

Zookeeper 启动时会找zoo.cfg这个文件作为默认配置文件,所以我们

复制一个名称为zoo.cfg的文件,如图所示:


我们查看一下这个文件的里面的一些配置信息,如图所示:



说明:

 clientPort:监听客户端连接的端口。

 tickTime:基本事件单元,以毫秒为单位。它用来控制心跳和超时,默认情况下最小的会话超时时间为两倍的 tickTime

我们可以对配置文件的端口等或者进行高级配置和集群配置例如:

maxClientCnxns:限制连接 ZooKeeper 的客户端的数量等

(4)启动Zookeeper 的服务,如图所示:


到这边Zookeeper的安装和配置完成


第二步:配置dubbo-admin的管理页面,方便我们管理页面

(1)下载dubbo-admin-2.4.1.war包,在Linuxtomcat部署,

    先把dubbo-admin-2.4.1放在tomcatwebapps/ROOT下,然后进行解压:

    #jar -xvf dubbo-admin-2.4.1.war

(2)然后到webapps/ROOT/WEB-INF下,有一个dubbo.properties文件,里面指向Zookeeper ,

    使用的是Zookeeper 的注册中心,如图所示:



如果需要配置多个zooker集群:则只需要在上面的配置中:另起一行添加新的配置,如下:




 (3)然后启动tomcat服务,用户名和密码:root,并访问服务,显示登陆页面,说明dubbo-admin部署成功,如图所示:




  第三步:SpringMVC与Dubbo的整合,这边使用的Maven的管理项目

 第一:我们先开发服务注册的,就是提供服务,项目结构如图所示


(1)写接口:test-maven-api项目加入了一个服务接口,代码如下:

public interface TestRegistryService {
   public String hello(String name);
}
(2)添加jar包:test-maven-console在pom.xml

加入Dubbo和Zookeeper的jar包、引用test-maven-apijar包,

已经上传资源中

  (3)实现接口的服务:test-maven-console实现具体的服务,代码如下:

@Service("testRegistryService")//【<span style="font-size: 13.3333px; line-height: 1.1em;">这里的service可以是spring的注解,也可以是dubbo的注解</span><span style="font-size: 13.3333px; line-height: 1.1em;">】</span>
public class TestRegistryServiceImpl implements TestRegistryService {
	public String hello(String name) {	
		return "hello"+name;
	}
}
  (4)修改dubbo-provider.xml文件:把我们实现的服务对外暴露,代码如下:

<span style="font-size: 13.3333px;"><?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	<span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span>
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="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/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false" >
   <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
   <dubbo:application name="dubbo_provider"></dubbo:application>
  </span><strong><span style="font-size:14px;color:#3333ff;"> </span></strong>
<strong style="line-height: 1.1em;"><span style="color:#3333ff;"><span style="font-size:14px;"><!-- 使用zookeeper注册中心暴露服务地址 -->  </span></span><span style="color:#3333ff;"><span style="font-size:14px;">   、</span></span></strong>
<strong style="line-height: 1.1em;"><span style="color:#3333ff;"><span style="font-size:10px;"><dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""><span style="white-space: pre;">	</span></dubbo:registry</span></span></strong><span style="line-height: 1.1em;"> </span>
<span style="color:#cc33cc;line-height: 1.1em; font-size: 13.3333px;"> <!-- 要暴露的服务接口 -->  </span><span style="color:#cc33cc;line-height: 1.1em; font-size: 13.3333px;">  </span>
<strong style="line-height: 1.1em;"><span style="font-size:10px;color:#cc33cc;"><dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService"      ref="testRegistryService" /> </span><span style="font-size:14px;color:#cc33cc;"> </span></strong><span style="line-height: 1.1em; font-size: 13.3333px;">	</span>
<span style="font-size: 13.3333px; line-height: 1.1em;"></beans></span>

说明:

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

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

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

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

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

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

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

     1interface服务接口的路径

     2ref引用对应的实现类的BeanID

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

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


   (5) 启动项目,然后我们在Dubbo管理页面上显示,已经暴露的服务,但显示还没有消费者,因为我们还没实现消费者服务,如图所示



第二:我们在开发服务消费者,就是调用服务,我们在新建一个新的消费者项目结构如图所示:


(1)消费端也需要添加zookeeper和dubbo的jar包:

test-maven-server-console的pom.xml引入Dubbo和Zookeeper的jar包、test-maven-apijar包,因为引入test-maven-apijar包,我们在项目中调用像在本地调用一样。代码如下:

 (2)消费工程添加dubbo服务的引入;修改配置文件我们要引用的地址,代码如下

<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	<span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span>
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="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/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false" >

   <dubbo:application name="dubbo_consumer"></dubbo:application>
   <!-- 使用zookeeper注册中心暴露服务地址 -->  
   <dubbo:registry address="zookeeper://192.168.74.129:2181" check="false"></dubbo:registry> 
     <!-- 要引用的服务 -->  
   <dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference>
</beans>

(3)消费端调用:test-maven-server-console项目的具体实现,代码如下:

<span style="font-size: 13.3333px; background-color: rgb(225, 255, 252);">@Controller
public class IndexController {
	
	</span><strong><span style="font-size:18px;color:#ff0000;background-color: rgb(102, 255, 153);">@Autowired
	private TestRegistryService testRegistryService;//这里就直接自动注入服务</span></strong><span style="font-size: 13.3333px; background-color: rgb(225, 255, 252);">
	
	@RequestMapping("/hello")
	public String index(Model model){
	     String name=testRegistryService.hello("zz");
	     System.out.println("xx=="+name);
		return "";
	}

}</span>
  说明:

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

      1interface调用的服务接口

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

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

      4loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用

(4)项目启动, Dubbo 管理页面,能看到消费者,如图所示:

(5)然后访问消费者项目,Controller层能像调用本地一样调用服务的具体实现,如图所示:


Dubbo提供了多种容错方案,包括负载均衡这些,如图所示:

===============================================================

dubbo使用后理解:

step1

写一个接口工程 ----把step1中工程export为jar包

step2:

a.写一个对接口工程的实现(通过打jar包引入,或者通过linkSoure方式【源码】)工程

b.把实现的服务对外暴露【也就是修改该工程中的dubbo.xml文件中暴露对外的接口】

step3:

把step2的工程部署到服务并启动,打开zookeeper管理后台,能看到step2中暴露的接口已经自己启动


使用:

step4:服务调用

a.把step1中jar包导入到要使用dubbo的工程中,通过打jar包引入,或者通过linkSoure方式【源码】)

b.在工程中的dubbo.xml文件中,

修改dubbo-provider.xml文件引入step3中接口配置【包括zookeeper的服务器位置】【引入step2中实现的服务】具体见下图:dubbo-provider.xml的配置


c.然后在程序中autowire 就可以调用了




duboo服务死活注册不上。。。


========================================

对dubbo使用的几点总结:

1.对已经注册到zookeeper上的服务,当服务停了以后,会有短暂缓存,过一会就服务就会自动消失了

刷新管理页面可以看到

2.对已经注册到zookeeper上的服务,如果在启动一个新的工程注册同样的服务,

则旧的服务依然存在,不会被覆盖,要想用新的注册服务,则需要先把旧的服务停掉

dubbo有个缺陷:不太适合用对象操作,一般都用map返回key,value集合

尽管应该可以用对象实现,但还是麻烦,所以就用map返回吧!!!!




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值