Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Add traffic sample. #21

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
27 changes: 27 additions & 0 deletions dubbogo/simple/traffic/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Licensed to Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Apache Software Foundation (ASF) licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

version: '3'

services:
zookeeper:
image: zookeeper
ports:
- 2181:2181
restart: on-failure
84 changes: 84 additions & 0 deletions dubbogo/simple/traffic/pixiu/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
---
static_resources:
listeners:
- name: "net/http"
protocol_type: "HTTP"
address:
socket_address:
address: "0.0.0.0"
port: 8888
filter_chains:
filters:
- name: dgp.filter.httpconnectionmanager
config:
route_config:
routes:
- match:
prefix: "/user"
route:
cluster: "user"
cluster_not_found_response_code: 505
http_filters:
- name: dgp.filter.http.traffic
config:
traffics:
- name: "user-v1"
router: "/user/1"
model: canary-by-header
canary: canary-by-header
value: 10
- name: "user-v2"
router: "/user/1"
model: canary-weight
canary: canary-weight
value: 90
- name: dgp.filter.http.httpproxy
config:
config:
idle_timeout: 5s
read_timeout: 5s
write_timeout: 5s
clusters:
- name: "user"
lb_policy: "lb"
endpoints:
- id: 1
socket_address:
address: 127.0.0.1
port: 1314
- name: "user-v1"
lb_policy: "lb"
endpoints:
- id: 1
socket_address:
address: 127.0.0.1
port: 1315
- name: "user-v2"
lb_policy: "lb"
endpoints:
- id: 1
socket_address:
address: 127.0.0.1
port: 1316
shutdown_config:
timeout: "60s"
step_timeout: "10s"
reject_policy: "immediacy"
30 changes: 30 additions & 0 deletions dubbogo/simple/traffic/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#Traffic Filter quick start
### Start Zookeeper:
```shell
cd samples/dubbogo/simple/traffic
run docker-compose.yml/services
```
### Start Http Server
```shell
cd server
go run server.go
```
```shell
cd server/v1
go run server.go
```
```shell
cd server/v2
go run server.go
```

### Start Pixiu
```shell
go run cmd/pixiu/*.go gateway start -c samples/dubbogo/simple/traffic/pixiu/conf.yaml
```
### Start test
```shell
curl http://localhost:8888/user
curl -H "canary-by-header: 10" http://localhost:8888/user
curl -H "canary-weight: 90" http://localhost:8888/user
```
39 changes: 39 additions & 0 deletions dubbogo/simple/traffic/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"fmt"
"log"
"net/http"
"strings"
)

func main() {
routers := []string{"/user", "/user/pixiu", "/prefix", "/health"}

for _, router := range routers {
msg := router[strings.LastIndex(router, "/")+1:]
http.HandleFunc(router, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(fmt.Sprintf(`{"message":"%s","status":200}`, msg)))
})
}

log.Println("Starting sample server ...")
log.Fatal(http.ListenAndServe(":1314", nil))
}
39 changes: 39 additions & 0 deletions dubbogo/simple/traffic/server/v1/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"fmt"
"log"
"net/http"
"strings"
)

func main() {
routers := []string{"/user", "/user/pixiu", "/prefix", "/health"}

for _, router := range routers {
msg := router[strings.LastIndex(router, "/")+1:]
http.HandleFunc(router, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(fmt.Sprintf(`{"server": "v1","message":"%s","status":200}`, msg)))
})
}

log.Println("Starting sample server ...")
log.Fatal(http.ListenAndServe(":1315", nil))
}
39 changes: 39 additions & 0 deletions dubbogo/simple/traffic/server/v2/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"fmt"
"log"
"net/http"
"strings"
)

func main() {
routers := []string{"/user", "/user/pixiu", "/prefix", "/health"}

for _, router := range routers {
msg := router[strings.LastIndex(router, "/")+1:]
http.HandleFunc(router, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(fmt.Sprintf(`{"server": "v2","message":"%s","status":200}`, msg)))
})
}

log.Println("Starting sample server ...")
log.Fatal(http.ListenAndServe(":1316", nil))
}
44 changes: 44 additions & 0 deletions dubbogo/simple/traffic/test/pixiu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package test

import (
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
)

import (
"github.com/stretchr/testify/assert"
)

func TestGET(t *testing.T) {
url := "http://localhost:8888/user"
client := &http.Client{Timeout: 5 * time.Second}
req, err := http.NewRequest("GET", url, nil)
assert.NoError(t, err)
req.Header.Add("canary-by-header", "10")
resp, err := client.Do(req)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, 200, resp.StatusCode)
s, _ := ioutil.ReadAll(resp.Body)
assert.True(t, strings.Contains(string(s), `"server": "v1"`))
}
4 changes: 2 additions & 2 deletions dubbogo/simple/wasm/pixiu/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static_resources:
- name: dgp.filter.http.webassembly
config:
wasm_services:
- name: header
- name: wasm.header
- name: dgp.filter.http.dubboproxy
config:
dubboProxyConfig:
Expand Down Expand Up @@ -65,6 +65,6 @@ static_resources:
reject_policy: "immediacy"
wasm:
services:
- name: header
- name: wasm.header
config:
path: wasm/data/filter.wasm
50 changes: 50 additions & 0 deletions dubbogo/simple/wasm/test/pixiu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package test

import (
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
)

import (
"github.com/stretchr/testify/assert"
)

func TestPost(t *testing.T) {
url := "http://localhost:8883/UserService/com.dubbogo.pixiu.UserService/GetUserByName"
data := "{\"types\":\"string\",\"values\":\"tc\"}"
client := &http.Client{Timeout: 5 * time.Second}
req, err := http.NewRequest("POST", url, strings.NewReader(data))
req.Header.Set("x-dubbo-http1.1-dubbo-version", "1.0.0")
req.Header.Set("x-dubbo-service-protocol", "dubbo")
req.Header.Set("x-dubbo-service-version", "1.0.0")
req.Header.Set("x-dubbo-service-group", "test")

assert.NoError(t, err)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, 200, resp.StatusCode)
s, _ := ioutil.ReadAll(resp.Body)
assert.Contains(t, string(s), "hello wasm")
}