dubbo使用教程

1 dubbo结构介绍

说明:

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

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

Registry:服务注册用发现的注册中心

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

Container:服务运行容器

调用关系说明:

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

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

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

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

4:服务消费者,从提供者地址列表中,基于选一台提供者进行调用,如果调用失败,再选另一台调用。

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

2 dubbo使用方法

     Dubbo采用全Spring配置方式,透明化介入程序,对应用没有任何API侵入,只需用Spring加载Dubbo的配置既可,Dubbo基于Spring的Schema扩展进行加载。

 单一工程中spring的配置

<bean id=”xxxService” class=”com.xxx.XxxServiceImpl”/>

<bean id=”sssAction” class=”con.xxx.XxxAction”>

           <property name=”xxxService” ref=”xxxService”/>

</bean>

远程服务:

在本地服务的基础上,做简单配置,完成远程化:

将上面的local.xml配置拆分为两份,讲服务定义部分放在服务提供方remote-provider.xml,将服务引用部分放在服务消费方remote-consumer.xml

并在提供方增加暴露服务配置<dubbo.service>,在消费方增加引用服务配置<dubbo:reference>

发布服务:

<!—和本地服务一样实现远程服务—>

<bean id=”xxxService” class=”com.xxx.XxxServcieImp”/>

<!—增加暴露远程服务配置—>

<dubbo:service interface=”com.xxx.XxxService” ref=”xxxService”/>

调用服务:

<!—增加引用远程服务配置à

<dubbo:reference id=”xxxService” interface=”com.xxx.XxxService”/>

<!—和本地服务一样使用远程服务 –>

<bean id=”xxxAction” class=”com.xxx.XxxAction”>

       <property name=”xxxService” ref=”xxxService”>

</bean>

3. 注册中心

3.1 Zookeeper介绍

使用zookeeper注册中心

注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小

Zookeeper是apacahe Hadoop的子项目,是一个树形的目录服务,支持变更推送,适合作为dubbo服务的注册中心。

Zookeeper:作为集群的管理工具使用,集中管理配置文件

3.2 Zookeeper的安装

 Zookeeper是java开发的可以运行在windows,linux环境,需要先安装jdk:

 安装步骤:

第一步:安装jdk

第二步:把zookeeper的压缩包上传到Linux系统

第三步:解压压缩包

          tar –zxvf zookeeper-3.4.6.tar.gz

第四步:进入zookeeper-3.4.6目录,创建data文件夹

第五步:进入conf文件夹中,把zoo_sample.cfg改名为zoo.cfg

        mv zoo_sample.cfg zoo.cfg

第六步:修改zoo.cfg文件,修改data属性:

        DataDir=/root/zookeeper-3.4.6/data

第七步:启动zookeeper:

        开启:[root@bogon bin]# ./zkServer.sh start

        关闭:[root@bogon bin]# ./zkServer.sh stop

        查看状态:[root@bogon bin]# ./zkServer.sh status

注意:需要关闭防火墙

  防火墙操作:

:查看防火墙状态:

       systemctl status firewalld

       service iptables status

: 暂时关闭防火墙:

       systemctl stop firewalld

       service iptables stop

: 永久关闭防火墙:

       systemctl disable firewalld

       service iptables off

: 重启防火墙:

       systemctl enable firewalld

       service iptables restart

: 永久关闭后重启:

       chkconfig iptables on

 

Zookeeper启动报错,未启动成功:

原因:

  1. 网络问题:防火墙未关闭
  2. 环境问题:java环境|端口占用

4. 监控中心

需要安装tomcat,然后部署监控中心

安装tomcat:建议和zookeeper安装在同一台服务器上

部署监控中心:

cp dubbo-admin-2.5.4.war /software/apache-tomcat-7.0.47/webapps/dubbo-admin.war

启动tomcat:

[root@localhost apache-tomcat-7.0.47]# bin/startup.sh

Using CATALINA_BASE:   /software/apache-tomcat-7.0.47

Using CATALINA_HOME:   /software/apache-tomcat-7.0.47

Using CATALINA_TMPDIR: /software/apache-tomcat-7.0.47/temp

Using JRE_HOME:        /software/jdk1.7.0_71

Using CLASSPATH:      

/software/apache-tomcat-7.0.47/bin/bootstrap.jar:/software/apache-tomcat-7.0.47/bin/tomcat-juli.jar

显示控制台:

[root@localhost apache-tomcat-7.0.47]# tail -f logs/catalina.out

访问:IP地址:8080/dubbo-admin/

用户名:root

密码:root

注:如果监控中心和注册中心在同一个服务器上,不需要任何配置

       如果不在同一台服务器,需要修改配置文件:

webapps/dubbo-admin/WEB-INF/

vim dubbo.properties

5. 实现通信

服务层工程:

1.在pom.xml文件中添加dubbo依赖的jar包:

<!-- dubbo相关 -->

              <dependency>

                     <groupId>com.alibaba</groupId>

                     <artifactId>dubbo</artifactId>

                     <exclusions>

                            <!-- 排除 -->

                            <exclusion>

                                   <groupId>org.springframework</groupId>

                                   <artifactId>spring</artifactId>

                            </exclusion>

                            <exclusion>

                                   <groupId>org.jboss.netty</groupId>

                                   <artifactId>netty</artifactId>

                            </exclusion>

                     </exclusions>

              </dependency>

              <dependency>

                     <groupId>org.apache.zookeeper</groupId>

                     <artifactId>zookeeper</artifactId>

              </dependency>

              <dependency>

                     <groupId>com.github.sgroschupf</groupId>

                     <artifactId>zkclient</artifactId>

              </dependency>

注意:dubbo会依赖spring,自动添加spring的jar包但添加版本较低,所以应该排除。

2.在spring的配置文件中添加dubbo的约束,然后使用dubbo:service发布服务

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

       <!-- 配置包扫描器 -->

       <context:component-scan base-package="cn.e3mall.service"/>

       <!-- 使用dubbo发布服务 -->

       <!-- 提供方应用信息,用于计算依赖关系 -->

       <dubbo:application name="e3-manager" />

       <dubbo:registry protocol="zookeeper" address="192.168.113.130:2181" />

       <!-- dubbo协议在20880端口暴露服务 -->

       <dubbo:protocol name="dubbo" port="20880" />

       <!-- 声明需要暴露的服务接口 -->

       <dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" timeout="600000"/>

</beans>

注:约束如果不起作用,需要配置本地约束

Timeout:设置超时时间,默认1000,超时后默认尝试三次

 

表现层工程

添加依赖,和服务层一样

修改springmvc.xml,在springmv的配置文件中添加服务的引用

<?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:p="http://www.springframework.org/schema/p"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

              http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

       <context:component-scan base-package="cn.e3mall.controller" />

       <mvc:annotation-driven />

       <bean

       class="org.springframework.web.servlet.view.InternalResourceViewResolver">

              <property name="prefix" value="/WEB-INF/jsp/" />

              <property name="suffix" value=".jsp" />

       </bean>

       <!-- 配置资源映射 -->

       <mvc:resources location="/css/" mapping="/css/**"/>

       <mvc:resources location="/js/" mapping="/js/**"/>

       <!-- 引用dubbo服务 -->

       <dubbo:application name="e3-manager-web"/>

       <dubbo:registry protocol="zookeeper" address="192.168.113.130:2181"/>     

       <dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />

</beans>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值