【第12章】Spring Cloud之集成 Spring Cloud Gateway

文章目录


前言

Spring Cloud Gateway是一个基于Spring Framework 5、Spring Boot 2和Project Reactor等技术构建的API网关服务器,旨在为微服务架构提供简单且有效的路由管理方式和一系列网关功能。


一、新建项目

1. 项目结构

2. 引入依赖

nacos相关配置我定义在了父工程中,前面也有过介绍,这里不再赘述

xml 复制代码
<dependency>
   <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

网关不能包含spring-boot-starter-web模块

3. 启动类

java 复制代码
package org.example.gateway;

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

/**
 * Create by zjg on 2024/7/21
 */
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

4. 基本配置

yml 复制代码
server:
  port: 8888
spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      config:
        file-extension: yaml
        server-addr: ${NACOS_SERVER_ADDR}
        namespace: ${NACOS_NAMESPACE}
        username: ${NACOS_USERNAME}
        password: ${NACOS_PASSWORD}
        shared-configs[0]:
            data-id: base-discovery.yaml
            group: DEFAULT_GROUP
            refresh: true

二、新建配置

我们通过网关来为提供者和消费者配置路由规则信息

yml 复制代码
spring:
  cloud:
    gateway:
      routes[0]:
        id: provider-service
        uri: lb://provider-service
        predicates[0]:
          name: Path
          args[pattern]: /provider/**
      routes[1]:
        id: consumer-service
        uri: lb://consumer-service
        predicates[0]:
          name: Path
          args[pattern]: /consumer/**

三、新建服务

这里我们需要在提供者和消费者提供一个新的服务供测试使用

1. 提供者

java 复制代码
package org.example.nacos.provider.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create by zjg on 2024/7/21
 */
@RestController
@RequestMapping("/provider/")
public class HelloController {
    @RequestMapping("hello")
    public String hello(){
        return "hello provider";
    }
}

2. 消费者

java 复制代码
package org.example.nacos.consumer.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create by zjg on 2024/7/21
 */
@RestController
@RequestMapping("/consumer/")
public class HelloController {
    @RequestMapping("hello")
    public String hello(){
        return "hello consumer";
    }
}

接着我们再启动提供者和消费者服务。

四、单元测试

1. 启动网关服务

这里我们启动GatewayApplication服务

2. 提供者

localhost:8888/provider/hello

3. 消费者

localhost:8888/consumer/hello


总结

回到顶部

通过整合Spring Cloud Gateway统一后端服务的入口,在网关层进行统一的安全认证,降低微服务系统的复杂性。

相关推荐
budingxiaomoli43 分钟前
Spring统一功能处理
java·后端·spring
Flynn Liang2 小时前
可观测性与成本治理:一次对话的 17 个观测点
java·langchain·可观测性·micrometer·langchain4j·token 成本·llm 监控
金銀銅鐵3 小时前
带您看懂字节码指令(三):同一行加法,为什么有四种指令
java·jvm
yuyuxun15 小时前
协同过滤的校园二手物品智能推荐系统的设计与实现 毕业设计-附源码40082
javascript·vue.js·spring boot·python·数据分析·django·课程设计
文丑颜不良啊7 小时前
LeetCode2821~2840题目解析
java·leetcode
apcipot_rain7 小时前
C++转Java日记(10)
java·开发语言
简 洁 冬冬8 小时前
泛型方法写法全面辨析:从「能编译」到「能跑」到「能用」
java·泛型
LGL6030A8 小时前
Java学习历程31——初识Spring Boot
java·spring boot·学习
阿达和自己9 小时前
springboot学习-springboot引入MyBatis-Plus
spring boot·学习·mybatis
Lansonli10 小时前
大数据Spark(八十八):Spark任务资源之Standalone集群测试
java·服务器·spark