一、背景

在微服务架构中 API网关 非常重要,网关作为全局流量入口并不单单是一个反向路由,更多的是把各个边缘服务(Web层)的各种共性需求抽取出来放在一个公共的“服务”(网关)中实现,例如安全认证、权限控制、限流熔断、监控、跨域处理、聚合API文档等公共功能。

 

在以 Dubbo 框架体系来构建的微服务架构下想要增加API网关,如果不想自研开发的情况下在目前的开源社区中几乎没有找到支持dubbo协议的主流网关,但是 Spring Cloud 体系下却有两个非常热门的开源API网关可以选择;本文主要介绍如何通过 Nacos 整合 Spring Cloud GatewayDubbo 服务

 

二、传统 dubbo 架构

dubbo属于rpc调用,所以必须提供一个web层的服务作为http入口给客户端调用,并在上面提供安全认证等基础功能,而web层前面对接Nginx等反向代理用于统一入口和负载均衡。

web层一般是根据业务模块来切分的,用于聚合某个业务模块所依赖的各个service服务

PS:我们能否把上图中的web层全部整合在一起成为一个API网关呢?(不建议这样做)

因为这样的web层并没有实现 泛化调用 必须引入所有dubbo服务的api依赖,会使得网关变得非常不稳定,任何服务的接口变更都需要修改网关中的api依赖!

 

三、整合 Srping Cloud Gateway 网关

下面就开始聊聊直接拿热门的 Srping Cloud Gateway 来作为dubbo架构体系的网关是否可行,首先该API网关是属于 Spring Cloud 体系下的组件之一,要整合dubbo的话需要解决以下问题:

  1. 打通注册中心:spring cloud gateway 需要通过注册中心发现下游服务,而 dubbo 也需要通过注册中心实现服务的注册与发现,如果两者的注册中心不能打通的话就会变成双注册中心架构就非常复杂了!
  2. 协议转换: gateway 使用http传输协议调用下游服务,而dubbo服务默认使用的是tcp传输协议

上面提到的第一个问题“打通注册中心”其实已经不是问题了,目前dubbo支持 ZookeeperNacos 两个注册中心,而 Spring Cloud 自从把 @EnableEurekaClient 改为 @EnableDiscoveryClient 之后已经基本上支持所有主流的注册中心了,本文将使用 Nacos 作为注册中心打通两者

 

3.1. 方式一

把传统dubbo架构中的 Nginx 替换为 Spring Cloud Gateway ,并把 安全认证 等共性功能前移至网关处实现

由于web层服务本身提供的就是http接口,所以网关层无需作协议转换,但是由于 安全认证 前移至网关了需要通过网络隔离的手段防止被绕过网关直接请求后面的web层

 

3.2. 方式二

dubbo服务本身修改或添加 rest 传输协议的支持,这样网关就可以通过http传输协议与dubbo服务通信了

rest传输协议:基于标准的Java REST API——JAX-RS 2.0(Java API for RESTful Web Services的简写)实现的REST调用支持

目前版本的dubbo已经支持dubbo、rest、rmi、hessian、http、webservice、thrift、redis等10种传输协议了,并且还支持同一个服务同时定义多种协议,例如配置 protocol = { "dubbo", "rest" } 则该服务同时支持 dubborest 两种传输协议

 

3.3. 总结

方式一 对比 方式二 多了一层web服务所以多了一次网络调用开销,但是优点是各自的职责明确单一,web层可以作为聚合层用于聚合多个service服务的结果经过融合加工一并返回给前端,所以这种架构下能大大减少服务的 循环依赖

 

四、代码实践

依赖环境

  • lombok
  • jdk 1.8
  • Nacos 1.3
  • Spring Boot 2.2.8.RELEASE
  • Spring Cloud Hoxton.SR5
  • Spring Cloud Alibaba 2.2.1.RELEASE

 

在根目录的 pom.xml 中定义全局的依赖版本

    <properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>8</java.version> <spring-boot-dependencies.version>2.2.8.RELEASE</spring-boot-dependencies.version>
<spring-cloud-dependencies.version>Hoxton.SR5</spring-cloud-dependencies.version>
<spring-cloud-alibaba-dependencies.version>2.2.1.RELEASE</spring-cloud-alibaba-dependencies.version>
<jaxrs.version>3.12.1.Final</jaxrs.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

 

4.1. 创建dubbo-api工程

分别定义两个api接口

DubboService 使用dubbo协议的服务

public interface DubboService {
String test(String param);
}

RestService 使用rest协议的服务

public interface RestService {
String test(String param);
}

 

