微服务网关之基础配置

804人浏览 / 0人评论

为什么要使用网关?

不用,会出现哪些问题?

  • 客户端会多次请求不同的微服务,增加了客户端的复杂性

  • 存在跨域请求,在一定场景下处理相对复杂

  • 认证复杂,每个服务都需要独立认证

  • 难以重构,随着项目的迭代,可能需要重新划分微服务。例如,可能将多个服务合并成一个或者将一个服务拆分成多个。如果客户端直接与微服务通信,那么重构将会很难实施

  • 某些微服务可能使用了防火墙 / 浏览器不友好的协议,直接访问会有一定的困难

微服务网关概述:

网关是介于客户端和服务器端之间的中间层,所有的外部请求都会先经过 网关这一层。也就是说,API 的实现方面更多的考虑业务逻辑,而安全、性能、监控可以交由 网关来做,这样既提高业务灵活性又不缺安全性。它是一个系统,通过暴露该微服务网关系统,方便我们进行相关的鉴权,安全控制,日志统一处理,易于监控的相关功能。

gateway官网:

https://spring.io/projects/spring-cloud-gateway

微服务网关架构如下:

以上,可以看出微服务网关的优势:

  • 安全 ,只有网关系统对外进行暴露,微服务可以隐藏在内网,通过防火墙保护。

  • 易于监控。可以在网关收集监控数据并将其推送到外部系统进行分析。

  • 易于认证。可以在网关上进行认证,然后再将请求转发到后端的微服务,而无须在每个微服务中进行认证。

  • 减少了客户端与各个微服务之间的交互次数

  • 易于统一授权。

微服务网关技术的种类:

  • nginx  高性能的HTTP和反向代理web服务器,同时提供了IMAP/POP3/SMTP服务

  • zuul ,Netflix 出品的基于 JVM 路由和服务端的负载均衡器。

  • spring-cloud-gateway, spring 出品的 基于spring 的网关项目,集成断路器,路径重写,性能比Zuul好。

搭建后台网关系统

1.创建Gateway工程,引入依赖,打包方式为pom

<!--网关依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

创建引导类

@SpringBootApplication
@EnableEurekaClient
public class GatewayWebApplication {

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

application.yml配置

spring:
  application:
    name: gateway-web
server:
  port: 8001
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka
  instance:
    prefer-ip-address: true
management:
  endpoint:
    gateway:
      enabled: true
    web:
      exposure:
        include: true

跨域配置

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]': # 匹配所有请求
              allowedOrigins: "*" #跨域处理 允许所有的域
              allowedMethods: # 支持的方法
                - GET
                - POST
                - PUT
                - DELETE

 

网关过滤设置

1.Host路由

      routes:
            - id: wiki_route
              uri: http://localhost:18081
              predicates:
              - Host=wiki.yiii.site**

测试请求:

http://wiki.yiii.site:8001/brand

2.路径匹配

      routes:
            - id: wiki_route
              uri: http://localhost:18081
              predicates:
              - Path=/brand/**

测试请求:

http://localhost:8001/brand

3.PrefixPath 过滤(给真实请求加一个统一前缀 )

routes:
            - id: wiki_route
              uri: http://localhost:18081
              predicates:
              #- Host=wiki.yiii.site**
              - Path=/**
              filters:
              #- PrefixPath=/brand
              - StripPrefix=1

测试请求:

http://localhost:8001

#实际请求
http://localhost:8001/brand

4.StripPrefix 过滤配置(使用SttripPrefix功能来实现路径的过滤操作 )

routes:
            - id: wiki_route
              uri: http://localhost:18081
              predicates:
              #- Host=wiki.yiii.site**
              - Path=/**
              filters:
              #- PrefixPath=/brand
              - StripPrefix=1

测试请求:

http://localhost:8001/api/brand

#真实请求
http://localhost:8001/brand

 

全部评论