dubbo入门基于maven项目

原创 2016年05月31日 17:11:00

一、首先安装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

  


Maven+SpringMVC+Dubbo 简单的入门demo配置

之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息。最近有时间了,打算做一个demo把dubbo在本地跑起来先。先copy一段dubbo的...

Dubbo+Zookeeper+Maven+Idea 入门搭建笔记

一、Zookeeper集群安装(同机器) 二、Dubbo Admin端搭建 三、 Dubbo + Spring+Zookeeper 开发 1、首先在idea 中新建两个maven 项目(一个pr...

spring maven 搭建dubbo框架 服务端

1 创建一个3 maven项目   1 定义接口项目      service 2 服务的提供者   provider    3 服务消费者     consumer  service...

dubbo调用和maven依赖的区别

dubbo:跨系统通信。比如:两个系统,一个系统A作客户端,一个系统B作服务器, 服务器B把自己的接口定义提供给客户端A,客户端A将接口定义在spring中的bean。客户端A可直接使用这个bean,...
  • syf1970
  • syf1970
  • 2017年04月19日 10:52
  • 1009

使用Maven构建Dubbo服务的可运行jar包 支持优雅关机

1.Dubbo项目结构如图 2. pom文件配置#注意:此处只展示打包所需的配置 dubbo-user-service ...

MAVEN的POM文件以及dubbo的相关配置(service和web)

标签表示打包成jar包还是war包 标签表示构建的方式 ${project.build.directory}/classes src/main/resources tr...

基于maven+dubbo+spring+zookeeper的简单项目搭建

Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。 Dubbo[]是一个分布式服务框架,...

跟我学习dubbo-使用Maven构建Dubbo服务的可执行jar包(4)

Dubbo服务的运行方式 服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源。 服...
  • jeesml
  • jeesml
  • 2016年01月06日 02:59
  • 1186

maven工程构建dubbo服务jar包示例

1.parent的pom文件

maven dubbo spring eclipse 项目集成

1 介绍 1.1 背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。 1....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:dubbo入门基于maven项目
举报原因:
原因补充:

(最多只允许输入30个字)