This is an automated email from the ASF dual-hosted git repository.

mark4z pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-go-pixiu-samples.git


The following commit(s) were added to refs/heads/main by this push:
     new a04eff1  ospp: update traffic sample. (#7)
a04eff1 is described below

commit a04eff1f81c112f43868fa8d5c2a5bb5c1f301dd
Author: 祭酒 <[email protected]>
AuthorDate: Mon Oct 17 20:56:02 2022 +0800

    ospp: update traffic sample. (#7)
    
    * feature: traffic sample update
    
    * fix: update traffic's config.yaml
---
 dubbogo/simple/traffic/pixiu/conf.yaml     | 14 +++++-
 dubbogo/simple/traffic/server/v3/server.go | 39 ++++++++++++++++
 dubbogo/simple/traffic/test/pixiu_test.go  | 71 ++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+), 2 deletions(-)

diff --git a/dubbogo/simple/traffic/pixiu/conf.yaml 
b/dubbogo/simple/traffic/pixiu/conf.yaml
index 122f072..25ba09e 100644
--- a/dubbogo/simple/traffic/pixiu/conf.yaml
+++ b/dubbogo/simple/traffic/pixiu/conf.yaml
@@ -43,11 +43,14 @@ static_resources:
                         - name: "user-v1"
                           router: "/user"
                           canary-by-header: v1
-                          canary-weight: 10
+                          canary-weight: 0
                         - name: "user-v2"
                           router: "/user"
                           canary-by-header: v2
-                          canary-weight: 10
+                          canary-weight: 100
+                        - name: "user-v3"
+                          router: "/user"
+                          canary-by-header: v3
                   - name: dgp.filter.http.httpproxy
                     config:
       config:
@@ -76,6 +79,13 @@ static_resources:
           socket_address:
             address: 127.0.0.1
             port: 1316
+    - name: "user-v3"
+      lb_policy: "lb"
+      endpoints:
+        - id: 1
+          socket_address:
+            address: 127.0.0.1
+            port: 1317
   shutdown_config:
     timeout: "60s"
     step_timeout: "10s"
diff --git a/dubbogo/simple/traffic/server/v3/server.go 
b/dubbogo/simple/traffic/server/v3/server.go
new file mode 100644
index 0000000..49d340b
--- /dev/null
+++ b/dubbogo/simple/traffic/server/v3/server.go
@@ -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": 
"v3","message":"%s","status":200}`, msg)))
+               })
+       }
+
+       log.Println("Starting sample server ...")
+       log.Fatal(http.ListenAndServe(":1317", nil))
+}
diff --git a/dubbogo/simple/traffic/test/pixiu_test.go 
b/dubbogo/simple/traffic/test/pixiu_test.go
new file mode 100644
index 0000000..8d396ef
--- /dev/null
+++ b/dubbogo/simple/traffic/test/pixiu_test.go
@@ -0,0 +1,71 @@
+/*
+ * 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", "v1")
+       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"`))
+}
+
+func TestGET1(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)
+       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": "v2"`))
+}
+
+func TestGET2(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", "v3")
+       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": "v3"`))
+}

Reply via email to