当前位置: 首页 > news >正文

SpringCloud系列(37)--搭建SpringCloud Gateway

前言:上一节中我们简单的介绍了一下关于SpringCloud Gateway的相关知识,而本节的主要内容是关于如果去搭建SpringCloud Gateway。

1、在搭建SpringCloud Gateway前需要先了解Gateway网关路由的两种配置方式
(1)在配置文件yml里进行配置
(2)在代码中注入RouteLocator的Bean

关于如何使用以上两种方式去搭建SpringCloud Gateway的官方文档: https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#configuration-properties


2、搭建gateway模块
(1)在父工程下新建模块

(2)选择模块的项目类型为Maven并选择模块要使用的JDK版本
(3)填写子模块的名称,然后点完成即可完成创建 

效果图:

(4)修改cloud-gateway-gateway9527子模块的pom.xml文件,然后reolad一下,下载依赖

例:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud01</artifactId><groupId>com.ken.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-gateway-gateway9527</artifactId><dependencies><!--gateway--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!--Eureka Clinet--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!--引入自己定义的api调用包,可以使用Payment模块的entity--><dependency><groupId>com.ken.springcloud</groupId><artifactId>api-commons</artifactId><version>${project.version}</version></dependency><!--热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!--lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies>
</project>

3、为子模块添加名为application的yml配置文件(注:yml是官方推荐的配置文件格式,最好使用yml文件而不是properties文件)

效果图:


如果application.yml不是绿色的,而是红色的,可以尝试install当前Maven工程,如果还不行可以尝试清除Idea的缓存

 


4、修改cloud-gateway-gateway9527模块的application.yml配置文件(这里我们先使用第一种方式即用配置文件yml来对Gateway网关路由进行配置)
server:port: 9527
spring:application:name: cloud-gatewaycloud:gateway:routes:#路由的ID,没有同定规则但要求唯一,建议配合服务名- id: payment_routh#断言,对与路径相匹配的进行路由,即对http://localhost:8001/payment/get/**下的路径进行路由predicates:- Path=/payment/get/**#匹配后提供服务的路由地址uri: http://localhost:8001#路由的ID,没有同定规则但要求唯一,建议配合服务名- id: payment_routh2#断言,对与路径相匹配的进行路由,即对http://localhost:8001/payment/lb/**下的路径进行路由predicates:- Path=/payment/lb/**#匹配后提供服务的路由地址uri: http://localhost:8001eureka:instance:hostname: cloud-gateway-serviceclient:#表示是否将自己注册进Eureka Server里,默认为trueregister-with-eureka: true#是否从Eureka Server抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡fetch-registry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka/

5、为cloud-gateway-gateway9527子模块新建一个主启动类,类名输入com.ken.springcloud.GateWayMain9527,然后创建即可

效果图:

6、编写GateWayMain9527主启动类
package com.ken.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {public static void main(String[] args) {SpringApplication.run(GateWayMain9527.class, args);}}

7、分别启动eureka-server7001、cloud-gateway-gateway9527、provider-payment8001

效果图:

8、在浏览器地址栏输入http://eureka7001.com:7001/然后进入eureka的界面查看cloud-gateway-gateway9527、provider-payment8001是否成功注册进eureka

9、在浏览器的地址栏里分别输入http://localhost:9527/payment/get/1、http://localhost:9527/payment/lb,通过调用接口可以看出我们成功的通过gateway来把请求从对cloud-gateway-gateway9527服务的访问转发到了对provider-payment8001服务的访问

效果图:

我是分割线 


在上述Gateway的搭建过程中,我们使用了编写yml的方式来对Gateway网关路由进行了配置,而在一开始我们就介绍了SpringCloud Gateway网关路由有两种配置方式,接下来则是介绍关于第二种路由配置方式,即如何在代码中注入RouteLocator的Bean来进行路由


10、在代码中注入RouteLocator的Bean在com.ken.springcloud包下新建一个名为config的包

效果图:

11、在config包下新建一个名为GateWayConfig的类

