篇六:项目使用Dubbo

  导语:Dubbo是阿里巴巴的一个分布式服务的开源框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

  参考网站:http://dubbo.io/

 

一、Dubbo-admin管理web工程配置

部署dubbo-admin
    1、校验jdk安装:参考jdk安装
java –version 
2、安装tomcat:参考tomcat安装            
3、yum install –y unzip
rm –rf /usr/local/tomcat/webapp/ROOT/*
unzip dubbo-admin-2.5.4.war –d /usr/local/tomcat/webapp/ROOT
4、配置 hosts : vi /etc/hosts
       a、【root@localhost】 
            127.0.0.1  localhost
            127.0.0.1  root
       b、【root@lgp】 //lgp只是一个例子
            127.0.0.1  lgp
5、启动服务
        ./usr/local/tomcat/bin/startup.sh

 

二、Dubbo+Zookeeper部署

   1、Zookeeper部署

    a、先安装单台

    yum -y install openssh-clients    :安装scp,用于不同机器上复制文件
    mkdir /home/lgp  :创建包用于存放 zookeeper
    cp –rf zookeeper-3.4.6.tar.gz /home/lgp
    cd /home/lgp
    tar -xzvf zookeeper-3.4.6.tar.gz
    cd zookeeper-3.4.6/conf/
    mv zoo_sample.cfg   zoo.cfg
    vi zoo.cfg    :参考详情

server.X=A:B:C  # 其中X是一个数字, 表示这是第几号server. A是该server所在的IP地址. B配置该server和集群中的leader交换消息所使用的端口. C配置选举leader时所使用的端口. 这里的x是一个数字,与myid文件中的id是一致的。

 

    cd ..
    mkdir data
    mkdir logs
    touch /data/myid
    echo 1 > /home/lgp/ zookeeper-3.4.6/data/myid
   vi /etc/hosts

   b、集群

复制:对应的机器创建 /home/lgp 
      yum -y install openssh-clients
    
      修改对应 myid ,etc/hosts

scp /home/lgp/zookeeper-3.4.6 192.168.1.81:/home/lgp  //跨机器复制

启动服务:
    service iptables stop:分别关闭对应机器的 iptables
    cd /home/lgp/zookeeper-3.4.6/bin
    ./bin/zkServer.sh start :开启zookeeper
    ./bin/zkServer.sh status :状态查询
    ./bin/zkServer.sh stop :关闭zookeeper

  2、整合Zookeeper

cd /usr/local/tomcat/webapp/ROOT/WEB-INF
vi dubbo.properties

  pwd:/usr/local/apache-tomcat-7.0.61/webapps/ROOT/WEB-INF   dubbo.properties

 

三、Dubbo项目整合SpringMVC

  1、下载jar包

maven导入所需jar包:dubbo、zookeeper、zkclient
    <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>dubbo</artifactId>
                    <version>2.5.3</version>
     </dependency>

<!-- Zookeeper 用于分布式服务管理 -->
                <dependency>
                     <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                   <version>3.4.5</version>
                </dependency>
            <dependency>
                    <groupId>com.101tec</groupId>
                    <artifactId>zkclient</artifactId>
                    <version>0.3</version>
            </dependency>
       <!-- Zookeeper 用于分布式服务管理 end -->

  2、eclipse配置dubbo.xsd,解决标签不识别的问题

    百度云盘:https://pan.baidu.com/s/1nuIKgMd

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

 

  3、在.properties配置zookeeper的注册地址

dubbo.registry.address=192.168.1.72:2181\,192.168.1.73:2182\,192.168.1.74:2183

 

  4、service提供服务:spring-dubbo-provider.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="guduo-service-user" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />
  <!—本机 -->
    <dubbo:protocol name="dubbo" port="20823" />
    <!-- 监控中心配置,protocol="registry",表示从注册中心发现监控中心地址 -->
    <dubbo:monitor protocol="registry"/>
    <!-- 当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值 -->
    <dubbo:provider timeout="30000" threadpool="fixed" threads="100" accepts="1000" />
    <!-- 服务接口 -->
    <dubbo:service retries="0" interface="com.guduo.service.system.UserReportService" ref="userReportService" />
<dubbo:service retries="0" interface="com.guduo.service.system.UserSuggestionService" ref="userSuggestionService" />
</beans>  

  spring-context.xml引入spring-dubbo-provider.xml

 

  5、consumer注册服务:dubbo-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="guduo-web-publish" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!-- 注册中心地址 -->
    <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />
    <!-- 用户服务接口 -->
    <dubbo:reference interface="com.guduo.service.user.PmsActionService" id="pmsActionService" check="false"/>
    <dubbo:reference interface="com.guduo.service.user.PmsMenuService" id="pmsMenuService" check="false"/>
</beans>  

 

四、开发阶段,启动Dubbo服务

  建议放在"src/test"目录下面

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @描述: 启动Dubbo服务用的MainClass.
 * @作者: 刘广平
 * @创建时间: 2016-9-5,下午9:47:55 .
 * @版本: 1.0 .
 */
public class DubboProvider {

    private static final Log log = LogFactory.getLog(DubboProvider.class);

    public static void main(String[] args) {
        try {
            ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
            context.start();
        } catch (Exception e) {
            log.error("== DubboProvider context start error:",e);
        }
        synchronized (DubboProvider.class) {
            while (true) {
                try {
                    DubboProvider.class.wait();
                } catch (InterruptedException e) {
                    log.error("== synchronized error:",e);
                }
            }
        }
    }
}

 

posted @ 2016-10-10 17:46  刘广平  阅读(297)  评论(0编辑  收藏  举报