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

相关推荐
倒悬于世几秒前
JVM-类加载详情
java·开发语言·jvm
xiezhr2 分钟前
别再被VO、BO、PO、DTO、DO绕晕!今天用一段代码把它们讲透
java·后端·spring
zwhdlb14 分钟前
Java + 工业物联网 / 智慧楼宇 面试问答模板
java·物联网·面试
Pitayafruit16 分钟前
Spring AI 进阶之路04:集成 SearXNG 实现联网搜索
spring boot·后端·ai编程
刘一说30 分钟前
CentOS 系统 Java 开发测试环境搭建手册
java·linux·运维·服务器·centos
卷福同学37 分钟前
来上海三个月,我在马路边上遇到了阿里前同事...
java·后端
bingbingyihao3 小时前
多数据源 Demo
java·springboot
在努力的前端小白7 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
一叶飘零_sweeeet10 小时前
从繁琐到优雅:Java Lambda 表达式全解析与实战指南
java·lambda·java8
艾伦~耶格尔10 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试