7、微服务组件gateway

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了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱人间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值