gateway不仅能转发api接口请求,也能转发页面请求,返回页面。

springcloud2 gateway 之一:简单样例
https://blog.csdn.net/haveqing/article/details/88424598

springcloud eureka注册中心搭建
https://blog.csdn.net/haveqing/article/details/86479813

eureka 客户端springcloud service
https://blog.csdn.net/haveqing/article/details/88427366

文件结构

一、pom.xml

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.urthink.upfs</groupId>
    <artifactId>springcloud-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-gateway</name>
    <description>springcloud-gateway project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

二、SpringcloudGatewayApplication.java

package com.urthink.upfs.springcloudgateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * http://localhost:8081/demo/test1
 * http://localhost:8080/app1/demo/test1
 * http://localhost:8082/demo/test1
 * http://localhost:8080/app2/demo/test1
 */
@SpringBootApplication
public class SpringcloudGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudGatewayApplication.class, args);
    }

}

三、application.yml

server:
  port: 8080

eureka:
  instance:
    prefer-ip-address: true
    #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(客户端告诉服务端自己会按照该规则),默认30
    lease-renewal-interval-in-seconds: 10
    #Eureka服务端在收到最后一次心跳之后等待的时间上限,单位为秒,超过则剔除(客户端告诉服务端按照此规则等待自己),默认90
    lease-expiration-duration-in-seconds: 12
  client:
    registry-fetch-interval-seconds: 10 #eureka client刷新本地缓存时间,默认30
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: gateway
  cloud:
    gateway:
      routes:
        #netty 路由过滤器,http或https开头
      - id: app1-route
        uri: http://localhost:8081
        predicates:
        - Path=/app1/**
        filters:
          #转发请求时去掉1级前缀
        - StripPrefix=1
        #LoadBalancerClient负载均衡过滤器(整合eureka注册中心),lb开头
      - id: app2-route
        uri: lb://springcloud-service
        predicates:
        - Path=/app2/**
        filters:
        #转发请求时去掉1级前缀
        - StripPrefix=1

四、访问

http://localhost:8082/demo/test1
http://localhost:8080/app2/demo/test1

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