spring cloud網關gateway

spring gateway使用基於netty異步io,第二代網關;
zuul 1使用servlet 3,第一代網關,每一個請求一個線程,同步Servlet,多線程阻塞模型。
而spring貌似不想在支持zuul 2了java

API網關做爲後端服務的統一入口,可提供請求路由、協議轉換、安全認證、服務鑑權、流量控制、日誌監控等服務。web

1.搭一個簡單的網關

idea中file-new-projectspring

pom.xml文件(引入eureka)apache

<?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.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>gatewaytest2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gatewaytest2</name>
    <description>Demo project for Spring Boot</description>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </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>

</project>

 

applicaton.yml文件配置後端

spring:
  application:
    name: gateway8710
  cloud:
    gateway:
      default-filter:
      routes:
      - id: user-server
        predicates:
          - Path=/java/**
        filters:
          - StripPrefix=1
        uri: lb://service-helloword
        #  uri: "http://192.168.111.133:8708/project/hello"
server:
  port: 8710
eureka:
  client:
    serviceUrl:
      #指向註冊中心
      defaultZone: http://192.168.111.133:8888/eureka/
  instance:
    # 每間隔1s,向服務端發送一次心跳,證實本身依然」存活「
    lease-renewal-interval-in-seconds: 1
    # 告訴服務端,若是我2s以內沒有給你發心跳,就表明我「死」了,將我踢出掉。
    lease-expiration-duration-in-seconds: 2

注意:uri項中的lb第一個字母L的小寫,配置這個表示要去eureka中查找相關的服務安全

StripPrefix=1表示去掉url中的前綴,如http://localhost:8710/java/project/hello,通過網關轉後變成http://192.168.111.133:8708/project/hello,即去掉前綴/java,後面的不變,去註冊中心找service-helloword服務的地址和端口多線程

當配置uri使用絕對路徑時寫了項目路徑(/project/hello)uri: "http://192.168.111.133:8708/project/hello",無論請求的是路徑是什麼(http://localhost:8710/java/xxx/xxx),都直接訪問配置的地址http://192.168.111.133:8708/project/helloapp

當配置uri使用絕對路徑時沒寫項目路徑(/project/hello)如uri: "http://192.168.111.133:8708",請求http://localhost:8710/java/xxx/xxx轉發後變爲http://192.168.111.133:8708/xxx/xxx異步

 

啓動類Gatewaytest2Application.javaxss

package com.example.gatewaytest2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class Gatewaytest2Application {

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

}

目錄結構

2.啓動測試

註冊中心已經有咱們的網關服務了

 

 

測試

返回

 

 

 3.附service-helloword

controller

package com.pu.helloworld;

import com.pu.common.ExcelUtil;
import com.pu.ioctest.IocTest;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;


@RestController
@RequestMapping("/project")
public class helloController {
    private static Logger log = LoggerFactory.getLogger(helloController.class);

    @RequestMapping(value = "/hello")
    //required=false 表示url中能夠不傳入id參數,此時就使用默認參數
    public String say(@RequestParam(value = "id", required = false, defaultValue = "hello world") String input) {
        log.debug("your input :" + input);
        return "your input :" + input;
    }
}
相關文章
相關標籤/搜索