1、引入gateway
在原来的项目中添加gateway模块

gateway是springcloud中的组件,所以要确保父项目的pom.xml中引入了springcloud

那么在gateway模块的pom.xml中引入gateway,如下:
<?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>springalibabanew</artifactId>
        <groupId>com.chinasofti</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>gateway</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--gateway的依赖springcloud开发-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>
</project>2、添加yml和启动类
yml配置如下:
server:
  port: 8088
spring:
  application:
    name: api-gateway
  #gateway配置
  cloud:
    gateway:
      #路由规则
      routes:
        - id: order_route #路由的唯一标识,路由到order
          uri: http://localhost:8010 #需要 转发的地址
          #断言规则,用于路由规则的匹配
          predicates:
            - Path=/order-serv/**
           #http://localhost:8088/order-serv/order/add路由到
            #http://localhost:8010/order-serv/order/add
          filters:
            - StripPrefix=1  #转发之前去掉第一层路径
            #http://localhost:8010/order/add
启动类:
package com.chinasofti;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * @Author mxx
 * @Date 2023/6/16 9:46
 * @Version 1.0
 */
@SpringBootApplication
public class GatewayApplication {
    public static void main(String args[]){
        SpringApplication.run(GatewayApplication.class,args);
    }
}
访问:

这样就路由到http://localhost:8010/order/add了


















