You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by ma...@apache.org on 2022/12/25 12:50:53 UTC
[dubbo-go-pixiu-samples] branch main updated: 远端配置中心之 nacos
This is an automated email from the ASF dual-hosted git repository.
ma642 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 8856dd9 远端配置中心之 nacos
new fcae070 Merge pull request #16 from zgq25302111/main
8856dd9 is described below
commit 8856dd99b32b23f67b82bc8c4c966d60b7e9d0b7
Author: zgq25302111 <zg...@126.com>
AuthorDate: Thu Dec 22 10:59:00 2022 +0800
远端配置中心之 nacos
---
dubbogo/simple/farconfnacos/README.md | 28 +++
.../simple/farconfnacos/docker/docker-compose.yml | 36 +++
dubbogo/simple/farconfnacos/farconfnacos.png | Bin 0 -> 66626 bytes
dubbogo/simple/farconfnacos/nacos/nacos.yaml | 61 +++++
dubbogo/simple/farconfnacos/pixiu/conf.yaml | 28 +++
dubbogo/simple/farconfnacos/server/app/server.go | 69 +++++
dubbogo/simple/farconfnacos/server/app/user.go | 280 +++++++++++++++++++++
.../farconfnacos/server/profiles/dev/log.yml | 45 ++++
.../farconfnacos/server/profiles/dev/server.yml | 39 +++
9 files changed, 586 insertions(+)
diff --git a/dubbogo/simple/farconfnacos/README.md b/dubbogo/simple/farconfnacos/README.md
new file mode 100644
index 0000000..295cc93
--- /dev/null
+++ b/dubbogo/simple/farconfnacos/README.md
@@ -0,0 +1,28 @@
+环境准备:
+1. 安装 docker
+2. 安装 goland,会在 goland 中启动 server、pixiu、test
+
+结构图:
+见 farconfnacos.png
+
+案例运行步骤:
+1. 在 docker 中启动 zk 和 nacos. 其中 zk 作为配置中心;nacos 作为远端配置中心,存储 pixiu 的启动参数
+2. 在 nacos 中创建配置,配置内容为 farconfnacos/nacos 目录中文件
+3. 设置命令行参数,启动 dubbogo server:
+- Working directory:
+- D:\mongo\samples\dubbogo\simple\farconfnacos\server
+
+- Environment:
+- APP_LOG_CONF_FILE=./profiles/dev/log.yml;DUBBO_GO_CONFIG_PATH=./profiles/dev/server.yml
+
+4. 设置命令行参数,启动 pixiu:
+- Package path:
+- github.com/apache/dubbo-go-pixiu/cmd/pixiu
+
+- Working directory:
+- D:/mongo/dubbo-go-pixiu-develop
+
+- Program arguments:
+- gateway start -c D:\mongo\dubbo-go-pixiu-samples\dubbogo\simple\farconfnacos\pixiu\conf.yaml
+
+5. 运行 test,查看结果
\ No newline at end of file
diff --git a/dubbogo/simple/farconfnacos/docker/docker-compose.yml b/dubbogo/simple/farconfnacos/docker/docker-compose.yml
new file mode 100644
index 0000000..e03a52e
--- /dev/null
+++ b/dubbogo/simple/farconfnacos/docker/docker-compose.yml
@@ -0,0 +1,36 @@
+#
+# 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
+ nacos:
+ image: nacos/nacos-server:2.0.1
+ container_name: nacos-standalone
+ environment:
+ - PREFER_HOST_MODE=hostname
+ - MODE=standalone
+ - NACOS_AUTH_ENABLE=true
+ ports:
+ - "8848:8848"
diff --git a/dubbogo/simple/farconfnacos/farconfnacos.png b/dubbogo/simple/farconfnacos/farconfnacos.png
new file mode 100644
index 0000000..709f3b0
Binary files /dev/null and b/dubbogo/simple/farconfnacos/farconfnacos.png differ
diff --git a/dubbogo/simple/farconfnacos/nacos/nacos.yaml b/dubbogo/simple/farconfnacos/nacos/nacos.yaml
new file mode 100644
index 0000000..25cfe4d
--- /dev/null
+++ b/dubbogo/simple/farconfnacos/nacos/nacos.yaml
@@ -0,0 +1,61 @@
+#
+# 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: 8881
+ filter_chains:
+ filters:
+ - name: dgp.filter.httpconnectionmanager
+ config:
+ route_config:
+ routes:
+ - match:
+ prefix: "*"
+ http_filters:
+ - name: dgp.filter.http.dubboproxy
+ config:
+ dubboProxyConfig:
+ auto_resolve: true
+ registries:
+ "zookeeper":
+ protocol: "zookeeper"
+ timeout: "3s"
+ address: "127.0.0.1:2181"
+ username: ""
+ password: ""
+ timeout_config:
+ connect_timeout: 5s
+ request_timeout: 5s
+
+ server_name: "test_http_dubbo"
+ generate_request_id: false
+ config:
+ idle_timeout: 5s
+ read_timeout: 5s
+ write_timeout: 5s
+ shutdown_config:
+ timeout: "60s"
+ step_timeout: "10s"
+ reject_policy: "immediacy"
\ No newline at end of file
diff --git a/dubbogo/simple/farconfnacos/pixiu/conf.yaml b/dubbogo/simple/farconfnacos/pixiu/conf.yaml
new file mode 100644
index 0000000..96e9ae8
--- /dev/null
+++ b/dubbogo/simple/farconfnacos/pixiu/conf.yaml
@@ -0,0 +1,28 @@
+#
+# 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.
+#
+config-center:
+ type: nacos
+ enable: true
+nacos:
+ server-configs:
+ - ip_addr: "127.0.0.1"
+ port: 8848
+ client-config:
+ username: nacos
+ password: nacos
\ No newline at end of file
diff --git a/dubbogo/simple/farconfnacos/server/app/server.go b/dubbogo/simple/farconfnacos/server/app/server.go
new file mode 100644
index 0000000..776280f
--- /dev/null
+++ b/dubbogo/simple/farconfnacos/server/app/server.go
@@ -0,0 +1,69 @@
+/*
+ * 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"
+ "os"
+ "os/signal"
+ "syscall"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/logger"
+ "dubbo.apache.org/dubbo-go/v3/config"
+ _ "dubbo.apache.org/dubbo-go/v3/imports"
+)
+
+// Version dubbo version
+const Version = "2.7.5"
+
+var survivalTimeout = int(3e9)
+
+// they are necessary:
+// export DUBBO_GO_CONFIG_PATH="../profiles/dev/server.yml"
+// export APP_LOG_CONF_FILE="../profiles/dev/log.yml"
+func main() {
+ config.Load()
+ logger.Infof("dubbo version is: %s", Version)
+ initSignal()
+}
+
+func initSignal() {
+ signals := make(chan os.Signal, 1)
+ // It is not possible to block SIGKILL or syscall.SIGSTOP
+ signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
+ for {
+ sig := <-signals
+ logger.Infof("get signal %s", sig.String())
+ switch sig {
+ case syscall.SIGHUP:
+ // reload()
+ default:
+ time.AfterFunc(time.Duration(survivalTimeout), func() {
+ logger.Warnf("app exit now by force...")
+ os.Exit(1)
+ })
+
+ // The program exits normally or timeout forcibly exits.
+ fmt.Println("provider app exit now...")
+ return
+ }
+ }
+}
diff --git a/dubbogo/simple/farconfnacos/server/app/user.go b/dubbogo/simple/farconfnacos/server/app/user.go
new file mode 100644
index 0000000..35cc39b
--- /dev/null
+++ b/dubbogo/simple/farconfnacos/server/app/user.go
@@ -0,0 +1,280 @@
+/*
+ * 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 (
+ "context"
+ "errors"
+ "fmt"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/config"
+
+ hessian "github.com/apache/dubbo-go-hessian2"
+)
+
+func init() {
+ config.SetProviderService(new(UserProvider))
+ // ------for hessian2------
+ hessian.RegisterPOJO(&User{})
+
+ cache = newUserDB()
+
+ t1, _ := time.Parse(
+ time.RFC3339,
+ "2021-08-01T10:08:41+00:00")
+
+ cache.Add(&User{ID: "0001", Code: 1, Name: "tc", Age: 18, Time: t1})
+ cache.Add(&User{ID: "0002", Code: 2, Name: "ic", Age: 88, Time: t1})
+}
+
+var cache *userDB
+
+// userDB cache user.
+type userDB struct {
+ // key is name, value is user obj
+ nameIndex map[string]*User
+ // key is code, value is user obj
+ codeIndex map[int64]*User
+ lock sync.Mutex
+}
+
+// userDB create func
+func newUserDB() *userDB {
+ return &userDB{
+ nameIndex: make(map[string]*User, 16),
+ codeIndex: make(map[int64]*User, 16),
+ lock: sync.Mutex{},
+ }
+}
+
+// nolint
+func (db *userDB) Add(u *User) bool {
+ db.lock.Lock()
+ defer db.lock.Unlock()
+
+ if u.Name == "" || u.Code <= 0 {
+ return false
+ }
+
+ if !db.existName(u.Name) && !db.existCode(u.Code) {
+ return db.AddForName(u) && db.AddForCode(u)
+ }
+
+ return false
+}
+
+// nolint
+func (db *userDB) AddForName(u *User) bool {
+ if len(u.Name) == 0 {
+ return false
+ }
+
+ if _, ok := db.nameIndex[u.Name]; ok {
+ return false
+ }
+
+ db.nameIndex[u.Name] = u
+ return true
+}
+
+// nolint
+func (db *userDB) AddForCode(u *User) bool {
+ if u.Code <= 0 {
+ return false
+ }
+
+ if _, ok := db.codeIndex[u.Code]; ok {
+ return false
+ }
+
+ db.codeIndex[u.Code] = u
+ return true
+}
+
+// nolint
+func (db *userDB) GetByName(n string) (*User, bool) {
+ db.lock.Lock()
+ defer db.lock.Unlock()
+
+ r, ok := db.nameIndex[n]
+ return r, ok
+}
+
+// nolint
+func (db *userDB) GetByCode(n int64) (*User, bool) {
+ db.lock.Lock()
+ defer db.lock.Unlock()
+
+ r, ok := db.codeIndex[n]
+ return r, ok
+}
+
+func (db *userDB) existName(name string) bool {
+ if len(name) <= 0 {
+ return false
+ }
+
+ _, ok := db.nameIndex[name]
+ if ok {
+ return true
+ }
+
+ return false
+}
+
+func (db *userDB) existCode(code int64) bool {
+ if code <= 0 {
+ return false
+ }
+
+ _, ok := db.codeIndex[code]
+ if ok {
+ return true
+ }
+
+ return false
+}
+
+// User user obj.
+type User struct {
+ ID string `json:"id,omitempty"`
+ Code int64 `json:"code,omitempty"`
+ Name string `json:"name,omitempty"`
+ Age int32 `json:"age,omitempty"`
+ Time time.Time `json:"time,omitempty"`
+}
+
+// UserProvider the dubbo provider.
+// like: version: 1.0.0 group: test
+type UserProvider struct{}
+
+// CreateUser new user, PX config POST.
+func (u *UserProvider) CreateUser(ctx context.Context, user *User) (*User, error) {
+ outLn("Req CreateUser data:%#v", user)
+ if user == nil {
+ return nil, errors.New("not found")
+ }
+ _, ok := cache.GetByName(user.Name)
+ if ok {
+ return nil, errors.New("data is exist")
+ }
+
+ b := cache.Add(user)
+ if b {
+ return user, nil
+ }
+
+ return nil, errors.New("add error")
+}
+
+// GetUserByName query by name, single param, PX config GET.
+func (u *UserProvider) GetUserByName(ctx context.Context, name string) (*User, error) {
+ outLn("Req GetUserByName name:%#v", name)
+ r, ok := cache.GetByName(name)
+ if ok {
+ outLn("Req GetUserByName result:%#v", r)
+ return r, nil
+ }
+ return nil, nil
+}
+
+// GetUserByCode query by code, single param, PX config GET.
+func (u *UserProvider) GetUserByCode(ctx context.Context, code int64) (*User, error) {
+ outLn("Req GetUserByCode name:%#v", code)
+ r, ok := cache.GetByCode(code)
+ if ok {
+ outLn("Req GetUserByCode result:%#v", r)
+ return r, nil
+ }
+ return nil, nil
+}
+
+// GetUserTimeout query by name, will timeout for pixiu.
+func (u *UserProvider) GetUserTimeout(ctx context.Context, name string) (*User, error) {
+ outLn("Req GetUserByName name:%#v", name)
+ // sleep 10s, pixiu config less than 10s.
+ time.Sleep(10 * time.Second)
+ r, ok := cache.GetByName(name)
+ if ok {
+ outLn("Req GetUserByName result:%#v", r)
+ return r, nil
+ }
+ return nil, nil
+}
+
+// GetUserByNameAndAge query by name and age, two params, PX config GET.
+func (u *UserProvider) GetUserByNameAndAge(ctx context.Context, name string, age int32) (*User, error) {
+ outLn("Req GetUserByNameAndAge name:%s, age:%d", name, age)
+ r, ok := cache.GetByName(name)
+ if ok && r.Age == age {
+ outLn("Req GetUserByNameAndAge result:%#v", r)
+ return r, nil
+ }
+ return r, nil
+}
+
+// UpdateUser update by user struct, my be another struct, PX config POST or PUT.
+func (u *UserProvider) UpdateUser(ctx context.Context, user *User) (bool, error) {
+ outLn("Req UpdateUser data:%#v", user)
+ r, ok := cache.GetByName(user.Name)
+ if ok {
+ if user.ID != "" {
+ r.ID = user.ID
+ }
+ if user.Age >= 0 {
+ r.Age = user.Age
+ }
+ return true, nil
+ }
+ return false, errors.New("not found")
+}
+
+// UpdateUserByName update by user struct, my be another struct, PX config POST or PUT.
+func (u *UserProvider) UpdateUserByName(ctx context.Context, name string, user *User) (bool, error) {
+ outLn("Req UpdateUserByName data:%#v", user)
+ r, ok := cache.GetByName(name)
+ if ok {
+ if user.ID != "" {
+ r.ID = user.ID
+ }
+ if user.Age >= 0 {
+ r.Age = user.Age
+ }
+ return true, nil
+ }
+ return false, errors.New("not found")
+}
+
+// nolint
+func (u *UserProvider) Reference() string {
+ return "UserProvider"
+}
+
+// nolint
+func (u User) JavaClassName() string {
+ return "com.dubbogo.pixiu.User"
+}
+
+// nolint
+func outLn(format string, args ...interface{}) {
+ fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...)
+}
diff --git a/dubbogo/simple/farconfnacos/server/profiles/dev/log.yml b/dubbogo/simple/farconfnacos/server/profiles/dev/log.yml
new file mode 100644
index 0000000..9330cda
--- /dev/null
+++ b/dubbogo/simple/farconfnacos/server/profiles/dev/log.yml
@@ -0,0 +1,45 @@
+#
+# 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.
+#
+level: "debug"
+development: true
+disableCaller: false
+disableStacktrace: false
+sampling:
+encoding: "console"
+
+# encoder
+encoderConfig:
+ messageKey: "message"
+ levelKey: "level"
+ timeKey: "time"
+ nameKey: "logger"
+ callerKey: "caller"
+ stacktraceKey: "stacktrace"
+ lineEnding: ""
+ levelEncoder: "capitalColor"
+ timeEncoder: "iso8601"
+ durationEncoder: "seconds"
+ callerEncoder: "short"
+ nameEncoder: ""
+
+outputPaths:
+ - "stderr"
+errorOutputPaths:
+ - "stderr"
+initialFields:
diff --git a/dubbogo/simple/farconfnacos/server/profiles/dev/server.yml b/dubbogo/simple/farconfnacos/server/profiles/dev/server.yml
new file mode 100644
index 0000000..c93feb2
--- /dev/null
+++ b/dubbogo/simple/farconfnacos/server/profiles/dev/server.yml
@@ -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.
+#
+# dubbo server yaml configure file
+# application config
+dubbo:
+ registries:
+ zk:
+ protocol: zookeeper
+ timeout: 3s
+ address: 127.0.0.1:2181
+ protocols:
+ dubbo:
+ name: dubbo
+ port: 20000
+ provider:
+ registry-ids: zk
+ services:
+ UserProvider:
+ group: test
+ version: 1.0.0
+ cluster: test_dubbo
+ serialization: hessian2
+ interface: com.dubbogo.pixiu.UserService
\ No newline at end of file