Comments (8)
may I have a try for this,write a go wasm plugin
from higress.
We will implement this functionality through plugins instead of annotations
This feature can be implemented by using routeCluster in the plugin and using it with httpcall.
Interested developers are welcome to contribute.
reference:
routeCluster:
httpCall:
from higress.
@haifzhu
will finished this weekend, thanks a lot.
from higress.
@haifzhu 有点问题,应该在回调里迭代处理下一个trypath
from higress.
@OnlyPiglet Are you finished the feature, i also have the need for it。
from higress.
package main
import (
"errors"
"net/http"
"strings"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/gjson"
)
var defaultTimeout uint32 = 60000
func main() {
wrapper.SetCtx(
"try-paths",
wrapper.ParseConfigBy(parseConfig),
wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders),
)
}
// 自定义插件配置
type TryPathsConfig struct {
host string
port int64
tryPaths []string
code int
client wrapper.HttpClient
}
func Client(json gjson.Result) (wrapper.HttpClient, error) {
serviceSource := json.Get("serviceSource").String()
serviceName := json.Get("serviceName").String()
servicePort := json.Get("servicePort").Int()
if serviceName == "" || servicePort == 0 {
return nil, errors.New("invalid service config")
}
switch serviceSource {
case "k8s":
namespace := json.Get("namespace").String()
return wrapper.NewClusterClient(wrapper.K8sCluster{
ServiceName: serviceName,
Namespace: namespace,
Port: servicePort,
}), nil
case "nacos":
namespace := json.Get("namespace").String()
return wrapper.NewClusterClient(wrapper.NacosCluster{
ServiceName: serviceName,
NamespaceID: namespace,
Port: servicePort,
}), nil
case "ip":
return wrapper.NewClusterClient(wrapper.StaticIpCluster{
ServiceName: serviceName,
Port: servicePort,
}), nil
case "dns":
domain := json.Get("domain").String()
return wrapper.NewClusterClient(wrapper.DnsCluster{
ServiceName: serviceName,
Port: servicePort,
Domain: domain,
}), nil
default:
return nil, errors.New("unknown service source: " + serviceSource)
}
}
func parseConfig(json gjson.Result, config *TryPathsConfig, log wrapper.Log) error {
// 解析出配置,更新到config中
for _, result := range json.Get("tryPaths").Array() {
config.tryPaths = append(config.tryPaths, result.String())
}
// code默认值为404
if json.Get("code").String() == "" {
config.code = http.StatusNotFound
} else {
config.code = int(json.Get("code").Int())
}
client, err := Client(json)
if err != nil {
return err
}
config.client = client
return nil
}
func convHttpHeadersToStruct(responseHeaders http.Header) [][2]string {
headerStruct := make([][2]string, len(responseHeaders))
i := 0
for key, values := range responseHeaders {
headerStruct[i][0] = key
headerStruct[i][1] = values[0]
i++
}
return headerStruct
}
func onHttpRequestHeaders(ctx wrapper.HttpContext, config TryPathsConfig, log wrapper.Log) types.Action {
path := ctx.Path()
log.Infof("onHttpRequestHeaders path: %s, config: %s", path, config)
for _, requestPath := range config.tryPaths {
requestPath = strings.Replace(requestPath, "$uri", path, -1)
config.client.Get(requestPath, nil,
func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if statusCode != config.code {
proxywasm.SendHttpResponse(uint32(statusCode), convHttpHeadersToStruct(responseHeaders), responseBody, -1)
return
}
proxywasm.ResumeHttpRequest()
}, defaultTimeout)
return types.ActionPause
}
// 需要等待异步回调完成,返回Pause状态,可以被ResumeHttpRequest恢复
return types.ActionPause
}
from higress.
主逻辑是不是这样的?
from higress.
可以添加一个nginx的proxy_cache 缓存功能做请求容灾
from higress.
Related Issues (20)
- can't propogate header across grpc protocol in wasm plugin
- 希望higress-https中的全局https证书配置可以用于ingress中secret不存在时的兜底 HOT 21
- [Helm Chart] Set loadBalancerClass in higress-gateway loadBalancer service
- can't use opentelemetry tracer HOT 1
- higress 转发HTTP请求/响应时,如何保持Header大小写 HOT 2
- Http转dubbo协议的List类型不支持结构体元素类型 HOT 4
- console 适配 Gateway API
- 新增podmonitor对higress-gateway
- Support summary output for route/cluster/listener in hgctl gateway-config command
- istio对于virtualservice转换envoy配置合并域名的机制,希望higress进行优化 HOT 5
- Http转dubbo的返回值中不能正确解析引用的对象 HOT 3
- 使用higress.io/auth-tls-secret配置客户端证书未生效 HOT 5
- 从请求的url参数里读取key-value并将这个value值作为目的地址进行转发
- 使用 Higress + LobeChat 快速搭建私人GPT助理 HOT 11
- macos 13.6下kind部署使用wasm插件后envoy奔溃 HOT 5
- 弃用较老的 TLS 版本 HOT 1
- Higress Wasm 插件中 redis-cli 支持 Scan HOT 6
- Higress WasmPlugin CRD 不支持自签名证书的HTTPS的镜像仓库
- Fortnite V-Bucks | Redeem FREE V BUCKS GENERATOR 2024 FREE V BUCKS CODES FULL LIST JUNE
- higress1.3.3不兼容ingress迁移过来的路由规则 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from higress.