首页 > 代码库 > 3.Dubbo2.5.3快速启动Hello World

3.Dubbo2.5.3快速启动Hello World

转载请出自出处:http://www.cnblogs.com/hd3013779515/

1.服务提供者

(1)工程目录

技术分享

(2)接口代码 HelloService.java

package cn.ljh.dubbo.service;

/** 
 * 功能概要:提供者service接口层 
 *  
 * @author ljh
 * @since  2017年5月23日  
 */
public interface HelloService {
    public String sayHello(String name); 
}

(3)接口实现 HelloServiceImpl.java

package cn.ljh.dubbo.service.impl;

import cn.ljh.dubbo.service.HelloService;

/** 
 * 功能概要:提供者service实现层 
 *  
 * @author ljh 
 * @since  2017年5月23日   
 */ 
public class HelloServiceImpl implements HelloService{

    public String sayHello(String name) {
        // TODO Auto-generated method stub
        return name + ", Hello World!";
    }

}

(4)配置文件 applicationProvider.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">
    <!-- 具体的实现bean -->    
    <bean id="helloservice" class="cn.ljh.dubbo.service.impl.HelloServiceImpl" />
     <!-- 提供方应用信息,用于计算依赖关系 --> 
    <dubbo:application name="hello-provider"  />
     <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.137.130:2181?backup=192.168.137.131:2181,192.168.137.132:2181" />
     <!-- 用dubbo协议在20880端口暴露服务 --> 
    <dubbo:protocol name="dubbo" port="20880" />
     <!-- 声明需要暴露的服务接口 --> 
    <dubbo:service interface="cn.ljh.dubbo.service.HelloService" ref="helloservice" />
</beans>

(5)log属性文件 log4j.properties

# Global logging configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

(6)服务启动 HelloServiceTest.java

package cn.ljh.dubbo.service;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloServiceTest {
     public static void main(String[] args) {  
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
                    new String[]{"applicationProvider.xml"});    
            context.start();   
            System.out.println("服务注册成功");
            try {  
                System.in.read();//让此程序一直跑,表示一直提供服务  
            } catch (IOException e) {         
                e.printStackTrace();  
            }    
        }  
}

(7)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>cn.ljh.dubbo</groupId>
  <artifactId>dubbo-hello-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>  
       <spring.version>3.2.8.RELEASE</spring.version>  
   </properties>  
 
   <dependencies>
           <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.10</version>
          <scope>test</scope>
        </dependency>
       <dependency>  
           <groupId>com.alibaba</groupId>  
           <artifactId>dubbo</artifactId>  
           <version>2.5.3</version>  
           <exclusions>  
               <exclusion>  
                   <groupId>org.springframework</groupId>  
                   <artifactId>spring</artifactId>  
               </exclusion>  
           </exclusions>  
       </dependency>  
       <dependency>  
           <groupId>com.github.sgroschupf</groupId>  
           <artifactId>zkclient</artifactId>  
           <version>0.1</version>  
       </dependency>  
       <!-- spring相关 -->  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-core</artifactId>  
           <version>${spring.version}</version>  
       </dependency>  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-beans</artifactId>  
           <version>${spring.version}</version>  
       </dependency>  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-context</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-web</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-tx</artifactId>  
           <version>${spring.version}</version>  
       </dependency>  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-orm</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.springframework</groupId>  
           <artifactId>spring-jms</artifactId>  
           <version>${spring.version}</version>  
       </dependency>  
   </dependencies>  
</project>

2.服务消费者

(1)工程目录

技术分享

(2)接口代码(和服务提供者一致) HelloService.java

package cn.ljh.dubbo.service;

/** 
 * 功能概要:提供者service接口层 
 *  
 * @author ljh
 * @since  2017年5月23日  
 */
public interface HelloService {
    public String sayHello(String name); 
}

(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">
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="hello-consumer"  />
 
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.137.130:2181?backup=192.168.137.131:2181,192.168.137.132:2181" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用HelloService -->
    <dubbo:reference id="helloService" interface="cn.ljh.dubbo.service.HelloService" />
 
</beans>

(4)log属性文件 log4j.properties

# Global logging configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

(5)服务消费 HelloServiceConsumerTest.java

package cn.ljh.dubbo.service;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloServiceConsumerTest {
    public static void main(String[] args) {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                new String[] { "applicationConsumer.xml" });  
          
        context.start();  
        HelloService providerService = (HelloService) context.getBean("helloService");  
  
        System.out.println(providerService.sayHello("Tom"));  
        
        try {  
            System.in.read();//让此程序一直跑
        } catch (IOException e) {         
            e.printStackTrace();  
        }    
  
    }  
}

(6)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>cn.ljh.dubbo</groupId>
  <artifactId>dubbo-hello-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <properties>  
       <spring.version>3.2.8.RELEASE</spring.version>  
   </properties>  
 
   <dependencies>
           <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.10</version>
          <scope>test</scope>
        </dependency>
       <dependency>  
           <groupId>com.alibaba</groupId>  
           <artifactId>dubbo</artifactId>  
           <version>2.5.3</version>  
           <exclusions>  
               <exclusion>  
                   <groupId>org.springframework</groupId>  
                   <artifactId>spring</artifactId>  
               </exclusion>  
           </exclusions>  
       </dependency>  
       <dependency>  
           <groupId>com.github.sgroschupf</groupId>  
           <artifactId>zkclient</artifactId>  
           <version>0.1</version>  
       </dependency>  
       <!-- spring相关 -->  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-core</artifactId>  
           <version>${spring.version}</version>  
       </dependency>  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-beans</artifactId>  
           <version>${spring.version}</version>  
       </dependency>  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-context</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-web</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-tx</artifactId>  
           <version>${spring.version}</version>  
       </dependency>  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring-orm</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.springframework</groupId>  
           <artifactId>spring-jms</artifactId>  
           <version>${spring.version}</version>  
       </dependency>  
   </dependencies>  
</project>

3.测试

(1)启动服务提供者

启动HelloServiceTest的main方法

技术分享

在监控中心可以看到服务已经注册http://192.168.137.130:8080/dubbo-admin/governance/services

技术分享

(2)启动消费者

启动HelloServiceConsumerTest的main方法

技术分享

在监控中心可以看到服务消费者http://192.168.137.130:8080/dubbo-admin/governance/applications

技术分享

 

参考

http://dubbo.io/User+Guide-zh.htm

3.Dubbo2.5.3快速启动Hello World