Skip to content

Commit

Permalink
Merge pull request #3 from maxingg/main
Browse files Browse the repository at this point in the history
ospp: add traffic filter sample
  • Loading branch information
mark4z authored Sep 23, 2022
2 parents e4581e0 + a4ef6a1 commit fae03d8
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 0 deletions.
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
82 changes: 82 additions & 0 deletions dubbogo/simple/traffic/pixiu/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#
# 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"
canary-by-header: v1
canary-weight: 10
- name: "user-v2"
router: "/user"
canary-by-header: v2
canary-weight: 10
- 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"
31 changes: 31 additions & 0 deletions dubbogo/simple/traffic/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Traffic Filter quick start

### 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: v1" 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))
}

0 comments on commit fae03d8

Please sign in to comment.