Dubbo入门教程

实现功能

存在2个系统,A系统和B系统,A系统调用B系统的接口获取数据.

安装zookeeper

  1. 解压zookeeper-3.4.8.tar.gz,将/conf/zoo_sample.cfg修改为zoo.cfg,修改配置文件

    Dubbo入门教程_第1张图片
    Paste_Image.png

2.双击启动zookeeper

Dubbo入门教程_第2张图片
Paste_Image.png

创建工程B

  • 新建B工程Maven项目


    Dubbo入门教程_第3张图片
    Paste_Image.png
  • 导入依赖


    4.0.0
    io.menm91
    dubbo-b
    1.0.0-SNAPSHOT
    war
    
        
            io.menm91
            dubbo-b-api
            1.0.0-SNAPSHOT
        
        
        
            org.springframework
            spring-webmvc
            4.1.3.RELEASE
        
        
            org.slf4j
            slf4j-log4j12
            1.6.4
        

        
            com.alibaba
            dubbo
            2.5.3
            
                
                    
                    spring
                    org.springframework
                
            
        
        
        
        
            org.apache.zookeeper
            zookeeper
            3.3.3
        
        
            com.github.sgroschupf
            zkclient
            0.1
        
    
    
        
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    8081
                    /
                
            
        
    

  • 配置log4j文件

    log4j.properties

log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n

创建B-API工程

  • 新建B-API工程MAVEN项目

    Dubbo入门教程_第4张图片
    Paste_Image.png
  • 新建User类

package io.menm91.bean;

//使用dubbo要求传输的对象必须实现序列化接口
public class User implements java.io.Serializable {

     private static final long serialVersionUID = -2668999087589887337L;

     private Long id;

     private String username;

     private String password;

     private Integer age;

     public Long getId() {
             return id;
     }

     public void setId(Long id) {
             this.id = id;
     }

     public String getUsername() {
             return username;
     }

     public void setUsername(String username) {
             this.username = username;
     }

     public String getPassword() {
             return password;
     }

     public void setPassword(String password) {
             this.password = password;
     }

     public Integer getAge() {
             return age;
     }

     public void setAge(Integer age) {
             this.age = age;
     }

    @Override
    public String toString() {
        return "User [username=" + username + ", age=" + age + "]";
    }

}
  • 创建UserService(接口)提供查询服务
package io.menm91.service;

import io.menm91.bean.User;
import java.util.List;

public interface UserService {

    /**
     * 查询所有用户
     * @return
     */
    public List queryAll();
}

在B工程中导入B-API的依赖

  • 导入B-API工程的依赖

    Dubbo入门教程_第5张图片
    Paste_Image.png
  • 创建UserServiceImpl实现类

package io.menm91.service.impl;

import io.menm91.bean.User;
import io.menm91.service.UserService;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {

    /**
     * 这里通过模拟查询数据库返回用户信息
     */
    public List queryAll() {
        List list = new ArrayList();
        for (int i = 0; i < 10; i++) {
                User user = new User();
                user.setAge(10 + i);
                user.setId(Long.valueOf(i + 1));
                user.setPassword("123456");
                user.setUsername("username_" + i);
                list.add(user);
        }
        return list;
    }


}
  • 编写dubbo配置文件
Dubbo入门教程_第6张图片
Paste_Image.png


    
    

    
    

    
    

    
    

    
    


  • 编写web.xml文件


    dubbo-b

    
        contextConfigLocation
        classpath:dubbo/dubbo-*.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
        index.jsp
    


  • 启动tomcat
Dubbo入门教程_第7张图片
Paste_Image.png

搭建A系统MAVEN项目

  • 创建A系统maven项目

    Dubbo入门教程_第8张图片
    Paste_Image.png
  • 导入依赖


    4.0.0
    io.menm91
    dubbo-a
    1.0.0-SNAPSHOT
    
        
            io.menm91
            dubbo-b-api
            1.0.0-SNAPSHOT
        
        
        
            org.springframework
            spring-webmvc
            4.1.3.RELEASE
        
        
            org.slf4j
            slf4j-log4j12
            1.6.4
        

        
            com.alibaba
            dubbo
            2.5.3
            
                
                    
                    spring
                    org.springframework
                
            
        

        
            org.apache.zookeeper
            zookeeper
            3.3.3
        

        
            com.github.sgroschupf
            zkclient
            0.1
        
        
        
            junit
            junit
            4.12
        
    

  • 配置log4j.properties文件

    log4j.properties

    log4j.rootLogger=DEBUG,A1
    log4j.appender.A1=org.apache.log4j.ConsoleAppender
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
    
  • 编写dubbo配置文件

Dubbo入门教程_第9张图片
Paste_Image.png


    
    

    
    

    
    


  • 编写UserServiceTest测试类

    Dubbo入门教程_第10张图片
    Paste_Image.png
package io.menm91.service;

import io.menm91.bean.User;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceTest {

    private UserService userService;
    @Before
    public void setUp() throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "classpath:dubbo/dubbo-*.xml");
        userService = applicationContext.getBean(UserService.class);
    }

    @Test
    public void testQueryAll() {
        List userList = userService.queryAll();
        for(User user : userList){
            System.out.println(user.toString());
        }
    }

}
  • 查看输出结果

    Dubbo入门教程_第11张图片
    Paste_Image.png

源代码:https://github.com/menm91/dubbo-index.git

你可能感兴趣的:(Dubbo入门教程)