Spring Cloud Gateway替代zuul作为API网关的方法

本文简要介绍如何使用Spring Cloud Gateway 作为API 网关(不是使用zuul作为网关),关于Spring Cloud Gateway和zuul的性能比较本文不再赘述,基本可以肯定Spring Cloud Finchley版本的gateway比zuul 1.x系列的性能和功能整体要好。

特别提醒:Spring Cloud Finchley版本中,即使你引入了spring-cloud-starter-netflix-zuul,也不是2.0版本的zuul

        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        

我们的介绍分为3个部分, 第一,pom文件,第二项目的基本架构,第三源码和截图。

第一,pom文件

因为使用Eureka作为服务注册和发现,因此在pom中引入了eureka,各位可根据自己的实际情况修改。


    4.0.0

    com.yq
    GatewayDemo
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
                import
            
        
    

    
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        

        
            org.projectlombok
            lombok
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            com.alibaba
            fastjson
            1.1.33
        

        
            org.apache.commons
            commons-lang3
            3.5
        

    


    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


第二,项目结构

总共有3个项目,
第一个项目是eureka 注册中心,非常简单,基本就一个Application类。 端口7700
第二个项目是User service,也非常简单提供两个rest api,为了简略不连接数据库,直接在内存中生成一组数据。端口6601
第三个项目就是我们的网关。端口6604

目前项目中集成websocket服务配置,本文暂不介绍可直接忽略。

第三,项目代码和运行截图

网关的主代码

package com.yq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;


@SpringCloudApplication
public class APIGatewayApplication  {
    private static final Logger logger = LoggerFactory.getLogger(APIGatewayApplication.class);

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/baidu")
                        .uri("http://baidu.com:80/")
                )
               .route("websocket_route", r -> r.path("/apitopic1/**")
                .uri("ws://127.0.0.1:6605"))
                .route(r -> r.path("/userapi3/**")
                        .filters(f -> f.addResponseHeader("X-AnotherHeader", "testapi3"))

                        .uri("lb://user-service/")
                )
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(APIGatewayApplication.class, args);
        logger.info(" Start APIGatewayApplication Done");
    }

}

网关的配置文件application.yml

server:
  port: 6604


#服务名
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      filter:
        remove-non-proxy-headers:
          headers:
          - dummy
      routes:
        - id:  apiuser
        # 重点!/info必须使用http进行转发,lb代表从注册中心获取服务
          uri: lb://user-service
          predicates:
          # 重点!转发该路径!,/userapi/**,
          - Path=/userapi/**
          # http://localhost:6601/userapi/user/users/2, 必须加上StripPrefix=1,否则访问服务时会带上userapi
          #而不是我们期望的去掉userapi,只保留**部分
          filters:
          - StripPrefix=1
        - id:  api2user
          uri: lb://user-service
          predicates:
          - Path=/userapi2/**
          filters:
          - StripPrefix=1


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7700/eureka/

我们简要分析分析一下配置文件

  • id: apiuser

# 重点!/info必须使用http进行转发,lb代表从注册中心获取服务
uri: lb://user-service
predicates:
# 重点!转发该路径!,/userapi/,
- Path=/userapi/

# http://localhost:6601/userapi/user/users/2, 必须加上StripPrefix=1,否则访问服务时会带上userapi
#而不是我们期望的去掉userapi,只保留**部分
filters:
- StripPrefix=1

配置了一个路由apiuser, 当路径( - Path=/userapi/**),就转发到服务(lb://user-service),同时把路径中的userapi这部分去掉(- StripPrefix=1)。

运行效果图

直接访问User service
http://localhost:6601/user/users/2

通过网关访问user service
http://localhost:6604/userapi/user/users/2

Spring Cloud Gateway替代zuul作为API网关的方法_第1张图片

参考文档:

1, http://cloud.spring.io/spring-cloud-static/Finchley/single/spring-cloud.html#_spring_cloud_gateway
2, https://github.com/spring-cloud-samples/spring-cloud-gateway-sample

到此这篇关于Spring Cloud Gateway替代zuul作为API网关的文章就介绍到这了,更多相关Spring Cloud Gateway替代zuul作为API网关内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Spring Cloud Gateway替代zuul作为API网关的方法)