springcloud——gateway的入门

gateway三大核心概念:
1、路由(Route):路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由。
2、断言(Predicate):开发人员可以匹配Http请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由。
3、过滤(Filter):指的是spring框架中gatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。

在这里插入图片描述

第一个gateway程序

1、pom文件

<?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>springcloud</artifactId>
        <groupId>com.springcloud</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gateway-gateway9527</artifactId>

    <dependencies>

        <!--此版本号必须与spring-boot-parent的版本号一致,否则不能运行-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>com.springcloud</groupId>
            <artifactId>api-commons</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-hystrix</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

    </dependencies>


</project>

2、主配置文件

#设置端口号
server:
  port: 9527

#指定微服务的名称
spring:
  application:
    name: cloud-gateway
  
  cloud:
    gateway:
      routes:
        #设置路由的id
        - id: payment_routh
          #设置路由的路径
          uri: http://localhost:8081
          #设置路由的拦截点
          predicates:
            - Path=/payment/first/**

        #设置路由的id
        - id: payment_routh2
          #设置路由的路径
          uri: http://localhost:8081
          #设置路由的拦截点
          predicates:
            - Path=/payment/second/**


eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

  #设置主机名
  instance:
    instance-id: cloud-gateway-service

猜你喜欢

转载自blog.csdn.net/weixin_43925059/article/details/107764561