4.2. 创建web-dubbo工程

使用 方式一 整合对接网关,这里为了简化在同一个服务下只使用逻辑分层定义controller层与service层,并没有做服务拆分

4.2.1. 创建配置

定义 spring boot 配置

server:
port: 8081 spring:
application:
name: zlt-web-dubbo
main:
allow-bean-definition-overriding: true
cloud:
nacos:
server-addr: 192.168.28.130:8848
username: nacos
password: nacos

server.port:配置应用服务器暴露的端口

spring.cloud.nacos:配置 spring cloud 的注册中心相关参数,nacos 的配置需要改为自己环境所对应

定义 dubbo 配置

dubbo:
scan:
base-packages: org.zlt.service
protocols:
dubbo:
name: dubbo
port: -1
registry:
address: spring-cloud://localhost
consumer:
timeout: 5000
check: false
retries: 0
cloud:
subscribed-services:

dubbo.scan.base-packages:指定 Dubbo 服务实现类的扫描基准包

dubbo.protocols:服务暴露的协议配置,其中子属性 name 为协议名称,port 为协议端口( -1 表示自增端口,从 20880 开始)

dubbo.registry.address:Dubbo 服务注册中心配置,其中子属性 address 的值 "spring-cloud://localhost",说明挂载到 Spring Cloud 注册中心

 

4.2.2. 创建DubboService的实现类

通过 protocol = "dubbo" 指定使用 dubbo协议 定义服务

@Service(protocol = "dubbo")
public class DubboServiceImpl implements DubboService {
@Override
public String test(String param) {
return "dubbo service: " + param;
}
}

 

4.2.3. 创建Controller类

使用 Spring Boot@RestController 注解定义web服务

@RestController
public class WebController {
@Autowired
private DubboService dubboService; @GetMapping("/test/{p}")
public String test(@PathVariable("p") String param) {
return dubboService.test(param);
}
}

 

4.3. 创建rest-dubbo工程

使用 方式二 整合对接网关,由于该服务是通过dubbo来创建rest服务,所以并不需要使用 Spring Boot 内置应用服务

4.3.1. 创建配置

定义 spring boot 配置

spring:
application:
name: zlt-rest-dubbo
main:
allow-bean-definition-overriding: true
cloud:
nacos:
server-addr: 192.168.28.130:8848
username: nacos
password: nacos

因为不使用 Spring Boot 内置的应用服务所以这里并不需要指定 server.port

定义 dubbo 配置

dubbo:
scan:
base-packages: org.zlt.service
protocols:
dubbo:
name: dubbo
port: -1
rest:
name: rest
port: 8080
server: netty
registry:
address: spring-cloud://localhost
consumer:
timeout: 5000
check: false
retries: 0
cloud:
subscribed-services:

dubbo.protocols:配置两种协议,其中rest协议定义 8080 端口并使用 netty 作为应用服务器

 

4.3.2. 创建RestService的实现类

通过 protocol = "rest" 指定使用 rest协议 定义服务

@Service(protocol = "rest")
@Path("/")
public class RestServiceImpl implements RestService {
@Override
@Path("test/{p}")
@GET
public String test(@PathParam("p") String param) {
return "rest service: " + param;
}
}

 

4.4. 创建Spring Cloud Gateway工程

定义 spring boot 配置

server:
port: 9900 spring:
application:
name: sc-gateway
main:
allow-bean-definition-overriding: true
cloud:
nacos:
server-addr: 192.168.28.130:8848
username: nacos
password: nacos

server.port:定义网关端口为 9090

定义网关配置

