SpringBoot2.x Dubbo 整合

avatar 2020年08月19日15:27:04 6 2745 views
博主分享免费Java教学视频,B站账号:Java刘哥

昨天帮一个朋友把之前开源的轻量级博客系统改造成 Dubbo 版本的

这里记录一下过程,顺便讲解一下 SpringBoot + Dubbo 的整合

duubo 博客系统代码地址:https://github.com/saysky/ChuyunBlog-Dubbo

文档地址:https://liuyanzhao.com/1295973330599415808.html

 

一、模块拆分

将项目拆成多模块

拆分方法一、

blog-api

blog-core

blog-server

blog-web

 

拆分方法二、

blog-core

       blog-core-api

blog-core

       blog-core-api

blog-web

 

说明:

blog-core 主要是放核心服务:用户、权限、角色等

blog-service 主要是放业务服务:文章、评论、分类、标签等

blog-web 是对外暴露的控制层

api 层里面放的是接口,用于 web 层引入,core 和 service 层实现

 

我这里为了简单,采用第一种方式

 

二、拆分详细说明

1、api层主要是 service 接口,实体类

备注:博主比较懒,把其他公共的也放在这里了

一般只放无状态的东西,如实体类、接口 等

 

2、blog-core 

core 层和 service 层都只放 mapper 和 service实现

core 层主要是放用户、角色和权限相关的逻辑的接口实现

到时候 BlogCoreApplication 需要启动,所以 application.yml 需要配置 zookeeper 和 dubbo 信息,数据库连接信息等

 

3、blog-service

service 层主要是放文章、评论、分类和标签等相关的逻辑的接口实现

到时候 BlogServiceApplication 需要启动,所以 application.yml 需要配置 zookeeper 和 dubbo 信息,数据库连接信息等

 

4、blog-web

web 层主要放 controller、统一异常处理,web config ,前端代码(因为我们这个项目没有前后端分离,前端是 Thymleaf)

 

三、Dubbo 整合

1、添加依赖

 blog-core、blog-service、blog-web 都需要添加 dubbo 依赖

<!-- dubbo -->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-actuator</artifactId>
    <version>0.2.0</version>
</dependency>

 

2、application.yml

blog-core 与 Dubbo 相关配置如下,数据库等其他配置这里省略

server:
  port: 8081
  
dubbo:
  application:
    id: blog-core
    name: blog-core
    qosEnable: true
    qosPort: 18081
  protocol:
    id: dubbo
    name: dubbo
    port: 12341
  registry:
    address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
#    address: zookeeper://127.0.0.1:2188
    id: blog-core-registry
  scan:
    basePackages: com.example.blog.core.*
management:
  endpoint:
    dubbo:
      enabled: true
    dubbo-configs:
      enabled: true
    dubbo-properties:
      enabled: true
    dubbo-references:
      enabled: true
    dubbo-services:
      enabled: true
    dubbo-shutdown:
      enabled: true
  health:
    dubbo:
      status:
        defaults: memory
        extras: load,threadpool

 

blog-service 与 Dubbo 相关配置如下,数据库等其他配置这里省略

server:
  port: 8082
  
dubbo:
  application:
    id: blog-service
    name: blog-service
    qosEnable: true
    qosPort: 18082
  protocol:
    id: dubbo
    name: dubbo
    port: 12342
  registry:
    address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
#    address: zookeeper://127.0.0.1:2188
    id: blog-service-registry
  scan:
    basePackages: com.example.blog.service.*
management:
  endpoint:
    dubbo:
      enabled: true
    dubbo-configs:
      enabled: true
    dubbo-properties:
      enabled: true
    dubbo-references:
      enabled: true
    dubbo-services:
      enabled: true
    dubbo-shutdown:
      enabled: true
  health:
    dubbo:
      status:
        defaults: memory
        extras: load,threadpool

 

blog-web 与 Dubbo 相关配置如下,thymleaf、文件上传等其他配置这里省略

server:
  port: 8083
  
dubbo:
  application:
    id: blog-web
    name: blog-web
    qosEnable: true
    qosPort: 18083
  protocol:
    id: dubbo
    name: dubbo
    port: 12343
  registry:
    address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
#    address: zookeeper://127.0.0.1:2188
    id: blog-api-registry
  scan:
    basePackages: com.example.blog.web.*
management:
  endpoint:
    dubbo:
      enabled: true
    dubbo-configs:
      enabled: true
    dubbo-properties:
      enabled: true
    dubbo-references:
      enabled: true
    dubbo-services:
      enabled: true
    dubbo-shutdown:
      enabled: true
  health:
    dubbo:
      status:
        defaults: memory
        extras: load,threadpool

 

四、服务注册

将服务注册到 zookeeper 上需要做两步即可

1、如上 application 配置文件里设置 dubbo @Service 的扫包路径,即 dubbo.scan

2、给具体的接口实现加 @Service 注解 (注意导包不要导入错了,是 Dubbo 的 @Service,不是 Spring 的)

如下图代码

@Service(
    version = "1.0.0",
    application = "${dubbo.application.id}",
    protocol = "${dubbo.protocol.id}",
    registry = "${dubbo.registry.id}"
)
public class UserServiceImpl implements UserService {

导入的包是 import com.alibaba.dubbo.config.annotation.Service;

 

启动 blog-core 和 blog-service 后

通过启动 dubbo-admin 项目来查看服务注册状态

可以看到服务都正常注册了

 

 

五、服务消费

服务消费是使用另一个注解了 @Reference

之前我们在其他地方如 controller 里注入 service 实现类,是通过

@Autowired
private UserService userService;

 

现在我们要改成

@Reference(version = "1.0.0",
    application = "${dubbo.application.id}",
    interfaceName = "com.example.blog.api.service.UserService",
    check = false,
    timeout = 3000,
    retries = 0
)
private UserService userService;

 

然后启动 blog-web

项目能正常启动,则表示能调用 blog-core 和 blog-service 提供的接口服务

当然也可以通过 dubbo-admin 来查看服务消费记录

 

六、获取代码

代码地址:https://github.com/saysky/ChuyunBlog-Dubbo

文档地址:https://liuyanzhao.com/1295973330599415808.html

需要完整代码,也可以联系博主,微信847064370

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0