效果图:

12、编写GateWayConfig类
package com.ken.springcloud.config;import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class GateWayConfig {/*** 配置一个id为payment_routh的路由规则,当访问地址为http://localhost:9527/payment/get/**时会自动把请求转发到http://localhost:8001/payment/get/*** @param routeLocatorBuilder* @return*/@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();routes.route("payment_routh", r -> r.path("/payment/get/**").uri("http://localhost:8001")).build();return routes.build();}/*** 配置一个id为payment_routh2的路由规则,当访问地址为http://localhost:9527/payment/lb/**时会自动把请求转发到http://localhost:8001/payment/lb/*** @param routeLocatorBuilder* @return*/@Beanpublic RouteLocator customRouteLocator2(RouteLocatorBuilder routeLocatorBuilder) {RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();routes.route("payment_routh2", r -> r.path("/payment/lb/**").uri("http://localhost:8001")).build();return routes.build();}}

参数相关介绍: 

13、注释掉yml配置文件里的关于路由的相关配置
server:port: 9527
spring:application:name: cloud-gateway
#  cloud:
#    gateway:
#      routes:
#        #路由的ID,没有同定规则但要求唯一,建议配合服务名
#        - id: payment_routh
#          #断言,对与路径相匹配的进行路由,即对http://localhost:8001/payment/get/**下的路径进行路由
#          predicates:
#            - Path=/payment/get/**
#          #匹配后提供服务的路由地址
#          uri: http://localhost:8001
#        #路由的ID,没有同定规则但要求唯一,建议配合服务名
#        - id: payment_routh2
#          #断言,对与路径相匹配的进行路由,即对http://localhost:8001/payment/lb/**下的路径进行路由
#          predicates:
#            - Path=/payment/lb/**
#          #匹配后提供服务的路由地址
#          uri: http://localhost:8001eureka:instance:hostname: cloud-gateway-serviceclient:#表示是否将自己注册进Eureka Server里,默认为trueregister-with-eureka: true#是否从Eureka Server抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡fetch-registry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka/

14、重启cloud-gateway-gateway9527服务

效果图:

15、再次在浏览器的地址栏里分别输入http://localhost:9527/payment/get/1、http://localhost:9527/payment/lb,通过调用接口可以看出我们成功的通过gateway来把请求从对cloud-gateway-gateway9527服务的访问转发到了对provider-payment8001服务的访问

效果图:

相关文章:

  • 微信企业微网站网页设计代做
  • 网站开发 app域名搜索引擎
  • 做游戏视频网站有哪些怎么拿到百度推广的代理
  • 大网站是用什么做html5的seo免费教程
  • 余杭区建设规划局网站深圳企业黄页网
  • 网站的flash怎么做新闻发布会
  • 解释一下黑盒测试和白盒测试的区别?
  • 零基础入门Java+大模型(持续更新)
  • 创新让生活更美好丨“鑫亘科技亮相2025上海CMEF,创新医疗材料引领未来!”
  • 淘宝API安全合规指南:避免数据泄露与封禁
  • Encoder-only PLM RoBERTa ALBERT (BERT的变体)
  • 使用 Spread.net将 Excel 中的文本拆分为多段
  • EloqCloud for KV 初体验:兼容redis的云原生KV数据库
  • 《解锁前端潜力:自动化流程搭建秘籍》
  • 代码随想录day15二叉树3
  • 获取YARN application 应用列表的几种方法
  • 博图运动控制入门篇1-伺服组态和基本设置
  • Windows 安装 Redis8.0.2
  • 逆序对的数量
  • python的少数民族音乐网站系统
  • JVM调优实战 Day 6:JVM性能监控工具实战
  • DICOM 协议中DIMSE-C 和 DIMSE‑N 功能
  • Windows10中设置多个虚拟IP方法
  • vue2 点击按钮高亮显示不同项,再次点击隐藏相同项
  • Java8 Stream流:Stream流的思想和获取Stream流
  • 【AI】Manus自主产品原型设计