springcloud(5)-实现网关gateway

zuul和gateway的区别可查阅其它资料

期望
通过gateway统一去访问consumer、provider的服务,
例如之前的consumer访问 http://127.0.0.1:8002/dept/list
通过网关 http://127.0.0.1:9527/consumer/dept/list 同样可以访问

实现

创建spring boot项目

springcloud(5)-实现网关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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>cloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>cloud-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--网关gateway相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!--eureka相关依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--断路器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application配置

server:
  port: 9527

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #当访问http://网关地址/服务名称(大写)/**地址会自动转发到http://服务名称(大写)/**地址,如果为false就不会自动转发
          lowerCaseServiceId: true   #为true表示服务名称(小写)
      routes:
        # 方法一: 不建议使用
        - id: cloud-provider #路由的ID,没有固定规则,但要求唯一,建议配合服务名
          uri: http://localhost:8001 #匹配后提供服务的路由地址
          predicates:
            - Path=/provider/**
          filters:
            - StripPrefix=1
        # 方法二:实际生产中使用
        - id: cloud-consumer
          uri: lb://cloud-consumer #匹配后提供服务的路由地址
          predicates:
            - Path=/consumer/** #断言,路径相匹配的进行路由
          filters:
            - StripPrefix=1

只需要配置这些即可
开启所有服务
springcloud(5)-实现网关gateway
访问provider
springcloud(5)-实现网关gateway
consumer通过feign访问provider
springcloud(5)-实现网关gateway
通过网关访问consumer、provider
springcloud(5)-实现网关gateway
springcloud(5)-实现网关gateway
解析:通过网关访问上述接口时

# 方法二:实际生产中使用
        - id: cloud-consumer
          uri: lb://cloud-consumer #匹配后提供服务的路由地址
          predicates:
            - Path=/consumer/** #断言,路径相匹配的进行路由
          filters:
            - StripPrefix=1

StripPrefix=1 将上述断言放行的url请求过滤之后(即截取路径前缀的第一部分,然后用剩余部分和下面的url进行拼接产生路由地址,
lb:表示是负载均衡协议 ,cloud-consumer是发布的微服务的服务名
比如http://localhost:9527/consumer/dept/list,根据/consumer/**截取,将/dept/list与 lb://cloud-consumer 的路由地址拼接
最后实际访问的地址是 http://localhost:8002/dept/list

上一篇:Spring Boot Serverless 实战系列“部署篇” | Mall 应用


下一篇:SpringCloud:Gateway组件