【第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统一后端服务的入口,在网关层进行统一的安全认证,降低微服务系统的复杂性。

相关推荐
在荒野的梦想40 分钟前
若依微服务集成Flowable仿钉钉工作流
spring cloud·微服务·钉钉
Pitayafruit2 小时前
📌 Java 工程师进阶必备:Spring Boot 3 + Netty 构建高并发即时通讯服务
spring boot·后端·netty
梦想实现家_Z2 小时前
SpringBoot实现MCP Server实战详解
spring boot·后端·mcp
你是理想3 小时前
wait 和notify ,notifyAll,sleep
java·开发语言·jvm
helloworld工程师3 小时前
【微服务】SpringBoot整合LangChain4j 操作AI大模型实战详解
java·eclipse·tomcat·maven
Java&Develop3 小时前
idea里面不能运行 node 命令 cmd 里面可以运行咋回事啊
java·ide·intellij-idea
q567315234 小时前
使用Java的HttpClient实现文件下载器
java·开发语言·爬虫·scrapy
遥不可及~~斌4 小时前
Spring Boot 项目日志系统全攻略:Logback、Log4j2、Log4j与SLF4J整合指南
spring boot·log4j·logback
你们补药再卷啦4 小时前
不用额外下载jar包,idea快速查看使用的组件源码
java·ide·intellij-idea
爱的叹息4 小时前
Spring Boot 自定义配置类(包含字符串、数字、布尔、小数、集合、映射、嵌套对象)实现步骤及示例
java·linux·spring boot