• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Idea+maven+spring-cloud项目搭建系列--11 整合dubbo

武飞扬头像
拽着尾巴的鱼儿
帮助1

前言: 微服务之间通信框架dubbo,使用netty (NIO 模型)完成RPC 接口调用;

1 dubbo 介绍:

Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 Java、Golang 等多语言 SDK 实现。使用 Dubbo 开发的微服务原生具备相互之间的远程地址发现与通信能力, 利用 Dubbo 提供的丰富服务治理特性,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。Dubbo 被设计为高度可扩展,用户可以方便的实现流量拦截、选址的各种定制逻辑。
在云原生时代,Dubbo 相继衍生出了 Dubbo3、Proxyless Mesh 等架构与解决方案,在易用性、超大规模微服务实践、云原生基础设施适配、安全性等几大方向上进行了全面升级。

2 spring-cloud 集成dubbo:

2.1 抽取要对外发布的接口到api 模块中:

public interface DubboTestService {
    // 暴露dubbo 服务
    String test();
}

2.2 服务提供者:
1)增加依赖pom:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>

2)实现要暴露的接口:

// dubbo 服务暴露标识实现要暴露的方法
@DubboService
public class DubboTestServiceImpl implements DubboTestService {
    @Override
    public String test() {
        return "hello";
    }
}

3)启动类增加暴露接口的扫描路径:

@DubboComponentScan(basePackages ="org.lgx.bluegrass.bluegrasses.module.subscribe.service")

4)bootstrap.yml 增加dubbo 服务协议配置:

dubbo:
  protocol:
    name: dubbo
    port: 20881
  registry:
    address: spring-cloud://localhost   #使用SpringCloud中的注册中心

2.3 服务消费端:
1)增加依赖pom:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
  1. 消费接口:
import org.apache.dubbo.config.annotation.DubboReference;
import org.lgx.bluegrass.api.service.DubboTestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DubboRpcController {
    // dubbo 服务调用
    @DubboReference
    private DubboTestService  dubboTestService;
    @RequestMapping(value = "/dubbo-test", method = RequestMethod.GET)
    public String index() {
        return dubboTestService.test();
    }

}
学新通

3)bootstrap.yml 增加dubbo 服务协议配置:

dubbo:
  registry:
    address: spring-cloud://localhost  #使用cloud的注册中心
  consumer:
    check: false   #dubbo默认有启动检查
    retries: 0     #dubbo内置的重试机制

2.4 测试:
学新通

3 dubbo 和feign 关系:

  • 两者底层都是通过tcp 完成通信;
  • feign 是应用层,dubbo 是传输层;
  • feign 每次通信都进行连接建立,dubbo 会建立长连接,dubbo 的性能要优于feign;

4 参考:

1)Dubbo x Spring Boot 开发微服务应用;
2)配置项参考手册;

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjfhce
系列文章
更多 icon
同类精品
更多 icon
继续加载