Skip to content

Commit

Permalink
Merge branch 'main' into fix/kind-and-o11y
Browse files Browse the repository at this point in the history
  • Loading branch information
CH3CHO authored Jul 5, 2024
2 parents 020009a + 6beabbd commit 7ca6fb4
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ custom_edit_url: https://github.com/higress-group/higress-group.github.io/blob/m
| v1.2.0 | 支持作为 Knative 的网络层使用;支持 ARM 架构部署;支持对接 Consul | 2023-08 |
| v1.3.0 | 完整支持 GatewayAPI;支持 Operator 模式部署 | 2023-11 |
| v1.4.0 | WASM 支持 redis 调用;简化 HTTPS 证书管理 | 2024-05 |
| v2.0.0 | Istio/Envoy 内核版本升级,更丰富的 API 网关能力 | 2024-06 |
| v2.0.0 | Istio/Envoy 内核版本升级,更丰富的 API 网关能力 | 2024-07 |
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,208 @@ rejected_msg: '{"code":-1,"msg":"Too many requests"}'
redis:
service_name: redis.static
```
## 完整示例
AI Token 限流插件依赖 Redis 记录剩余可用的 token 数,因此首先需要部署 Redis 服务。
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
labels:
app: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis
ports:
- containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
name: redis
labels:
app: redis
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
---
```

在本例中,使用通义千问作为 AI 服务提供商。另外还需要设置 AI 统计插件,因为 AI Token 限流插件依赖 AI 统计插件计算每次请求消耗的 token 数,以下配置限制每分钟的 input 和 output token 总数为 200 个。

```yaml
apiVersion: extensions.higress.io/v1alpha1
kind: WasmPlugin
metadata:
name: ai-proxy
namespace: higress-system
spec:
matchRules:
- config:
provider:
type: qwen
apiTokens:
- "<YOUR_API_TOKEN>"
modelMapping:
'gpt-3': "qwen-turbo"
'gpt-35-turbo': "qwen-plus"
'gpt-4-turbo': "qwen-max"
'*': "qwen-turbo"
ingress:
- qwen
url: oci://higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/wasm-go-ai-proxy:v1.0.0
phase: UNSPECIFIED_PHASE
priority: 100
---
apiVersion: extensions.higress.io/v1alpha1
kind: WasmPlugin
metadata:
name: ai-statistics
namespace: higress-system
spec:
defaultConfig:
enable: true
url: oci://higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/wasm-go-ai-token-statistics:v1.0.0
phase: UNSPECIFIED_PHASE
priority: 200
---
apiVersion: extensions.higress.io/v1alpha1
kind: WasmPlugin
metadata:
name: ai-token-ratelimit
namespace: higress-system
spec:
defaultConfig:
rule_name: default_limit_by_param_apikey
rule_items:
- limit_by_param: apikey
limit_keys:
- key: 123456
token_per_minute: 200
redis:
# 默认情况下,为了减轻数据面的压力,Higress 的 global.onlyPushRouteCluster 配置参数被设置为 true,意味着不会自动发现 Kubernetes Service
# 如果需要使用 Kubernetes Service 作为服务发现,可以将 global.onlyPushRouteCluster 参数设置为 false,
# 这样就可以直接将 service_name 设置为 Kubernetes Service, 而无须为 Redis 创建 McpBridge 以及 Ingress 路由
# service_name: redis.default.svc.cluster.local
service_name: redis.dns
service_port: 6379
url: oci://higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/wasm-go-ai-token-ratelimit:v1.0.0
phase: UNSPECIFIED_PHASE
priority: 600
```
注意,AI Token 限流插件中的 Redis 配置项 `service_name` 来自 McpBridge 中配置的服务来源,另外我们还需要在 McpBridge 中配置通义千问服务的访问地址。

```yaml
apiVersion: networking.higress.io/v1
kind: McpBridge
metadata:
name: default
namespace: higress-system
spec:
registries:
- domain: dashscope.aliyuncs.com
name: qwen
port: 443
type: dns
- domain: redis.default.svc.cluster.local # Kubernetes Service
name: redis
type: dns
port: 6379
```

分别创建两条路由规则。

