Spring Cloud Gateway整合Nacos实现服务路由及集群负载均衡

简介: 我们都知道Spring Cloud Gateway是一个基于Spring Boot、Spring WebFlux、Project Reactor构建的高性能网关,旨在提供简单、高效的API路由。

目录

一、序言

二、代码示例

1、父工程spring-cloud-gateway-learning

2、子工程spring-cloud-api-gateway

(1) pom.xml

(2) 配置文件和代码示例

3、子工程spring-cloud-user-service

(1) pom.xml

(2) 配置文件

4、子工程spring-cloud-message-service

(1) pom.xml

(2) 配置文件和代码示例

三、测试结果

1、集群负载均衡测试

2、服务路由测试

一、序言


我们都知道Spring Cloud Gateway是一个基于Spring BootSpring WebFluxProject Reactor构建的高性能网关,旨在提供简单、高效的API路由。

Spring Cloud Gateway基于Netty运行,因此在传统Servlet容器中或者打成war包是不能正常运行的。

二、代码示例


这里我们注册中心选型的是Nacos,如果还没有安装Nacos,请参考:Nacos快速安装部署

1、父工程spring-cloud-gateway-learning

  <modules>
          <module>spring-cloud-api-gateway</module>
          <module>spring-cloud-user-service</module>
          <module>spring-cloud-message-service</module>
  </modules>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.boot.version>2.3.7.RELEASE</spring.boot.version>
        <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
        <spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
        <commons.lang3.version>3.12.0</commons.lang3.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons.lang3.version}</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.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.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

备注:具体Spring Cloud各版本说明请参考Spring Cloud Alibaba版本说明

2、子工程spring-cloud-api-gateway

(1) pom.xml

<parent>
        <groupId>com.universe</groupId>
        <artifactId>spring-cloud-gateway-learning</artifactId>
        <version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
</dependencies>

(2) 配置文件和代码示例

  1. bootstrap.yml
spring:
  application:
    name: api-gateway
server:
  port: 9000
  1. application.yml
spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/user/**
        - id: message-service
          uri: lb://message-service
          predicates:
            - Path=/message/**
    nacos:
      discovery:
        server-addr: localhost:8848

如果URI以lb开头,比如如上配置中的lb://user-service,Spring Cloud Gateway会用ReactiveLoadBalancerClientFilter 解析服务名为user-service的实例对应的实际host和端口,并做集群负载均衡。


这项功能通过全局过滤器ReactiveLoadBalancerClientFilter 实现,官网描述如下:

15.png

  1. RouteRecordGlobalFilter
@Slf4j
@Component
public class RouteRecordGlobalFilter implements GlobalFilter, Ordered {
  @Override
  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // RouteToRequestUrlFilter会把实际路由的URL通过该属性保存
    URI proxyRequestUri = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR);
    long start = System.currentTimeMillis();
    return chain.filter(exchange).then(Mono.fromRunnable(() -> {
      long end = System.currentTimeMillis();
      log.info("实际调用地址为:{},调用耗时为:{}ms", proxyRequestUri, (end - start));
    }));
  }
  @Override
  public int getOrder() {
    // 优先级设为最低,先让RouteToRequestUrlFilter先调用
    return Ordered.LOWEST_PRECEDENCE;
  }
}

RouteRecordGlobalFilter 这个全局过滤器我们主要用来记录路由后的实际代理地址,以及调用耗时。


我们看下RouteToRequestUrlFilter的描述会发现实际路由地址会通过ServerWebExchange中名为ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR的属性保存。

16.png

关于RouteToRequestUrlFilter的部分源码如下:

@Override
  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
    if (route == null) {
      return chain.filter(exchange);
    }
    log.trace("RouteToRequestUrlFilter start");
    URI uri = exchange.getRequest().getURI();
    boolean encoded = containsEncodedParts(uri);
    URI routeUri = route.getUri();
    if (hasAnotherScheme(routeUri)) {
      // this is a special url, save scheme to special attribute
      // replace routeUri with schemeSpecificPart
      exchange.getAttributes().put(GATEWAY_SCHEME_PREFIX_ATTR,
          routeUri.getScheme());
      routeUri = URI.create(routeUri.getSchemeSpecificPart());
    }
    if ("lb".equalsIgnoreCase(routeUri.getScheme()) && routeUri.getHost() == null) {
      // Load balanced URIs should always have a host. If the host is null it is
      // most
      // likely because the host name was invalid (for example included an
      // underscore)
      throw new IllegalStateException("Invalid host: " + routeUri.toString());
    }
    URI mergedUrl = UriComponentsBuilder.fromUri(uri)
        // .uri(routeUri)
        .scheme(routeUri.getScheme()).host(routeUri.getHost())
        .port(routeUri.getPort()).build(encoded).toUri();
    exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, mergedUrl);
    return chain.filter(exchange);
  }

备注:更多关于全局过滤器的介绍请参考 Spring Cloud Gateway全局过滤器

3、子工程spring-cloud-user-service

(1) pom.xml

<parent>
        <groupId>com.universe</groupId>
        <artifactId>spring-cloud-gateway-learning</artifactId>
        <version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
</dependencies>

(2) 配置文件

  1. bootstrap.yml
spring:
  application:
    name: user-service
server:
  servlet:
    context-path: /user
---
spring:
  profiles: user-service-master
server:
  port: 9091
---
spring:
  profiles: user-service-slave
server:
  port: 9092
  1. application.yml
spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  1. UserController
@RestController
public class UserController {
  @GetMapping("/info")
  public Map<String, Object> getUserInfo() {
  Random random = new Random();
    int waitTime = random.nextInt(1000);
    LockSupport.parkNanos(1000 * 1000 * waitTime);
    Map<String, Object> result = new HashMap<>();
    result.put("name", "Nick");
    result.put("age", 25);
    return result;
  }
}

4、子工程spring-cloud-message-service

(1) pom.xml

<parent>
        <groupId>com.universe</groupId>
        <artifactId>spring-cloud-gateway-learning</artifactId>
        <version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
</dependencies>

(2) 配置文件和代码示例

spring:
  application:
    name: message-service
server:
  servlet:
    context-path: /message
---
spring:
  profiles: message-service-master
server:
  port: 9093
---
spring:
  profiles: message-service-slave
server:
  port: 9094
  1. application.yml
spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  1. MessageController
@RestController
public class MessageController {
  @GetMapping("/info")
  public Map<String, Object> getMessageInfo() {
  Random random = new Random();
    int waitTime = random.nextInt(1000);
    LockSupport.parkNanos(1000 * 1000 * waitTime);
    Map<String, Object> result = new HashMap<>();
    result.put("id", 1);
    result.put("title", "我爱中国");
    return result;
  }
}

三、测试结果


分别启动api-gateway、指定概要文件启动两个user-service服务实例、和两个message-service服务实例,查看Nacos控制台。

17.png

可以看到,api-gateway启动了一个服务实例,user-service和message-service都启动了两个服务实例。

备注:IDEA运行时指定Active Profiles即可。

1、集群负载均衡测试

连续访问http://localhost:9000/user/info,可以看到user-service集群服务实例被轮询调用。

18.png

2、服务路由测试

分别访问 http://localhost:9000/user/infohttp://localhost:9000/message/info,我们可以看到基于路径匹配的服务路由分发是成功的。

19.png


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
相关文章
|
人工智能 Java Serverless
【MCP教程系列】搭建基于 Spring AI 的 SSE 模式 MCP 服务并自定义部署至阿里云百炼
本文详细介绍了如何基于Spring AI搭建支持SSE模式的MCP服务,并成功集成至阿里云百炼大模型平台。通过四个步骤实现从零到Agent的构建,包括项目创建、工具开发、服务测试与部署。文章还提供了具体代码示例和操作截图,帮助读者快速上手。最终,将自定义SSE MCP服务集成到百炼平台,完成智能体应用的创建与测试。适合希望了解SSE实时交互及大模型集成的开发者参考。
14625 60
|
8月前
|
负载均衡 NoSQL Redis
不增加 GPU,首 Token 延迟下降50%|LLM 服务负载均衡的新实践
针对LLM服务的特点,Higress AI网关以插件形式提供了面向LLM服务的负载均衡算法,包括全局最小请求数负载均衡、前缀匹配负载均衡以及GPU感知负载均衡,能够在不增加硬件成本的前提下,提升系统的吞吐能力、降低响应延迟,并实现更公平、高效的任务调度。
794 135
|
8月前
|
API
使用Gateway with Inference Extension路由外部MaaS服务
本文介绍如何通过Gateway with Inference Extension对接百炼服务,实现请求路由时自动添加API Key并重写路径,包含操作步骤及验证方法。
|
10月前
|
Java API Nacos
|
9月前
|
Prometheus 监控 Cloud Native
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务实现步骤
779 0
|
人工智能 自然语言处理 Java
对话即服务:Spring Boot整合MCP让你的CRUD系统秒变AI助手
本文介绍了如何通过Model Context Protocol (MCP) 协议将传统Spring Boot服务改造为支持AI交互的智能系统。MCP作为“万能适配器”,让AI以统一方式与多种服务和数据源交互,降低开发复杂度。文章以图书管理服务为例,详细说明了引入依赖、配置MCP服务器、改造服务方法(注解方式或函数Bean方式)及接口测试的全流程。最终实现用户通过自然语言查询数据库的功能,展示了MCP在简化AI集成、提升系统易用性方面的价值。未来,“对话即服务”有望成为主流开发范式。
8945 7
|
前端开发 Java Nacos
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
本文介绍了如何使用Spring Cloud Alibaba 2023.0.0.0技术栈构建微服务网关,以应对微服务架构中流量治理与安全管控的复杂性。通过一个包含鉴权服务、文件服务和主服务的项目,详细讲解了网关的整合与功能开发。首先,通过统一路由配置,将所有请求集中到网关进行管理;其次,实现了限流防刷功能,防止恶意刷接口;最后,添加了登录鉴权机制,确保用户身份验证。整个过程结合Nacos注册中心,确保服务注册与配置管理的高效性。通过这些实践,帮助开发者更好地理解和应用微服务网关。
2292 0
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
971 7
|
API
Istio 使用ingress和gateway两种方式公开服务
本文档指导您完成Istio网关的部署与配置。首先安装`istiod`(步骤略过)。接着,创建`ingress.yaml`文件,定义Istio入口网关的服务、部署及权限设置,通过`kubectl apply -f ingress.yaml`命令应用。最后,创建Ingress资源,指定主机名、后端服务及TLS配置,实现对外部请求的路由管理。
1267 0

热门文章

最新文章