Spring Cloud微服务项目搭建系列文章(八):Spring Cloud gateway网关集成

上一篇Swagger处理我们讲解了API的处理,那么公共模块基本上我们处理的差不多了。

本文源码地址:

源码地址

这一篇我们主要介绍网关的集成,在Spring Cloud的体系里面网关主要有以下两种:

1、zuul

2、gateway

zuul

Spring Cloud 只支持1.X的版本,因为zuul2.x的版本一直没有出,所以才有Spring Cloud gateway的出现。所以Spring cloud是没有提供对zuul2.x的集成的,如果想使用2.x版本的得自己研究集成了。

对于zuul的1.x的版本在我的使用过程有一个比较严重的问题,就是不知道ws协议。

而且在性能上面也比Spring Cloud Gateway差点。毕竟这2者在技术实现上有一定的差别。

Spring Cloud Gateway

POM文件引入

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

注册中心按需引入即可,我这里使用的是nacos

        <!-- 注册中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.version}</version>
        </dependency>

新建启动类

/**
 * 启动类
 * @author 大仙
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayApplication {

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

}

这样网关集成就完事了,下面来说下YML配置,主要是针对路由相关的配置

    #开启自动路由
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: auth  #权限
          uri: lb://oauth2-server
          order: 0
          predicates:
            - Path=/auth/**
          filters:
            - StripPrefix=1

关于网关怎么集成认证中心,可以参考Gateway集成Oauth2

发布了183 篇原创文章 · 获赞 37 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/zhuwei_clark/article/details/104747080