```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
higress.io/backend-protocol: HTTPS
higress.io/destination: qwen.dns
higress.io/proxy-ssl-name: dashscope.aliyuncs.com
higress.io/proxy-ssl-server-name: "on"
labels:
higress.io/resource-definer: higress
name: qwen
namespace: higress-system
spec:
ingressClassName: higress
rules:
- host: qwen-test.com
http:
paths:
- backend:
resource:
apiGroup: networking.higress.io
kind: McpBridge
name: default
path: /
pathType: Prefix
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
higress.io/destination: redis.dns
higress.io/ignore-path-case: "false"
labels:
higress.io/resource-definer: higress
name: redis
spec:
ingressClassName: higress
rules:
- http:
paths:
- backend:
resource:
apiGroup: networking.higress.io
kind: McpBridge
name: default
path: /
pathType: Prefix
```

触发限流效果如下:

```bash
curl "http://qwen-test.com:18000/v1/chat/completions?apikey=123456" -H "Content-Type: application/json" -d '{
"model": "gpt-3",
"messages": [
{
"role": "user",
"content": "你好,你是谁?"
}
],
"stream": false
}'
{"id":"88cfa80f-545d-93b4-8ff3-3f5245ca33ba","choices":[{"index":0,"message":{"role":"assistant","content":"我是通义千问,由阿里云开发的AI助手。我可以回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?"},"finish_reason":"stop"}],"created":1719909825,"model":"qwen-turbo","object":"chat.completion","usage":{"prompt_tokens":13,"completion_tokens":33,"total_tokens":46}}
curl "http://qwen-test.com:18000/v1/chat/completions?apikey=123456" -H "Content-Type: application/json" -d '{
"model": "gpt-3",
"messages": [
{
"role": "user",
"content": "你好,你是谁?"
}
],
"stream": false
}'
Too many requests # 限流成功
```
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ custom_edit_url: https://github.com/higress-group/higress-group.github.io/blob/m
| global.defaultUpstreamConcurrencyThreshold | 单个数据面实例和后端服务之间的最大并发(不同服务独立计算),注意过多并发可能导致网关内存过高,请相应调高数据面内存限制 | 10000 |
| global.o11y.enabled | 若为 true,将同时安装可观测性套件(Grafana、Promethues、Loki、PromTail 等)。 | false |
| global.pvc.rwxSupported | RwxSupported 参数用于指示是否支持读写多个 Pod,即是否支持共享卷。如果该参数设置为 true,则表明支持共享卷,多个 Pod 可以同时挂载该 PVC,进行读写操作。如果设置为 false,则表明不支持共享卷,只有一个 Pod 可以挂载该 PVC 进行读写操作。 | true |
| global.onlyPushRouteCluster | 若为`true`,Higress Controller 将会只推送被路由关联的服务 | true |

## meshConfig参数
| 参数 | 参数说明 | 默认值 |
Expand Down Expand Up @@ -67,7 +68,6 @@ custom_edit_url: https://github.com/higress-group/higress-group.github.io/blob/m
| higress-core.gateway.affinity | 亲和性,用于控制 gateway容器如何调度,使其与其他 Pod 或节点保持亲和或反亲和。 | {} |
| higress-core.gateway.networkGateway | 用于指定网络网关的名称或 IP 地址。 | "" |


## Controller参数
| 参数 | 参数说明 | 默认值 |
|--------------------------------------------------------|----------------------------------------------------------------------|-----------|
Expand Down Expand Up @@ -130,16 +130,13 @@ custom_edit_url: https://github.com/higress-group/higress-group.github.io/blob/m
| higress-core.pilot.configMap | 安装由values.yaml生成的 mesh config ,如果为 false ,则 Pilot 将使用默认值(默认情况下)或用户提供的值,如果为 false ,则 Pilot 将使用默认值(默认情况下)或用户提供的值 | true |
| higress-core.pilot.podLabels | 为 Pod 添加额外的标签,以用于监控和日志记录配置。 | {} |



