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多模块,Dubbo分布式服务框架,SpringMVC,前后端分离项目,基础搭建,搭建过程出现的问题

现互联网公司后端架构常用到Spring+SpringMVC+MyBatis,通过Maven来构建。通过学习,我已经掌握了基本的搭建过程,写下基础文章为而后的深入学习奠定基础。   首先说一下这篇文章...

maven项目dubbo服务怎么打成tar包来部署

1.在src/main/下创建assembly文件夹 2.新建assembly.xml文件,文件内容如下 assembly tar.gz true ${...

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

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

IDEA下搭建maven管理的DUBBO项目,Zookeeper作为注册中心

maven DUBBO+zookeeper框架搭建

zookeeper注册中心,使用dubbo的maven项目

1.首先介绍一下整个环境测试需要的东西: 1.CentOS 6.6 2.tomcat 8.0 3.jdk 1.8 4..zookeeper 3.5.3 beta(测试版) 5.mysql ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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