springboot快速集成dubbo

当springboot和dubbo走到了一起,那么如何让它两更快更容易的集成在一起呢?dubbo-spring-boot-starter帮助我们做到了这点,它实现了dubbo在springboot项目中自动配置(@EnableAutoConfiguration)。

dubbo-spring-boot-starter模块

dubbo-spring-boot-starter主要由以下部分内容组成:

  • dubbo-spring-boot-parent 模块主要管理 Dubbo Spring Boot 工程的 Maven 依赖;
  • dubbo-spring-boot-autoconfigure 模块提供 Spring Boot’s @EnableAutoConfiguration 的实现 - DubboAutoConfiguration, 它简化了 Dubbo 核心组件的装配;
  • dubbo-spring-boot-actuator 提供 Production-Ready 特性:健康检查、控制断点、外部化配置等;

dubbo-starter方式集成dubbo案例

现在通过一个案例来看springboot使用dubbo-spring-boot-starter来集成dubbo是多么简单的事儿。

这里通过dubbo直连的方式,不引入注册中心。

首先在项目POM文件中中引入如下信息(制定dubbo的版本号以及dubbo的依赖管理):

    <properties>
        <dubbo.version>2.7.0</dubbo.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>javax.servlet</groupId>
                        <artifactId>servlet-api</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

然后定义一个公共的server-api:

public interface HelloService {

    String hello(String name);
}

springboot-dubbo-provider

在服务提供者中引入如下依赖:

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

在application.yml文件中设置dubbo的简单配置:


spring:
  application:
    name: springboot-dubbo-provider

dubbo:
#  application:
#    name: #defualt is ${spring.application.name}
  protocol:
    name: dubbo
    port: 28080
  registry:
    address: N/A
  scan:
    base-packages: com.lazycece.sbac.dubbo.provider.service
  provider:
    group: demo

HelloSerice服务提供信息如下:

package com.lazycece.sbac.dubbo.provider.service;

import com.lazycece.sbac.dubbo.api.HelloService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author lazycece
 * @date 2019/03/30
 */
@Component
@Service(version = "1.0.0", timeout = 3000)
public class HelloServiceImpl implements HelloService {

    @Value("${dubbo.application.name}")
    private String serviceName;

    @Override
    public String hello(String name) {
        return String.format("Hello, %s ! the service is %s", name, serviceName);
    }
}

springboot-dubbo-consumer

在服务消费者中引入如下依赖:

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

在application.yml文件中设置dubbo的简单配置:

spring:
  application:
    name: springboot-dubbo-consumer
dubbo:
  consumer:
    group: demo

HelloSerice服务消费案例代码如下:

package com.lazycece.sbac.dubbo.consumer.controller;

import com.lazycece.sbac.dubbo.api.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lazycece
 * @date 2019/03/30
 */
@RestController
public class HelloController {

    @Reference(version = "1.0.0", url = "dubbo://127.0.0.1:28080")
    private HelloService helloService;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name) {
        return helloService.hello(name);
    }
}

结果验证

在浏览器访问如下地址:http://localhost:8080/hello/lazycece

输出信息为: Hello, lazycece ! the service is springboot-dubbo-provider

案例代码

案例代码地址:https://github.com/lazycece/springboot-actual-combat/tree/master/springboot-ac-dubbo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值