springboot2.15+ dubbo2.7.1 maven搭建简单demo

 

  • 准备工作

ide:idea2019

maven:3.3.7

jdk: 1.8

dubbo: 2.7.1 

  • 项目结构

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第1张图片

其中demo为root工程,可以抽取统一的配置信息和依赖版本控制,方便子pom直接引用,简化子pom的配置。(这里没有做抽取,想了解的可以参考这篇文章)其中service为公共接口工程,provider为接口是的实现,consumer调用端。

  • 创建demo工程

idea创建基本的maven工程

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第2张图片

填写goupid、artifactid

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第3张图片

填写project name、project loaction

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第4张图片

  • 创建公共接口service

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第5张图片

并创建公共接口DemoService

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第6张图片

package com.stu.service;

public interface DemoService {

    int sum(int a,int b);
}
  • 创建provider服务

依然是在demo下创建model,选择Spring Initializr

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第7张图片

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第8张图片

添加springboot依赖

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第9张图片

pom文件加入dubbo依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.stu
    provider
    0.0.1-SNAPSHOT
    provider
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.1
        

        
            org.apache.dubbo
            dubbo
            2.7.1
        

        
        
            org.apache.curator
            curator-framework
            4.2.0
        

        
        
            org.apache.curator
            curator-recipes
            
                
                    org.apache.zookeeper
                    zookeeper
                
            
            4.2.0
        

        
            org.apache.zookeeper
            zookeeper
            3.4.7
        
        
        
        
            com.stu
            service
            1.0-SNAPSHOT
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


创建DemoServiceImpl实现公共接口

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第10张图片

package com.stu.service.impl;

import com.stu.service.DemoService;

public class DemoServiceImpl implements DemoService {
    @Override
    public int sum(int a, int b) {
        return a + b;
    }
}

以XML方式注册服务

在resource中创建provider.xml,具体配置可参考dubbo网站



 
    
    
 
    
    
 
    
    
 
    
    
 
    
    

在ProviderApplication.java中引用xml

@ImportResource("classpath:provider.xml")

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第11张图片

  • 创建consumer调用端工程

创建步骤参考provider。创建好后,添加consumer.xml配置文件



 
    
    
 
    
    
 
    
    

同样在ConsumerApplication.java中引入xml

@ImportResource("classpath:consumer.xml")

创建测试web接口

package com.stu.controller;

import com.stu.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

    @Autowired
    private DemoService demoService;

    @RequestMapping("sum")
    @ResponseBody
    public int getResult(int a,int b){
        return demoService.sum(a,b);
    }
}

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第12张图片

注意 在此之前需要在demo工程中,把provider和consumer添加为modules,否则consumer中注入DemoService时编译错误,引入后idea也有提示,但不影响编译忽略掉。修改demo中的pom文件



    4.0.0

    com.stu
    demo
    pom
    1.0-SNAPSHOT
    
        service
        provider
        consumer
    


 

 

修改consumer下的application.properties文件配置启动端口,避免和provider重复

server.port=8081
  • windows安装zookeeper

看这篇文章介绍的比较简单

启动zookeeper

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第13张图片

  • 测试调用

依次启动provider、consumer

打开浏览器,测试,调用成功

springboot2.15+ dubbo2.7.1 maven搭建简单demo_第14张图片

你可能感兴趣的:(dubbo,springboot)