Springboot 2.x 整合Dubbo 2.6.x和Dubbo 2.7.x

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot 2.x 整合Dubbo 2.6.x和Dubbo 2.7.x相关的知识,希望对你有一定的参考价值。

参考技术A Dubbo有很长一段时间的发展史,2018年的时候不知道为什么阿里暂更了一年,之后又重新开始更新。之前一直是阿里在维护,后面阿里把它捐给了Apache基金会,由Apache来维护,2.6.x之前,maven中的包名都是alibaba,2.7.0之后包名改成了apache,其中整合入系统中有一些差异;发展史:

Dubbo的原理什么的以及它的组成这里就不扯了,直接说说dubbo的项目结构吧。
基本的Dubbo项目组成分为三个部分:

接口层这里自定义了接口,实现部分由服务提供层来实现。建议将model也放在接口层中,接口层中对dubbo没有相关依赖,在这里pom就不提供了。

dubbo入门学习-----dubbo整合springboot

springboot节省了大量的精力去配置各种bean,因此通过一个简单的demo来整合springboot与dubbo

一、创建boot-user-service-provider

本篇博文基于上篇中的dubbo项目,整体工程如下:

技术图片

1、pom.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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ty</groupId>
    <artifactId>boot-user-service-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-user-service-provider</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.ty</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    
        <!--引入springboot整合dubbo的jar包-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、application.properties

#dubbo项目名
dubbo.application.name=boot-user-service-providor

#注册中心地址
dubbo.registry.address=127.0.0.1:2181
#指定注册中心为zookeeper
dubbo.registry.protocol=zookeeper

#指定通信协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

#连接监控中心,去注册中心直接发现,不配地址
dubbo.monitor.protocol=registry

3、主类BootUserServiceProviderApplication

package com.ty;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//开启基于注解的dubbo功能
@EnableDubbo
@SpringBootApplication
public class BootUserServiceProviderApplication 

    public static void main(String[] args) 
        SpringApplication.run(BootUserServiceProviderApplication.class, args);
    

4、UserServiceImpl

package com.ty.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ty.bean.UserAddress;
import com.ty.service.UserService;

import java.util.Arrays;
import java.util.List;
//此@Service用的是dubbo的service注解
@Service
public class UserServiceImpl implements UserService 

    @Override
    public List<UserAddress> getUserAddressList(String userId) 
        UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
        UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");

        return Arrays.asList(address1,address2);
    

5、测试

启动项目,另外还需要启动dubbo-admin,这样可以很直观的在监控中心查看provider、consumer的状态。

技术图片

服务已经启动ok

二、创建boot-order-service-consumer

 1、pom.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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ty</groupId>
    <artifactId>boot-order-service-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-order-service-consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.ty</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--引入springboot整合dubbo的jar包-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、application.properties

#应用名称
dubbo.application.name=boot-order-service-consumer

#注册中心地址(可以直接全地址)
dubbo.registry.address=zookeeper://127.0.0.1:2181

#在注册中心找监控
dubbo.monitor.protocol=registry

3、主类

package com.ty;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class BootOrderServiceConsumerApplication 

    public static void main(String[] args) 
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);
    

4、OrderController

package com.ty.controller;

import com.ty.bean.UserAddress;
import com.ty.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class OrderController 

    @Autowired
    private OrderService orderService;

    @RequestMapping("/initOrder")
    public List<UserAddress> initOrder(@RequestParam("uid") String uid) 
        return orderService.initOrder(uid);
    

5、OrderServiceImpl

package com.ty.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ty.bean.UserAddress;
import com.ty.service.OrderService;
import com.ty.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 1、将服务提供者注册到注册中心(暴露服务)
 *         1)、导入dubbo依赖(2.6.2)\\操作zookeeper的客户端(curator)
 *         2)、配置服务提供者
 * 
 * 2、让服务消费者去注册中心订阅服务提供者的服务地址
 * @author lfy
 *
 */
@Service
public class OrderServiceImpl implements OrderService 

        //使用dubbo提供的@Reference注解引用远程服务
    @Reference
    UserService userService;

    @Override
    public List<UserAddress> initOrder(String userId) 
        // TODO Auto-generated method stub
        System.out.println("用户id:"+userId);
        //1、查询用户的收货地址
        List<UserAddress> addressList = userService.getUserAddressList(userId);
        return addressList;
    

6、测试

启动服务,并且调用boot-order-service-consumer的controller,如下:

技术图片

远程调用ok。

项目已经上传到了github上,地址:https://github.com/ali-mayun/dubbo 

以上是关于Springboot 2.x 整合Dubbo 2.6.x和Dubbo 2.7.x的主要内容,如果未能解决你的问题,请参考以下文章

最新官方版的SpringBoot 整合 Dubbo.md

springboot与dubbo整合入门(三种方式)

SpringBoot-整合Dubbo+Zookeeper

SpringBoot整合Dubbo的第三种方式——XML配置 + @ImportResource

dubbo远程调用(rpc)-->快速入门+管理控制台+整合Springboot开发

dubbo远程调用(rpc)-->快速入门+管理控制台+整合Springboot开发