• 使用SpringBoot开发一个API网关


    已经有很多开源的网关了,而且大公司一般都有自己的网关系统,但是有些场景也离不开我们自己需求定制,可以使用SpringCloud GateWay来满足我们业务的需求,即使需要在网关层实现一些具体的业务逻辑,我们也可以在开源的基础上进行二次开发。但如果我们只需要使用API网关核心的能力,同时需要在API层实现一些业务逻辑,我们基于SpringBoot自己来实现API网关。我们可以怎样来实现呢?通过结合实际业务需求以及对开源API网关的的学习。

    其实很简单,我们对着代码一步一步来:

    POM文件引入jar包

    <dependency>
    			<groupId>org.springframework.cloudgroupId>
    			<artifactId>spring-cloud-starter-gatewayartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    启动类

    @SpringBootApplication(scanBasePackages = {"com.xxx.xxx"})
    public class ThorGatewayApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(ThorGatewayApplication.class, args);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    路由配置

    这里是最重要的,后续的路由转发等都是根据这个文件

    server:
      port: 11618
    
    spring:
      application:
        name: quality-thor-gateway
      cloud:
        gateway:
          globalcors:
            cors-configurations:
              '[/**]':
                allowedOrigins: "*"
                allowedHeaders: "*"
                allowedMethods: "*"
          default-filters:
            - DedupeResponseHeader=Vary Access-Control-Allow-Origin Access-Control-Allow-Credentials, RETAIN_FIRST
          discovery:
            locator:
              enabled: true
    			routes:
          - id: projectID1
            predicates:
            - Path=/api/thor/core/web/**
            uri: projectURL1
          - id: projectID2
            predicates:
            - Path=/api/thor/perf/web/**
            uri:  projectURL2
    
    logging:
      level:
        org.springframework.cloud.gateway: debug
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    Docker in docker 实现
    防火墙firewalld
    jquery中的attr()和prop()的区别是什么?
    10.10作业
    ssh2-sftp-client实现前端项目自动部署
    Android如何实现实时音视频会议的背景分割
    centos 7无需token编译安装freeswitch 1.10.11 ——筑梦之路
    Javaweb-学习路线
    PostgreSQL 序列(Sequence)
    MyBatis-Plus更新字段为null时,update语句为null字段不生效的解决方法
  • 原文地址:https://blog.csdn.net/Kingsea442/article/details/134537805