spring:
cloud:
gateway:
discovery:
locator:
lowerCaseServiceId: true
enabled: true
routes:
- id: web
uri: lb://zlt-web-dubbo
predicates:
- Path=/api-web/**
filters:
- StripPrefix=1
- id: rest
uri: lb://zlt-rest-dubbo
predicates:
- Path=/api-rest/**
filters:
- StripPrefix=1

分别定义两个路由策略:

  • 路径 /api-web/ 为请求 web-dubbo 工程
  • 路径 /api-rest/ 为请求 rest-dubbo 工程

 

4.5. 测试

分别启动:Nacos、sc-gateway、web-dubbo、rest-dubbo 工程,通过网关的以下两个接口分别测试两种整合方式

  1. http://127.0.0.1:9900/api-web/test/abc :请求 web-dubbo 工程测试整合方式一
  2. http://127.0.0.1:9900/api-rest/test/abc :请求 rest-dubbo 工程测试整合方式二

 

五、demo下载

ide需要安装 lombok 插件

https://github.com/zlt2000/dubboSpringCloud

 

扫码关注有惊喜!

Dubbo想要个网关怎么办?试试整合Spring Cloud Gateway的更多相关文章

  1. Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway

    Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...

  2. Nacos整合Spring Cloud Gateway实践

    Spring Cloud Gateway官网:http://spring.io/projects/spring-cloud-gateway Eureka1.0的问题和Nacos对比:https://w ...

  3. Nacos整合Spring Cloud Gateway组件

    一.什么是Spring Cloud Gateway Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口有着非常大的作用,常见的功能有路由转发.权限校 ...

  4. 微服务网关实战——Spring Cloud Gateway

    导读 作为Netflix Zuul的替代者,Spring Cloud Gateway是一款非常实用的微服务网关,在Spring Cloud微服务架构体系中发挥非常大的作用.本文对Spring Clou ...

  5. 最全面的改造Zuul网关为Spring Cloud Gateway(包含Zuul核心实现和Spring Cloud Gateway核心实现)

    前言: 最近开发了Zuul网关的实现和Spring Cloud Gateway实现,对比Spring Cloud Gateway发现后者性能好支持场景也丰富.在高并发或者复杂的分布式下,后者限流和自定 ...

  6. API网关spring cloud gateway和负载均衡框架ribbon实战

    通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...

  7. spring Cloud网关之Spring Cloud Gateway

    Spring Cloud Gateway是什么?(官网地址:https://cloud.spring.io/spring-cloud-gateway/reference/html/) Spring C ...

  8. Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!

    大家好,我是不才陈某~ 这是<Spring Cloud 进阶>第八篇文章,往期文章如下: 五十五张图告诉你微服务的灵魂摆渡者Nacos究竟有多强? openFeign夺命连环9问,这谁受得 ...

  9. spring cloud gateway整合sentinel作网关限流

    说明: sentinel可以作为各微服务的限流,也可以作为gateway网关的限流组件. spring cloud gateway有限流功能,但此处用sentinel来作为替待. 说明:sentine ...

随机推荐

  1. Rocket - debug - TLDebugModule

    https://mp.weixin.qq.com/s/EhUb1z5oiIw6dJ-90ifDJA 简单介绍TLDebugModule中的实现. 1. device device是一个设备描述符,包含 ...

  2. HTML静态页面项目:英雄联盟官网网站 的实现

    效果: 源码与素材:链接: https://pan.baidu.com/s/1OuJd1lfEV7mrnf0I6FXm4A 提取码: 5c6j 复制这段内容后打开百度网盘手机App,操作更方便哦

  3. [统计信息系列7] Oracle 11g的自动统计信息收集

    (一)统计信息收集概述 在Oracle 11g中,默认有3个自动任务,分别是:自动统计信息收集.SQL调优顾问.段空间调整顾问,查看方法如下: SQL> SELECT CLIENT_NAME,T ...

  4. JS遍历对象修改属性名

    根据接口返回数据中number属性值,对数据进行截取,并改变属性名.直接上码: 下面是需要处理的数据 let data={"minValue":7400, "maxVal ...

  5. Java 第十一届 蓝桥杯 省模拟赛 无向连通图最少包含多少条边

    无向连通图最少包含多少条边 题目 问题描述 一个包含有2019个结点的无向连通图,最少包含多少条边? 答案提交 这是一道结果填空的题,你只需要算出结果后提交即可.本题的结果为一个整数,在提交答案时只填 ...

  6. Java实现 LeetCode 710 黑名单中的随机数(黑白名单)

    710. 黑名单中的随机数 给定一个包含 [0,n ) 中独特的整数的黑名单 B,写一个函数从 [ 0,n ) 中返回一个不在 B 中的随机整数. 对它进行优化使其尽量少调用系统方法 Math.ran ...

  7. Java实现 蓝桥杯 历届试题 斐波那契

    试题 历届试题 斐波那契 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 斐波那契数列大家都非常熟悉.它的定义是: f(x) = 1 - (x=1,2) f(x) = f(x-1) ...

  8. Java实现 LeetCode 509 斐波那契数

    509. 斐波那契数 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0, F(1) = 1 ...

  9. Java实现奇偶数排序

    1 问题描述 给定一个整数数组,请调整 数组中数的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分.要求时间复杂度为O(n). 2 解决方案 2.1 一头一尾指针往中间扫描法 pack ...

  10. java实现不连续处断开

    不连续处断开 下列代码运行结果为: 12345 23456 89 23456789 即把一个串从数字不连续的位置断开.试完善之. String s = "123452345689234567 ...