欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 【第12章】Spring Cloud之集成 Spring Cloud Gateway

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

2024/10/25 21:20:30 来源:https://blog.csdn.net/qq_44824164/article/details/140587101  浏览:    关键词:【第12章】Spring Cloud之集成 Spring Cloud Gateway

文章目录

  • 前言
  • 一、新建项目
    • 1. 项目结构
    • 2. 引入依赖
    • 3. 启动类
    • 4. 基本配置
  • 二、新建配置
  • 三、新建服务
    • 1. 提供者
    • 2. 消费者
  • 四、单元测试
    • 1. 启动网关服务
    • 2. 提供者
    • 3. 消费者
  • 总结


前言

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-servicecloud:nacos:config:file-extension: yamlserver-addr: ${NACOS_SERVER_ADDR}namespace: ${NACOS_NAMESPACE}username: ${NACOS_USERNAME}password: ${NACOS_PASSWORD}shared-configs[0]:data-id: base-discovery.yamlgroup: DEFAULT_GROUPrefresh: true

二、新建配置

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

在这里插入图片描述

spring:cloud:gateway:routes[0]:id: provider-serviceuri: lb://provider-servicepredicates[0]:name: Pathargs[pattern]: /provider/**routes[1]:id: consumer-serviceuri: lb://consumer-servicepredicates[0]:name: Pathargs[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统一后端服务的入口,在网关层进行统一的安全认证,降低微服务系统的复杂性。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com