Skip to content

Commit

Permalink
Update wasm17.md
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlanni authored Nov 11, 2024
1 parent 5a1022f commit 5a45b26
Showing 1 changed file with 59 additions and 33 deletions.
92 changes: 59 additions & 33 deletions src/content/docs/ebook/zh-cn/wasm17.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,33 @@ type Cluster interface {
}
```

### 1.1 静态配置(static)
### 1.1 FQDN

```golang

type FQDNCluster struct {
FQDN string
Host string
Port int64
}
```
- 集群名称规则为:`outbound|<Port>||<FQDN>`
- HostName 规则为:如果设置 Host,返回 Host,否则返回 `<FQDN>`

FQDN 即在服务列表里看到的服务名称,形如:"my-cluster.static","your-cluster.dns","foo.default.svc.cluster.local"

Host 字段用于发送实际 HTTP 请求时的缺省配置域名,如果在发送时的 URL 里指定了域名,那么将以指定的为准。下面其他的 cluster 中的 Host 字段含义也是一样的。

### 1.2 当前路由的服务

```golang
type RouteCluster struct {
Host string
}
```
集群名称是直接通过 proxywasm.GetProperty([]string{"cluster_name"}) 获取的当前路由的目标集群

### 1.3 静态配置(static)
```golang
type StaticIpCluster struct {
ServiceName string
Expand All @@ -32,7 +58,7 @@ type StaticIpCluster struct {
- 集群名称规则为:`outbound|<port>||<service_name>.static`
- HostName 规则为:默认为 <service_name>。

### 1.2 DNS 配置(dns)
### 1.4 DNS 配置(dns)
```golang
type DnsCluster struct {
ServiceName string
Expand All @@ -44,7 +70,7 @@ type DnsCluster struct {
- 集群名称规则为:`outbound|<Port>||<ServiceName>.dns`
- HostName 规则为:如果设置 Host,返回 Host,否则返回<Domain>。

### 1.3 Kubernetes 服务(kubernetes)
### 1.5 Kubernetes 服务(kubernetes)
```golang

type K8sCluster struct {
Expand All @@ -58,7 +84,7 @@ type K8sCluster struct {
- 集群名称规则为:`outbound|<Port>|<Version>|<ServiceName>.<Namespace>.svc.cluster.local`
- HostName 规则为:如果设置 Host,返回 Host,否则返回 <ServiceName>.<Namespace>.svc.cluster.local。

### 1.4 Nacos
### 1.6 Nacos
```golang

type NacosCluster struct {
Expand All @@ -76,7 +102,7 @@ type NacosCluster struct {
- 集群名称规则为:`outbound|<Port>|<Version>|<ServiceName>.<Group>.<NamespaceID>.nacos`
- HostName 规则为:如果设置 Host,返回 Host,否则返回 <service_name>。

### 1.5 Consul
### 1.7 Consul
```golang
type ConsulCluster struct {
ServiceName string
Expand All @@ -88,18 +114,6 @@ type ConsulCluster struct {
- 集群名称规则为:`outbound|<Port>||<ServiceName>.<Datacenter>.consul`
- HostName 规则为:如果设置 Host,返回 Host,否则返回 <ServiceName>。

### 1.6 FQDN

```golang

type FQDNCluster struct {
FQDN string
Host string
Port int64
}
```
- 集群名称规则为:`outbound|<Port>||<FQDN>`
- HostName 规则为:如果设置 Host,返回 Host,否则返回 `<FQDN>`

## 2 HTTP 调用
http_wrapper.go 部分核心代码如下:
Expand All @@ -109,16 +123,16 @@ type ResponseCallback func(statusCode int, responseHeaders http.Header, response

// HTTP 调用接口
type HttpClient interface {
Get(path string, headers [][2]string, cb ResponseCallback, timeoutMillisecond ...uint32) error
Head(path string, headers [][2]string, cb ResponseCallback, timeoutMillisecond ...uint32) error
Options(path string, headers [][2]string, cb ResponseCallback, timeoutMillisecond ...uint32) error
Post(path string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Put(path string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Patch(path string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Delete(path string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Connect(path string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Trace(path string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Call(method, path string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Get(rawURL string, headers [][2]string, cb ResponseCallback, timeoutMillisecond ...uint32) error
Head(rawURL string, headers [][2]string, cb ResponseCallback, timeoutMillisecond ...uint32) error
Options(rawURL string, headers [][2]string, cb ResponseCallback, timeoutMillisecond ...uint32) error
Post(rawURL string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Put(rawURL string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Patch(rawURL string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Delete(rawURL string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Connect(rawURL string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Trace(rawURL string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
Call(method, rawURL string, headers [][2]string, body []byte, cb ResponseCallback, timeoutMillisecond ...uint32) error
}

// 实现 httpClient 接口
Expand All @@ -129,23 +143,35 @@ type ClusterClient[C Cluster] struct {
ClusterClient Get、Head、Options、Post、PUT、Patch、Delete、Connect、Trace、Call 方法最后调用 HttpCall 方法,其核心代码如下:

```golang
func HttpCall(cluster Cluster, method, path string, headers [][2]string, body []byte,
func HttpCall(cluster Cluster, method, rawURL string, headers [][2]string, body []byte,
callback ResponseCallback, timeoutMillisecond ...uint32) error {

// 删除 :method, :path, :authority
// 忽略 headers 里设置的保留头
for i := len(headers) - 1; i >= 0; i-- {
key := headers[i][0]
if key == ":method" || key == ":path" || key == ":authority" {
headers = append(headers[:i], headers[i+1:]...)
}
}
// 设置 timeout
// 从 URL 里解析域名和路径
parsedURL, err := url.Parse(rawURL)
if err != nil {
proxywasm.LogCriticalf("invalid rawURL:%s", rawURL)
return err
}
authority := cluster.HostName()
if parsedURL.Host != "" {
authority = parsedURL.Host
}
path := "/" + strings.TrimPrefix(parsedURL.Path, "/")
if parsedURL.RawQuery != "" {
path = fmt.Sprintf("%s?%s", path, parsedURL.RawQuery)
}
// 默认超时时间是 500ms
var timeout uint32 = 500
if len(timeoutMillisecond) > 0 {
timeout = timeoutMillisecond[0]
}
// 重新设置 :method, :path, :authority
headers = append(headers, [2]string{":method", method}, [2]string{":path", path}, [2]string{":authority", cluster.HostName()})
headers = append(headers, [2]string{":method", method}, [2]string{":path", path}, [2]string{":authority", authority})
requestID := uuid.New().String()
// 调用 HTTP 请求
_, err := proxywasm.DispatchHttpCall(cluster.ClusterName(), headers, body, nil, timeout, func(numHeaders, bodySize, numTrailers int) {
Expand Down

0 comments on commit 5a45b26

Please sign in to comment.