Skip to content

Latest commit

 

History

History
247 lines (165 loc) · 7.3 KB

60.spring_boot_2.3_cloud_alibaba_2.2_gateway.md

File metadata and controls

247 lines (165 loc) · 7.3 KB

Spring Cloud Alibaba 集成网关Gateway


1 摘要

微服务路由网关功能的组件,前有 Netflix 的 Zuul,在官方停止更新后, Gateway 作为其有力的替代品,能够提供的功能也更加强大,除了路由功能之外,还可以提供限流、负载均衡等功能,支持长链接。关于 Zuul 与 Gateway 区别,可参考:

微服务网关Zuul和Gateway的区别

本文将基于 Spring Cloud Alibaba 2.2 集成 Spring Cloud Gateway 网关功能。

2 核心 Maven 依赖

./cloud-alibaba-gateway/pom.xml
    <dependencies>
        <!-- cloud alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
        </dependency>
        <!-- cloud gateway -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>${spring-cloud-gateway.version}</version>
        </dependency>

        
        <!-- 省略其他依赖 -->

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

其中 ${spring-cloud-alibaba.version} 的版本为 2.2.3.RELEASE , ${spring-cloud-gateway.version} 的版本为 2.2.5.RELEASE

注意事项: Spring Cloud Alibaba 2.2.3.RELEASE 版本支持的 Spring Boot 版本为 2.3.1.RELEASE ,建议在搭建项目时要保持版本的一致性,Spring Boot 版本过高或过低都可能导致不兼容问题

Spring Cloud Gateway 不能和 Spring Boot Web 一起打包

3 核心代码

3.1 application 配置

./cloud-alibaba-gateway/src/main/resources/application.yml
## config

## server
server:
  port: 8610

## spring
spring:
  application:
    name: cloud-alibaba-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 172.16.140.10:8688
    gateway:
      discovery:
        locator:
          enabled: true


## endpoint
management:
  endpoints:
    web:
      exposure:
        include: "*"

## log
#logging:
#  level:
#    org.springframework.cloud.gateway: debug

spring.cloud.gateway.discovery.locator.enabled: 启用 Spring Cloud Gateway

注意事项:

Spring Cloud Gateway 2.2 开始会自动代理服务注册中心的所有服务,不需要再手动配置路由

如果有需要配置特定的路由,可参考:

官方文档: Spring Cloud Gateway

3.2 SpringBoot 启动类

./cloud-alibaba-gateway/src/main/java/com/ljq/demo/springboot/alibaba/gateway/CloudAlibabaGatewayApplication.java
package com.ljq.demo.springboot.alibaba.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author junqiang.lu
 */
@EnableDiscoveryClient
@SpringBootApplication
public class CloudAlibabaGatewayApplication {

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

}

如果不做其他处理,单纯的路由功能,到这里已经实现功能了。没错,Gateway 的集成就这么简单。

4 路由功能测试

4.1 启动测试服务

启动一些在同一个 Nacos 注册中心的服务。

Nacos 控制台

image-20201217160653476

4.2 Gateway 代理的服务接口请求格式

被 Gateway 代理之后的接口,请求格式如下:

http://gateway_ip:gateway_port/proxy_service_id/proxy_service_api_url

eg: 
http://127.0.0.1:8610/cloud-alibaba-server-consumer-feign/api/nacos/consumer/replay

gateway_ip: 网关服务的 ip

gateway_port: 网关服务的端口

proxy_service_id: 被代理的服务 id

proxy_service_api_url: 被代理的服务接口地址

4.3 接口请求测试

测试的接口:

GET http://127.0.0.1:8610/cloud-alibaba-server-provider/api/nacos/hello?name=%E5%BE%B7%E7%8E%9B%E8%A5%BF%E4%BA%9A-Gateway

GET http://127.0.0.1:8610/cloud-alibaba-server-consumer/api/nacos/consumer/hello?name=%E5%BE%B7%E7%8E%9B%E8%A5%BF%E4%BA%9A-Gatway

GET http://127.0.0.1:8610/cloud-alibaba-server-consumer-feign/api/nacos/consumer/hello?name=%E5%BE%B7%E7%8E%9B%E8%A5%BF%E4%BA%9A-Gateway

这里分别请求了 3 个服务,即:

cloud-alibaba-server-provider
cloud-alibaba-server-consumer
cloud-alibaba-server-consumer-feign

其中 cloud-alibaba-server-provider 启动了两个服务实例,请求会被转发到两个服务实例中

cloud-alibaba-server-consumer-feign 服务的后台日志:

2020-12-17 19:13:23.982  INFO 13494 --- [nio-8604-exec-2] .l.d.s.a.s.c.f.c.NacosConsumerController : response: Hello,德玛西亚-Gateway

cloud-alibaba-server-provider 服务的后台日志:

2020-12-17 19:13:23.627  INFO 13783 --- [nio-8601-exec-4] c.l.d.p.a.s.p.c.NacosProviderController  : serverPort: 8601
2020-12-17 19:13:23.630  INFO 13783 --- [nio-8601-exec-4] c.l.d.p.a.s.p.c.NacosProviderController  : result: Hello,德玛西亚-Gateway
2020-12-17 19:13:23.778  INFO 13783 --- [nio-8601-exec-5] c.l.d.p.a.s.p.c.NacosProviderController  : serverPort: 8601
2020-12-17 19:13:23.778  INFO 13783 --- [nio-8601-exec-5] c.l.d.p.a.s.p.c.NacosProviderController  : result: Hello,德玛西亚-Gatway
2020-12-17 19:13:23.981  INFO 13783 --- [nio-8601-exec-6] c.l.d.p.a.s.p.c.NacosProviderController  : serverPort: 8601
2020-12-17 19:13:23.981  INFO 13783 --- [nio-8601-exec-6] c.l.d.p.a.s.p.c.NacosProviderController  : result: Hello,德玛西亚-Gateway

至此,Spring Cloud Alibaba 2.2 集成 Gateway 完成。

5 推荐参考资料

Spring Cloud Alibaba系列教程 - Spring Cloud Alibaba 路由网关(Gateway)

官方文档: Spring Cloud Gateway

微服务实战(六)集成服务网关 SpringCloud GateWay (配置中心&注册中心用Nacos)

6 本次提交记录

commit f4271a5c834e0975b14ba3bc06dc59747f927400 (HEAD -> dev, origin/master, origin/dev, origin/HEAD, master)
Author: ljq <flying9001@gmail.com>
Date:   Sun Dec 13 12:01:36 2020 +0800

    代码-新增基于 Spring boot 2.3 搭建 Spring Cloud Alibaba 2.2 微服务框架

版本回退命令

git reset --soft f4271a5c834e0975b14ba3bc06dc59747f927400