dubbo入门

dubbo-api 接口定义

StudentDto返回数据实体

package com.frank.dto;

import java.io.Serializable;
import java.util.Arrays;

public class StudentDto implements Serializable {
    private static final long serialVersionUID = -3811325927474026428L;
    private String name;
    private String grade;
    private String[] subjects;

    public StudentDto(String name, String grade, String[] subjects) {
        this.name = name;
        this.grade = grade;
        this.subjects = subjects;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String[] getSubjects() {
        return subjects;
    }

    public void setSubjects(String[] subjects) {
        this.subjects = subjects;
    }

    @Override
    public String toString() {
        return "StudentDto{" +
                "name='" + name + '\'' +
                ", grade='" + grade + '\'' +
                ", subjects=" + Arrays.toString(subjects) +
                '}';
    }
}

StudentService接口类

package com.frank.service;

import com.frank.dto.StudentDto;

import java.util.List;

public interface StudentService {

    List findAll();
}

pom



    
        dubbo-demo
        com.frank
        1.0-SNAPSHOT
    
    4.0.0

    dubbo-api

    
        
            org.apache.dubbo
            dubbo
            2.7.9
        
        
            org.apache.curator
            curator-framework
            5.1.0
        
        
            org.apache.curator
            curator-recipes
            5.1.0
        
    

dubbo-provider 提供者实现

StudentServiceImpl实现类

package com.frank.service.impl;

import com.frank.dto.StudentDto;
import com.frank.service.StudentService;
import org.springframework.stereotype.Service;

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

@Service
public class StudentServiceImpl implements StudentService {
    public List findAll() {
        List studentDtos=new ArrayList();
        studentDtos.add(new StudentDto("Frank","一年级",new String[]{"语文","数学"}));
        studentDtos.add(new StudentDto("Danny","六年级",new String[]{"语文","英语","数学","地理"}));
        return studentDtos;
    }
}

Provider测试启动类

package com.frank;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Provider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
    }
}

provider.xml




    
    

    
    

    
    

    
    

    
    


pom



    
        dubbo-demo
        com.frank
        1.0-SNAPSHOT
    
    4.0.0

    dubbo-provider
    
        
            com.frank
            dubbo-api
            1.0-SNAPSHOT
        
    

启动成功后可在管理控制台看到提供者信息


image.png

dubbo-custom 消费者

Consumer 测试启动类

package com.frank;

import com.frank.dto.StudentDto;
import com.frank.service.StudentService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("custom.xml");
        context.start();
        StudentService studentService = (StudentService) context.getBean("studentService");
        List list = studentService.findAll();
        for (StudentDto student:list) {
            System.out.println(student.toString());
        }
        System.in.read();
    }
}

custom.xml




    
    

    
    

    
    

    
    


pom.xml



    
        dubbo-demo
        com.frank
        1.0-SNAPSHOT
    
    4.0.0

    dubbo-custom

    
        
            com.frank
            dubbo-api
            1.0-SNAPSHOT
        

执行结果

StudentDto{name='Frank', grade='一年级', subjects=[语文, 数学]}
StudentDto{name='Danny', grade='六年级', subjects=[语文, 英语, 数学, 地理]}

你可能感兴趣的:(dubbo入门)