【Spring全家桶系列之核心篇 | Spring Cloud】 - 第七章 掌握Gateway核心技术,实现高效路由与转发
创始人
2024-12-26 22:03:50
0

目录

  • 前言
  • 示例
    • 创建一个服务提供者
    • 创建网关
  • 创建common子项目

前言

Spring Cloud Gateway 是一个基于 Spring Boot 的非阻塞 API 网关服务,它提供了动态路由、请求断言、过滤器等功能。

以下是关于 Spring Cloud Gateway 的示例:

示例

创建一个服务提供者

  1. 新建Module
    在这里插入图片描述在这里插入图片描述在这里插入图片描述dependency选择Spring Web及Eureka Discovery Client。
    在这里插入图片描述2. 打开pom.xml文件,修改如下:
    在这里插入图片描述3. 定义服务
    在src/main/java下面的com.cherry.provider 下创建新的包controller(也可以不创建新的包,在当前provider包下),并新建下面的类:
package com.cherry.provider1.controller;  import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  @RestController @RequestMapping("product") public class ProductController {     @RequestMapping("list")     public String list(){                  return "这是product列表";     } } 
  1. 在resource中的配置文件中加入配置
server.port=8009 spring.application.name=my_provider1 #注册中心的地址 eureka.client.service-url.defaultZone=http://localhost:8001/eureka/ 
  1. 修改启动类,如下:
package com.cherry.provider1;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  @SpringBootApplication @EnableEurekaClient public class Provider1Application {     public static void main(String[] args) {                SpringApplication.run(Provider1Application.class, args);     } } 
  1. 打开注册中心
    在这里插入图片描述

创建网关

  1. 新建Module
    在这里插入图片描述在这里插入图片描述在这里插入图片描述2. 修改pom.xml文件
    在这里插入图片描述3. 修改配置文件application.properties,对网关进行配置
server.port=9001 spring.application.name=gateway spring.cloud.gateway.routes[0].id= gateway-service spring.cloud.gateway.routes[0].uri= http://localhost:8002 spring.cloud.gateway.routes[0].predicates[0]= Path=/building/welcome 
  1. 启动服务
    在这里插入图片描述

创建common子项目

在这里插入图片描述在这里插入图片描述

相关内容

热门资讯

透视总结!微乐小程序黑科技,微... 透视总结!微乐小程序黑科技,微信微乐游戏苹果辅助器(透视)总是真的有挂(哔哩哔哩)进入游戏-大厅左侧...
第四分钟教程!微乐小程序免费黑... 第四分钟教程!微乐小程序免费黑科技,微乐福建辅助器(开挂)果然真的是有挂(2026)微乐小程序免费黑...
据相关数据显示!微乐小程序有脚... 据相关数据显示!微乐小程序有脚本,微乐小程序黑科技,法子教程(有挂神器)-哔哩哔哩;微乐小程序有脚本...
据目击者称!wpk透视(透视)... 据目击者称!wpk透视(透视)wpk官网下载链接-好像是有挂(哔哩哔哩)1、据目击者称!wpk透视(...
透视总结!微乐小程序免费黑科技... 透视总结!微乐小程序免费黑科技,微乐游戏小程序辅助器免费(透视)总是有挂(哔哩哔哩)1、微乐游戏小程...
第7分钟教程!微乐小程序免费黑... 第7分钟教程!微乐小程序免费黑科技,微信微乐辅助插件(开挂)原来真的有挂(2026)1、每一步都需要...
随着!德普之星辅助器app(透... 随着!德普之星辅助器app(透视)德普之星透视辅助软件激活码-切实有挂(哔哩哔哩)所有人都在同一条线...
据统计!万能透视辅助器免费版,... 据统计!万能透视辅助器免费版,微乐小程序黑科技,法子教程(有挂辅助)-哔哩哔哩1、首先打开万能透视辅...
四分钟工具!微乐小程序免费黑科... 四分钟工具!微乐小程序免费黑科技,微乐自建房脚本入口(开挂)切实有挂(2026)1、微乐小程序免费黑...
无独有偶!微乐小程序黑科技,微... 无独有偶!微乐小程序黑科技,微信小程序微乐辅助免费版(透视)竟然存在有挂(哔哩哔哩)1、微信小程序微...