码迷,mamicode.com
首页 > 其他好文 > 详细

Dubbo+Zookeeper平台的搭建

时间:2017-09-19 16:13:08      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:rri   scope   ota   run   scan   ble   out   client   model   

1、创建两个maven工程:provider和consumer

2、切换到provider

  技术分享

 

  1)pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wb.dubbo</groupId>
  <artifactId>provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
        <!-- spring版本号 -->
        <spring.version>4.2.5.RELEASE</spring.version>
  </properties>
  
  <dependencies>
      <!-- DUBBO -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.3</version>
                <exclusions>
                    <exclusion>
                        <artifactId>spring</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>commons-logging</artifactId>
                        <groupId>commons-logging</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>log4j</artifactId>
                        <groupId>log4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
                <version>4.11</version>
            </dependency>
        
            <!-- spring核心包 -->
            <!-- springframe start -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-oxm</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.4</version>
            </dependency>
            <!-- springframe end -->
  </dependencies>
</project>

   2) 创建DemoService接口

package com.wb.dubbo.service;

public interface DemoService {
    String sayHello(String name);

}

  实现DemoService接口,DemoServiceImpl

package com.wb.dubbo.serviceImpl;

import com.wb.dubbo.service.DemoService;

public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "hello " + name;
    }

}

  3)配置文件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="CIF" />

    <!-- 使用multicast广播注册中心暴露服务地址 uat -->
     <dubbo:registry address="zookeeper://10.199.11.11:2181?backup=10.199.11.22:2181,10.199.101.213:2181"/> 

    <dubbo:provider retries="0" cluster="failfast"></dubbo:provider>
    
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="-1" />
    <dubbo:monitor protocol="registry" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.wb.dubbo.service.DemoService"
        ref="demoService" timeout="1000" version="1.0.0" />
        
    <!-- 具体的实现bean -->  
    <bean id="demoService" class="com.wb.dubbo.serviceImpl.DemoServiceImpl" />  
    
</beans>

2、切换到consumer

 技术分享

  1)pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wb.dubbo</groupId>
  <artifactId>consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
        <!-- spring版本号 -->
        <spring.version>4.2.5.RELEASE</spring.version>
  </properties>
  
  <dependencies>
     <dependency>
         <groupId>com.shhxzq.fin.cif</groupId>
        <version>1.0.14-SNAPSHOT</version>
        <artifactId>cif-service</artifactId>
     </dependency>
      <dependency>
           <groupId>com.wb.dubbo</groupId>
          <artifactId>provider</artifactId>
          <version>0.0.1-SNAPSHOT</version>
     </dependency> 
     
      <!-- DUBBO -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.3</version>
                <exclusions>
                    <exclusion>
                        <artifactId>spring</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>commons-logging</artifactId>
                        <groupId>commons-logging</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>log4j</artifactId>
                        <groupId>log4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
                <version>4.11</version>
            </dependency>
        
            <!-- spring核心包 -->
            <!-- springframe start -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-oxm</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.4</version>
            </dependency>
            <!-- springframe end -->
  </dependencies>
</project>

  2)调用provider的服务,创建 BaseTest

package consumer;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:consumer.xml" })
public class BaseTest {

}

 

package consumer;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;


import com.wb.dubbo.service.DemoService;

public class DemoTest extends BaseTest {



    @Autowired
    private DemoService demoService;


    @Test
    public void sayHello() {
        System.out.println(demoService.sayHello("world"));
    }
}

  3)配置consumer

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
      http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- enable component scanning (beware that this does not enable mapper 
        scanning!) -->
    <!-- enable autowire -->
    <context:annotation-config />

     <dubbo:application name="cif"/>

     <!-- 使用multicast广播注册中心暴露服务地址 uat -->
     <dubbo:registry address="zookeeper://10.199.11.11:2181?backup=10.199.11.22:2181,10.199.101.213:2181"/> 

    <!-- 注册需要消费的服务 -->
     <dubbo:reference id="demoService"
        interface="com.wb.dubbo.service.DemoService"
        lazy="true" version="1.0.0" check="false" timeout="1000" /> 
    
</beans>

 

大功告成!!

Dubbo+Zookeeper平台的搭建

标签:rri   scope   ota   run   scan   ble   out   client   model   

原文地址:http://www.cnblogs.com/BinMax01/p/7552285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!