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


前言

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


一、新建项目

1. 项目结构

在这里插入图片描述

2. 引入依赖

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

<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. 启动类

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. 基本配置

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

二、新建配置

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

在这里插入图片描述

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. 提供者

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. 消费者

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

  • 18
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值