GateWay網關 - 環境搭建v2

 

目錄

基礎使用

Maven依賴 

application配置

Gateway整合Nacos實現服務轉發

Maven依賴

application配置

自定義TokenFilter實現參數攔截

gateWay高可用集羣方式


基礎使用

Maven依賴 

  注意:不能引入 spring-cloud-starter-web,會出現錯誤


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
        <version>2.0.0.RELEASE</version>
    </dependency>
</dependencies>

application配置

server:
  port: 80
####服務網關名稱
spring:
  application:
    name: mayikt-gateway
  cloud:
    gateway:
        ###路由策略
      routes:
        ###路由id
        - id: mayikt
          ####轉發http://www.mayikt.com/
          uri: http://www.mayikt.com/
          ###匹配規則  
          predicates:
            - Path=/mayikt/**
             ###瀏覽器請求127.0.0.1/mayikt會轉發到http://www.mayikt.com/
           

瀏覽器輸入127.0.0.1/mayikt會轉發到http://www.mayikt.com/


Gateway整合Nacos實現服務轉發

Maven依賴

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
        <version>2.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>0.2.2.RELEASE</version>
    </dependency>
</dependencies>

application配置

server:
  port: 80
####服務網關名稱
spring:
  application:
    name: mayikt-gateway
  cloud:
    gateway:
      discovery:
        locator:
          ####開啓以服務id去註冊中心上獲取轉發地址
          enabled: true
        ###路由策略
      routes:
        ###路由id
        - id: mayikt
          ####轉發http://www.mayikt.com/
          uri: http://www.mayikt.com/
          ###匹配規則
          predicates:
            - Path=/mayikt/**
        ###路由id
        - id: member
          #### 基於lb負載均衡形式轉發,mayikt-member爲nacos中的會員服務名稱
          uri: lb://mayikt-member
          filters:
            - StripPrefix=1
          ###匹配規則
          predicates:
            - Path=/member/**
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    ### nacos註冊中心地址

自定義TokenFilter實現參數攔截

案例:攔截所求請求,如果請求參數不包含token則返回500響應碼

@Component
public class TokenFilter implements GlobalFilter {
    
    // 攔截所求請求,如果請求參數不包含token則返回500響應碼
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token = exchange.getRequest().getQueryParams().getFirst("token");
        if (token == null || token.isEmpty()) {
            // 封裝響應內容
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.BAD_REQUEST);
            String msg = "token not is null "; 
            DataBuffer buffer = response.bufferFactory().wrap(msg.getBytes());
            return response.writeWith(Mono.just(buffer));
        }
        // 使用網關過濾
        return chain.filter(exchange); // 放行
    }

}

gateWay高可用集羣方式:

https://mp.csdn.net/postedit/104088145

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章