关闭

dubbo入门基于maven项目

标签: dubbomavenzookeeper
3704人阅读 评论(0) 收藏 举报
分类:

一、首先安装zookeeper,下载     zookeeper-3.3.6.tar   安装  下载地址:http://apache.fayea.com/zookeeper/zookeeper-3.3.6/

       注意\zookeeper-3.3.6\conf\下的zoo_sample.cfg复制一份改成zoo.cfg,目的防止启动zk时闪退,具体原因不详,有知道的可以给出解释。

      启动zkServer

二、dubbo-server服务项目

        1、pox.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<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>dubbo-server</groupId>
  <artifactId>dubbo-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>dubbo-server</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.3</version>
            </dependency>
            <dependency>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.18.1-GA</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.15</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.sun.jdmk</groupId>
                        <artifactId>jmxtools</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.sun.jmx</groupId>
                        <artifactId>jmxri</artifactId>
                    </exclusion>
                    <exclusion>
                        <artifactId>jms</artifactId>
                        <groupId>javax.jms</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>mail</artifactId>
                        <groupId>javax.mail</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
                <version>2.5.6.SEC03</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.6</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.6.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.5</version>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.4</version>
            </dependency>
        </dependencies>
</project>

2、服务器service代码

package com.demo.server.service;

public interface DemoTest {
	public String hello(String name);
}

package com.demo.server.service.impl;

import com.demo.server.service.DemoTest;

public class DemoTestImpl implements DemoTest{

	public String hello(String name) {
		return "hello "+ name;
	}
}
3、applicationProvider.xml配置    路径:/dubbo-server/src/main/resources

<?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
        ">
 <!-- 具体的实现bean -->
 <bean id="demoService"
  class="com.demo.server.service.impl.DemoTestImpl" />
 <!-- 提供方应用信息,用于计算依赖关系 -->
 <dubbo:application name="demo_provider" />
 <!-- 使用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="com.demo.server.service.DemoTest"
  ref="demoService" />
</beans>

4、启动服务代码

package com.demo.server;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args ) throws Exception
    {
    	ClassPathXmlApplicationContext context=
    			new ClassPathXmlApplicationContext(new String[] {"applicationProvider.xml"});
    	context.start();
    	System.out.println("任意键退出");
    	System.in.read();
    }
}

三、dubbo-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>dubbo-consumer</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>dubbo-consumer</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.18.1-GA</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>jms</artifactId>
                    <groupId>javax.jms</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>mail</artifactId>
                    <groupId>javax.mail</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6.SEC03</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.adyliu</groupId>
            <artifactId>zkclient</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>dubbo-server</groupId>
            <artifactId>dubbo-server</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    </project>

2、消费者service代码

package com.demo.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.demo.server.service.DemoTest;

public class ConsumerThd {
	public void sayHello(){
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(	
		new String[] {"applicationConsumer.xml"});
		context.start();
		DemoTest demoService=(DemoTest) context.getBean("demoService");
		System.out.println(demoService.hello("world"));
   }  
}

3、消费者applicationConsumer.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="com.demo.server.service.DemoTest" />
</beans>
4、消费服务启动

package com.test;

import com.demo.consumer.ConsumerThd;

public class AppTest {

	public static void main(String[] args) {
		ConsumerThd thd=new ConsumerThd();
		thd.sayHello();
	}
}

四、测试

dubbo-server启动:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
任意键退出
dubbo-consumer测试启动:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
hello world
需要先启动server且不能退出,否则找不到,报错(No provider available for the service com.demo.server.service.DemoTest from the url zookeeper://127.0.0.1:2181

  


2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13328次
    • 积分:222
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:22篇
    • 译文:1篇
    • 评论:1条
    文章分类