cloud-security-wiki/docs/module/gateway/README.md

2.2 KiB
Raw Blame History

网关服务模块

在集群环境中gateway将会是集群网络的唯一出入口是系统安全的重要节点同时gateway承担Sentinel限流服务分发负载均衡和提供swagger文档等工作。

文件结构

gateway

└── java
     ├──  config -- 配置包
     ├── controller -- swagger聚合需要使用的到的接口
     ├── filter -- 全局过滤器
     ├── handler -- 异常处理
     ├── util -- 工具
     └── GatewayApplication.java -- 启动类
└── resources
     ├── bootstrap.yml -- 配置文件
     └── logback-spring.xml -- 日志记录文件

配置文件

# Tomcat
server:
  port: 5000

# Spring
spring:
  application:
    # 应用名称
    name: monitor
  profiles:
    # 环境配置
    active: local
  security:
    user:
      name: root
      password: password
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 192.168.101.108:8848
        username: nacos
        password: nacos
        namespace: ${spring.profiles.active}
        metadata:
          management:
            context-path: /actuator
      config:
        # 配置中心地址
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        file-extension: yaml
        namespace: ${spring.profiles.active}
        shared-configs:
          - ${spring.application.name}-${spring.profiles.active}.yml

配置文件相当简洁,主要为设置运行端口,active的环境以及注册到nacos,并从nacos读取到gateway自身的配置文件

属于gateway的配置文件:

spring:
  cloud:
    gateway:
      discovery:
        locator:
          # gateway 能够进行服务发现
          enabled: true
      routes:
        # 认证中心
        - id: auth
          uri: lb://auth
          predicates:
            - Path=/auth/**

路由分发

路由分发是gateway的一个重要功能,当gateway启动后会向nacos发起注册,并从nacos cconfig中心获取到gateway的那一份配置文件,gateway再将文件加载到自己的路由中,当又接口请求到后,gateway则可以根据路由表对请求进行分发。