Solon Cloud Gateway 是基于 Solon Cloud、Vert.X 和 Solon-Rx(reactive-streams) 接口实现，响应式的接口体验。采用流式转发策略（性能好，内存少）。因为内置了 solon-server-vertx ，同时也支持 常规的 web 开发（v2.9.1后支持）。

<mark>提醒：不要再引入其它 http 的 solon-server-xxx （不然会冲突）。//已内置 solon-server-vertx</mark>


### 1、完整的配置说明（对应的配置结构类为：GatewayProperties）

```yaml
solon.cloud.gateway:
  discover:
    enabled: false
    excludedServices: 
      - "self-service"
  httpClient:
      responseTimeout: 1800 #单位：秒
  routes:
    - id: demo
      index: 0 #默认为0
      target: "http://localhost:8080" # 或 "lb://user-service"
      predicates:
        - "Path=/demo/**"
      filters:
        - "StripPrefix=1"
      timeout:
         responseTimeout: 1800 #单位：秒
  defaultFilters:
    - "AddRequestHeader=Gateway-Version,1.0"
```

配置项说明：


| 主要配置项             |  相关类型                        | 说明             | 
| --------------- | ---------------- | ----------- | 
| discover                   |                                       |  自动发现配置（基于 solon cloud discovery）  |
| - enabled                 |                                       |  是否启用自动发现                                      |
| - excludedServices    | String[]                            |  排除服务                                      |
| httpClient                 |                                       |  Http 客户端的默认超时（单位：秒）  |
| - connectTimeout     |                                       |  连接超时                                      |
| - requestTimeout     |                                        |  请求超时                                    |
| - responseTimeout   |                                        |  响应超时                                    |
| routes                     | Route[]                             | 路由                                          | 
| - id                         | String                               |  标识（必选）                            |
| - index                    | Int                                   |  顺序位                                       |
| - target                   | URI                                  |  目标（必选）                             |
| - predicates            | RoutePredicateFactory       |  检测器                                     |
| - filters                   | RouteFilterFactory              |   过滤器                                     |
| defaultFilters           | RouteFilterFactory              | 所有路由的默认过滤器                   |


target 目前支持的协议（可以添加 RouteHandler 进行扩展）：



| 协议         | 对应协议头 | 备注 |
| -------- | -------- | -------- |
| http 协议                   | `http://`、`https://`     |       |
| websocket 协议          | `ws://`、`wss://`     |       |
| lb 协议（负载均衡）     | `lb://`     |   Lb（负载均衡） 路由处理器<br/>找到节点后重新查找对应的路由处理器    |




### 2、配置示例

添加 solon-lib 和 solon-cloud-gateway 插件后就可以开始配置了。

* 手动配置示例

```yaml
solon.app:
  name: demo-gateway
  group: gateway

solon.cloud.gateway:
  routes:
    - id: demo
      target: "http://localhost:8080" #直接目标地址 或负载均衡地址 "lb://demo-service"
      predicates:
        - "Path=/demo/**"
      filters:
        - "StripPrefix=1"

```


* 自动发现配置示例（需要引入 [Solon Cloud Discovery 插件](/article/family-solon-cloud-discovery) ）


使用发现服务配置时。约定 path 的第一段为 serviceName。 


```yaml
solon.app:
  name: demo-gateway
  group: gateway

solon.cloud.nacos:
  server: "127.0.0.1:8848"   #以nacos为例

solon.cloud.gateway:
  discover:
    enabled: true
    excludedServices:
      - "self-service-name"
  defaultFilters:
      - "StripPrefix=1"
```


* 测试示例地址：

```
http://localhost:8080/demo/test/run?name=noear
```

