【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子项目

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

相关内容

热门资讯

2027版开挂!锦州麻将开挂多... 2027版开挂!锦州麻将开挂多功能AI教程,锦州麻将其实有开挂教程该软件可以轻松地帮助玩家将锦州麻将...
2027版辅助!欢喜广西麻将辅... 2027版辅助!欢喜广西麻将辅助多功能AIapp,欢喜广西麻将一直有辅助app1、任何欢喜广西麻将透...
2027版外挂!中至九江麻将外... 2027版外挂!中至九江麻将外挂多功能AI攻略,中至九江麻将真是有外挂攻略运中至九江麻将辅助工具,进...
2027版作弊器!开心门一番作... 2027版作弊器!开心门一番作弊器多功能AI工具,开心门一番竟然真的有作弊器工具1、开心门一番免费脚...
2027版开挂!羊城牌乐汇开挂... 2027版开挂!羊城牌乐汇开挂多功能AI软件,羊城牌乐汇好像是有开挂软件小薇(辅助器软件下载)致您一...
2027版透视器!朋友安徽麻将... 2027版透视器!朋友安徽麻将透视器多功能AI攻略,朋友安徽麻将其实真的是有透视器攻略1、起透看视 ...
2027版透视器!紫竹休闲透视... 2027版透视器!紫竹休闲透视器多功能AI方法,紫竹休闲其实是有透视器方法1、点击下载安装,紫竹休闲...
2027版开挂!广安欢欢麻将开... 2027版开挂!广安欢欢麻将开挂多功能AI脚本,广安欢欢麻将果然有开挂脚本1、打开软件启动之后找到中...
2027版开挂!微乐三丁拐开挂... 2027版开挂!微乐三丁拐开挂多功能AI方法,微乐三丁拐切实存在有开挂方法1、下载好微乐三丁拐正确养...
2027版透视挂!喜临门透视挂... 2027版透视挂!喜临门透视挂多功能AI攻略,喜临门一直真的有透视挂攻略1、完成喜临门有辅助插件,帮...