SpringBoot 2.0 集成 Dubbo

SpringBoot 2.0 集成 Dubbo

服务提供者示例

实现步骤

引入相关依赖
<dependencies>

    <!-- 引入springboot相关 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- 引入springboot dubbo依赖 -->
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>

    <!-- 引入RPC公共接口 -->
    <dependency>
        <groupId>cn.tyrone.springboot</groupId>
        <artifactId>springboot-dubbo-api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

</dependencies>

注意:springboot-dubbo-api属于dubbo要暴露的服务接口工程

创建application.yml文件
server:
  port: 9090
spring:
  application:
    name: spingboot-dubbo-provider

dubbo:
  application:
    id: spring-dubbo-provider
    name: spring-dubbo-provider
  registry: # 配置注册中心
    protocol: zookeeper # 指定注册中心协议
    address: localhost:2181 # 指定注册中心地址
  protocol: # 配置协议
    name: dubbo
    port: 20880
#  monitor:
#    protocol: registry  # 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址,否则直连监控中心。
创建springboot-dubbo-provider启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@SpringBootApplication
@EnableDubbo    // @EnableDubbo <==> @EnableDubboConfig + @DubboComponentScan
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}
创建服务提供者实现
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;

import cn.tyrone.springboot.dubbo.api.DemoService;

/*
 * 通过com.alibaba.dubbo.config.annotation.Service注解来声明要暴露的接口
 * 
 * @Service(interfaceClass = DemoService.class)相当于配置文件中的<dubbo:service interface="cn.tyrone.springboot.dubbo.api.DemoService" />
 * 
 */
@Service(interfaceClass = DemoService.class)
@Component
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return name + ":SpringBoot Dubbo Integrate.......";
    }
}

服务消费者示例

实现步骤

引入相关依赖
<dependencies>
    <!-- 引入springboot相关 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- 引入springboot dubbo依赖 -->
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>

    <!-- 引入RPC公共接口 -->
    <dependency>
        <groupId>cn.tyrone.springboot</groupId>
        <artifactId>springboot-dubbo-api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

注意:springboot-dubbo-api属于dubbo要暴露的服务接口工程

创建application.yml文件
server:
  port: 7070
spring:
  application:
    name: spring-boot-consumer

dubbo:
  application:
    id: spring-boot-consumer
    name: spring-boot-consumer
  registry:
    protocol: zookeeper
    address: localhost:2181
创建springboot-dubbo-consumer启动类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

import cn.tyrone.springboot.dubbo.api.DemoService;

@EnableDubbo    // @EnableDubbo <==> @EnableDubboConfig + @DubboComponentScan
@SpringBootApplication
public class Application implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    /*
     * 通过 @Reference 注解,生成远程代理服务,用于获取服务端请求
     */
    @Reference(interfaceClass = DemoService.class)
    private DemoService demoService;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }


    @Override
    public void run(String... args) throws Exception {

        String name = "SpringBoot-Dubbo-Consummer-Example";

        String result = demoService.sayHello(name);

        log.info("SpringBoot整合Dubbo消费端示例结果:" + result);

    }

}

RPC 接口示例

dubbo建议将服务接口,服务模型,服务异常等均放在 API 包中,因为服务模型及异常也是 API 的一部分,同时,这样做也符合分包原则:重用发布等价原则(REP),共同重用原则(CRP)。RPC 接口即将服务提供者和服务消费者共同使用的接口抽象到公共模块中。

实现步骤

按需创建服务接口

public interface DemoService {

    String sayHello(String name);

}

注意事项:本示例是用zookeeper做注册中心的,所以在搭建dubbo环境时,确保已经安装zookeeper。

测试

分别启动服务提供者示例和服务消费者示例,观察控制台日志输出,如果程序正常,会在服务消费者控制台看到如下日志:

2018-09-12 21:44:56.640  INFO 34995 --- [           main] c.t.s.dubbo.consumer.Application         : SpringBoot整合Dubbo消费端示例结果:SpringBoot-Dubbo-Consummer-Example:SpringBoot Dubbo Integrate....... 

源代码链接
服务提供者:https://github.com/myNameIssls/springboot-study/tree/master/springboot-dubbo-provider
服务消费者:https://github.com/myNameIssls/springboot-study/tree/master/springboot-dubbo-consumer
RPC接口工程:https://github.com/myNameIssls/springboot-study/tree/master/springboot-dubbo-api

参考链接:
http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
https://github.com/apache/incubator-dubbo-spring-boot-project/blob/master/README_CN.md
https://github.com/apache/incubator-dubbo

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值