## skywalking 参数
## SkyWalking参数
| 参数 | 参数说明 | 默认值 |
|-----------------------------------------|------------------------------|-----------|
| higress-core.skywalking.enabled | 是否启用 SkyWalking | false |
| higress-core.skywalking.service.address | SkyWalking 服务地址,如果不指定则使用默认值 | ~ |
| higress-core.skywalking.service.port | SkyWalking 服务端口,默认为 11800 | 11800 |


## 控制台参数
| 参数 | 参数说明 | 默认值 |
|------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------|------------------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ custom_edit_url: https://github.com/higress-group/higress-group.github.io/blob/m
# Mcp Bridge 配置说明

## McpBridge 字段说明
| 字段 | 类型 | 说明 | 示例值 | 是否必填 |
| --- | --- | --- | --- | --- |
| registries | RegistryConfig 数组 | 支持配置多个不同注册中心的服务来源 | [] ||
| 字段 | 类型 | 说明 | 示例值 | 是否必填 |
|------------| --- |-------------------|---------|-------------------|
| registries | RegistryConfig 数组 | 支持配置多个不同注册中心的服务来源 | [] ||
| name | 字符串 | McpBridge 名称 | default | 是,当前该值只能是 default |

## RegistryConfig 字段说明
| 字段 | 类型 | 说明 | 示例值 | 是否必填 |
Expand Down
53 changes: 12 additions & 41 deletions i18n/zh-cn/docusaurus-plugin-content-docs/current/user/wasm-go.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ hello world
### 无配置插件
插件无需配置时,直接定义空结构体即可

```
```go
package main

import (
Expand All @@ -410,7 +410,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log wrapper.
### 在插件中请求外部服务
目前仅支持 http 调用,支持访问在网关控制台中设置了服务来源的 Nacos、K8s 服务,以及固定地址或 DNS 来源的服务。请注意,无法直接使用`net/http`库中的 HTTP client,必须使用如下例中封装的 HTTP client。<br />下面例子中,在配置解析阶段解析服务类型,生成对应的 HTTP client ;在请求头处理阶段根据配置的请求路径访问对应服务,解析应答头,然后再设置在原始的请求头中。

```
```go
package main

import (
Expand Down Expand Up @@ -449,48 +449,19 @@ func parseConfig(json gjson.Result, config *MyConfig, log wrapper.Log) error {
if config.requestPath == "" {
return errors.New("missing requestPath in config")
}
serviceSource := json.Get("serviceSource").String()
// 固定地址和dns类型的serviceName,为控制台中创建服务时指定
// nacos和k8s来源的serviceName,即服务注册时指定的原始名称
// 带服务类型的完整 FQDN 名称,例如 my-svc.dns, my-svc.static, service-provider.DEFAULT-GROUP.public.nacos, httpbin.my-ns.svc.cluster.local
serviceName := json.Get("serviceName").String()
servicePort := json.Get("servicePort").Int()
if serviceName == "" || servicePort == 0 {
return errors.New("invalid service config")
}
switch serviceSource {
case "k8s":
namespace := json.Get("namespace").String()
config.client = wrapper.NewClusterClient(wrapper.K8sCluster{
ServiceName: serviceName,
Namespace: namespace,
Port: servicePort,
})
return nil
case "nacos":
namespace := json.Get("namespace").String()
config.client = wrapper.NewClusterClient(wrapper.NacosCluster{
ServiceName: serviceName,
NamespaceID: namespace,
Port: servicePort,
})
return nil
case "ip":
config.client = wrapper.NewClusterClient(wrapper.StaticIpCluster{
ServiceName: serviceName,
Port: servicePort,
})
return nil
case "dns":
domain := json.Get("domain").String()
config.client = wrapper.NewClusterClient(wrapper.DnsCluster{
ServiceName: serviceName,
Port: servicePort,
Domain: domain,
})
return nil
default:
return errors.New("unknown service source: " + serviceSource)
if servicePort == 0 {
if strings.HasSuffix(serviceName, ".static") {
// 静态IP类型服务的逻辑端口是80
servicePort = 80
}
}
config.client = wrapper.NewClusterClient(wrapper.FQDNCluster{
FQDN: serviceName,
Port: servicePort,
})
}

func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log wrapper.Log) types.Action {
Expand Down

0 comments on commit 7ca6fb4

Please sign in to comment.