Merge pull request #2 from georgehao/master
Add etcd registry examples
diff --git a/golang/.images/edit_configuratios.png b/golang/.images/edit_configuratios.png
new file mode 100644
index 0000000..b4c9235
--- /dev/null
+++ b/golang/.images/edit_configuratios.png
Binary files differ
diff --git a/golang/.images/edit_env.png b/golang/.images/edit_env.png
new file mode 100644
index 0000000..236c1bb
--- /dev/null
+++ b/golang/.images/edit_env.png
Binary files differ
diff --git a/golang/README.md b/golang/README.md
index 947cee5..a58e353 100644
--- a/golang/README.md
+++ b/golang/README.md
@@ -35,7 +35,11 @@
* registry
- Some examples of different registry. There are kubernetes and nacos at present.
+ Some examples of different registry. There are kubernetes, nacos and etcd at present.
+
+ **Note**:
+
+ When use different registry, you need update config file, but also must import the registry package. see the etcd `README`
* filter
@@ -102,6 +106,22 @@
sh ./bin/load_user_info_client.sh start $SUFFIX
```
+## How to debug with Goland
+
+### Edit Configurations
+
+![](.images/edit_configuratios.png)
+
+### Configure `Environment Variable`
+
+1. Add `APP_LOG_CONF_FILE`. eg: `/home/xx/dubbogo-samples/helloworld/client/conf/log.yml`
+2. Add `CONF_CONSUMER_FILE_PATH` eg: `/home/xx/dubbogo-samples/helloworld/client/conf/client.yml`
+3. Add `CONF_PROVIDER_FILE_PATH` eg: `/home/xx/dubbogo-samples/helloworld/server/conf/server.yml`
+
+![](.images/edit_env.png)
+
+### Apply & Run
+
## How to contribute
If you want to add some samples, we hope that you can do this:
diff --git a/golang/registry/etcd/README.md b/golang/registry/etcd/README.md
new file mode 100644
index 0000000..dcfd091
--- /dev/null
+++ b/golang/registry/etcd/README.md
@@ -0,0 +1,141 @@
+# etcd
+
+use etcd for registry
+
+# config
+
+## go-server/profiles/dev/server.yml
+
+```yaml
+# dubbo server yaml configure file
+
+
+# application config
+application:
+ organization : "ikurento.com"
+ name : "BDTService"
+ module : "dubbogo user-info server"
+ version : "0.0.1"
+ owner : "ZX"
+ environment : "dev"
+
+registries :
+ "etcd":
+ protocol: "etcdv3"
+ timeout : "3s"
+ address: "127.0.0.1:2379"
+
+services:
+ "UserProvider":
+ # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
+ registry: "etcd" # 特别注意这里
+ protocol : "dubbo"
+ # 相当于dubbo.xml中的interface
+ interface : "com.ikurento.user.UserProvider"
+ loadbalance: "random"
+ warmup: "100"
+ cluster: "failover"
+ methods:
+ - name: "GetUser"
+ retries: 1
+ loadbalance: "random"
+
+protocols:
+ "dubbo":
+ name: "dubbo"
+ port: 20000
+
+
+protocol_conf:
+ dubbo:
+ session_number: 700
+ session_timeout: "180s"
+ getty_session_param:
+ compress_encoding: false
+ tcp_no_delay: true
+ tcp_keep_alive: true
+ keep_alive_period: "120s"
+ tcp_r_buf_size: 262144
+ tcp_w_buf_size: 65536
+ pkg_rq_size: 1024
+ pkg_wq_size: 512
+ tcp_read_timeout: "1s"
+ tcp_write_timeout: "5s"
+ wait_timeout: "1s"
+ max_msg_len: 1024000
+ session_name: "server"
+```
+
+## go-client/profiles/dev/client.yml
+
+```yaml
+# dubbo client yaml configure file
+
+
+check: true
+# client
+request_timeout : "3s"
+# connect timeout
+connect_timeout : "3s"
+
+# application config
+application:
+ organization : "ikurento.com"
+ name : "BDTService"
+ module : "dubbogo user-info client"
+ version : "0.0.1"
+ owner : "ZX"
+ environment : "dev"
+
+registries :
+ "etcd":
+ protocol: "etcdv3"
+ timeout : "3s"
+ address: "127.0.0.1:2379"
+ username: ""
+ password: ""
+
+
+references:
+ "UserProvider":
+ # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
+ protocol : "dubbo"
+ interface : "com.ikurento.user.UserProvider"
+ cluster: "failover"
+ methods :
+ - name: "GetUser"
+ retries: 3
+
+
+protocol_conf:
+ dubbo:
+ reconnect_interval: 0
+ connection_number: 1
+ heartbeat_period: "5s"
+ session_timeout: "180s"
+ pool_size: 64
+ pool_ttl: 600
+ getty_session_param:
+ compress_encoding: false
+ tcp_no_delay: true
+ tcp_keep_alive: true
+ keep_alive_period: "120s"
+ tcp_r_buf_size: 262144
+ tcp_w_buf_size: 65536
+ pkg_rq_size: 1024
+ pkg_wq_size: 512
+ tcp_read_timeout: "1s"
+ tcp_write_timeout: "5s"
+ wait_timeout: "1s"
+ max_msg_len: 1024000
+ session_name: "client"
+```
+
+## import etcd
+
+import `github.com/apache/dubbo-go/registry/etcdv3` in `go-client/app/client.go` and `go-server/app/server.go`
+
+```go
+// import etcdv3 to init etcdv3 registry
+_ "github.com/apache/dubbo-go/registry/etcdv3"
+```
\ No newline at end of file
diff --git a/golang/registry/etcd/go-client/app/client.go b/golang/registry/etcd/go-client/app/client.go
new file mode 100644
index 0000000..e2fc934
--- /dev/null
+++ b/golang/registry/etcd/go-client/app/client.go
@@ -0,0 +1,77 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "os"
+ "os/signal"
+ "syscall"
+ "time"
+)
+
+import (
+ hessian "github.com/apache/dubbo-go-hessian2"
+ "github.com/apache/dubbo-go/common/logger"
+ _ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
+ "github.com/apache/dubbo-go/config"
+ _ "github.com/apache/dubbo-go/protocol/dubbo"
+ _ "github.com/apache/dubbo-go/registry/protocol"
+
+ _ "github.com/apache/dubbo-go/filter/filter_impl"
+
+ _ "github.com/apache/dubbo-go/cluster/cluster_impl"
+ _ "github.com/apache/dubbo-go/cluster/loadbalance"
+
+ // import etcdv3 to init etcdv3 registry
+ _ "github.com/apache/dubbo-go/registry/etcdv3"
+)
+
+var (
+ survivalTimeout int = 10e9
+)
+
+func println(format string, args ...interface{}) {
+ fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...)
+}
+
+// they are necessary:
+// export CONF_CONSUMER_FILE_PATH="xxx"
+// export APP_LOG_CONF_FILE="xxx"
+func main() {
+ hessian.RegisterPOJO(&User{})
+ config.Load()
+ time.Sleep(3e9)
+
+ println("\n\n\nstart to test dubbo")
+ user := &User{}
+ err := userProvider.GetUser(context.TODO(), []interface{}{"A001"}, user)
+ if err != nil {
+ panic(err)
+ }
+ println("response result: %v\n", user)
+ initSignal()
+}
+
+func initSignal() {
+ signals := make(chan os.Signal, 1)
+ // It is not possible to block SIGKILL or syscall.SIGSTOP
+ signal.Notify(signals, os.Interrupt, os.Kill, 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("app exit now...")
+ return
+ }
+ }
+}
diff --git a/golang/registry/etcd/go-client/app/user.go b/golang/registry/etcd/go-client/app/user.go
new file mode 100644
index 0000000..da98c72
--- /dev/null
+++ b/golang/registry/etcd/go-client/app/user.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+ "context"
+ "time"
+)
+
+import (
+ hessian "github.com/apache/dubbo-go-hessian2"
+ "github.com/apache/dubbo-go/config"
+)
+
+var userProvider = new(UserProvider)
+
+func init() {
+ config.SetConsumerService(userProvider)
+ hessian.RegisterPOJO(&User{})
+}
+
+type User struct {
+ Id string
+ Name string
+ Age int32
+ Time time.Time
+}
+
+type UserProvider struct {
+ GetUser func(ctx context.Context, req []interface{}, rsp *User) error
+}
+
+func (u *UserProvider) Reference() string {
+ return "UserProvider"
+}
+
+func (User) JavaClassName() string {
+ return "com.ikurento.user.User"
+}
diff --git a/golang/registry/etcd/go-client/app/version.go b/golang/registry/etcd/go-client/app/version.go
new file mode 100644
index 0000000..60c1a81
--- /dev/null
+++ b/golang/registry/etcd/go-client/app/version.go
@@ -0,0 +1,5 @@
+package main
+
+var (
+ Version = "2.6.0"
+)
diff --git a/golang/registry/etcd/go-client/assembly/bin/load.sh b/golang/registry/etcd/go-client/assembly/bin/load.sh
new file mode 100755
index 0000000..598bbcb
--- /dev/null
+++ b/golang/registry/etcd/go-client/assembly/bin/load.sh
@@ -0,0 +1,184 @@
+APP_NAME="APPLICATION_NAME"
+APP_ARGS=""
+SLEEP_INTERVAL=5
+MAX_LIFETIME=4000
+
+PROJECT_HOME=""
+OS_NAME=`uname`
+if [[ ${OS_NAME} != "Windows" ]]; then
+ PROJECT_HOME=`pwd`
+ PROJECT_HOME=${PROJECT_HOME}"/"
+else
+ APP_NAME="APPLICATION_NAME.exe"
+fi
+
+export CONF_CONSUMER_FILE_PATH=${PROJECT_HOME}"TARGET_CONF_FILE"
+export APP_LOG_CONF_FILE=${PROJECT_HOME}"TARGET_LOG_CONF_FILE"
+# export GOTRACEBACK=system
+# export GODEBUG=gctrace=1
+
+usage() {
+ echo "Usage: $0 start [conf suffix]"
+ echo " $0 stop"
+ echo " $0 term"
+ echo " $0 restart"
+ echo " $0 list"
+ echo " $0 monitor"
+ echo " $0 crontab"
+ exit
+}
+
+start() {
+ arg=$1
+ if [ "$arg" = "" ];then
+ echo "No registry type! Default client.yml!"
+ else
+ export CONF_CONSUMER_FILE_PATH=${CONF_CONSUMER_FILE_PATH//\.yml/\_$arg\.yml}
+ fi
+ if [ ! -f "${CONF_CONSUMER_FILE_PATH}" ];then
+ echo $CONF_CONSUMER_FILE_PATH" is not existing!"
+ return
+ fi
+ APP_LOG_PATH=${PROJECT_HOME}"logs/"
+ mkdir -p ${APP_LOG_PATH}
+ APP_BIN=${PROJECT_HOME}sbin/${APP_NAME}
+ chmod u+x ${APP_BIN}
+ # CMD="nohup ${APP_BIN} ${APP_ARGS} >>${APP_NAME}.nohup.out 2>&1 &"
+ CMD="${APP_BIN}"
+ eval ${CMD}
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $2}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $1}'`
+ fi
+ CUR=`date +%FT%T`
+ if [ "${PID}" != "" ]; then
+ for p in ${PID}
+ do
+ echo "start ${APP_NAME} ( pid =" ${p} ") at " ${CUR}
+ done
+ fi
+}
+
+stop() {
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $2}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $1}'`
+ fi
+ if [ "${PID}" != "" ];
+ then
+ for ps in ${PID}
+ do
+ echo "kill -SIGINT ${APP_NAME} ( pid =" ${ps} ")"
+ kill -2 ${ps}
+ done
+ fi
+}
+
+
+term() {
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $2}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $1}'`
+ fi
+ if [ "${PID}" != "" ];
+ then
+ for ps in ${PID}
+ do
+ echo "kill -9 ${APP_NAME} ( pid =" ${ps} ")"
+ kill -9 ${ps}
+ done
+ fi
+}
+
+list() {
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{printf("%s,%s,%s,%s\n", $1, $2, $9, $10)}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{printf("%s,%s,%s,%s,%s\n", $1, $4, $6, $7, $8)}'`
+ fi
+
+ if [ "${PID}" != "" ]; then
+ echo "list ${APP_NAME}"
+
+ if [[ ${OS_NAME} == "Linux" || ${OS_NAME} == "Darwin" ]]; then
+ echo "index: user, pid, start, duration"
+ else
+ echo "index: PID, WINPID, UID, STIME, COMMAND"
+ fi
+ idx=0
+ for ps in ${PID}
+ do
+ echo "${idx}: ${ps}"
+ ((idx ++))
+ done
+ fi
+}
+
+monitor() {
+ idx=0
+ while true; do
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $2}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $1}'`
+ fi
+ if [[ "${PID}" == "" ]]; then
+ start
+ idx=0
+ fi
+
+ ((LIFE=idx*${SLEEP_INTERVAL}))
+ echo "${APP_NAME} ( pid = " ${PID} ") has been working in normal state for " $LIFE " seconds."
+ ((idx ++))
+ sleep ${SLEEP_INTERVAL}
+ done
+}
+
+crontab() {
+ idx=0
+ while true; do
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $2}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $1}'`
+ fi
+ if [[ "${PID}" == "" ]]; then
+ start
+ idx=0
+ fi
+
+ ((LIFE=idx*${SLEEP_INTERVAL}))
+ echo "${APP_NAME} ( pid = " ${PID} ") has been working in normal state for " $LIFE " seconds."
+ ((idx ++))
+ sleep ${SLEEP_INTERVAL}
+ if [[ ${LIFE} -gt ${MAX_LIFETIME} ]]; then
+ kill -9 ${PID}
+ fi
+ done
+}
+
+opt=$1
+case C"$opt" in
+ Cstart)
+ start $2
+ ;;
+ Cstop)
+ stop
+ ;;
+ Cterm)
+ term
+ ;;
+ Crestart)
+ term
+ start $2
+ ;;
+ Clist)
+ list
+ ;;
+ Cmonitor)
+ monitor
+ ;;
+ Ccrontab)
+ crontab
+ ;;
+ C*)
+ usage
+ ;;
+esac
\ No newline at end of file
diff --git a/golang/registry/etcd/go-client/assembly/common/app.properties b/golang/registry/etcd/go-client/assembly/common/app.properties
new file mode 100644
index 0000000..5708ca9
--- /dev/null
+++ b/golang/registry/etcd/go-client/assembly/common/app.properties
@@ -0,0 +1,6 @@
+export TARGET_EXEC_NAME="user_info_client"
+# BUILD_PACKAGE="dubbogo-examples/user-info/client/app"
+export BUILD_PACKAGE="app"
+
+export TARGET_CONF_FILE="conf/client.yml"
+export TARGET_LOG_CONF_FILE="conf/log.yml"
\ No newline at end of file
diff --git a/golang/registry/etcd/go-client/assembly/common/build.sh b/golang/registry/etcd/go-client/assembly/common/build.sh
new file mode 100644
index 0000000..2afbb49
--- /dev/null
+++ b/golang/registry/etcd/go-client/assembly/common/build.sh
@@ -0,0 +1,65 @@
+rm -rf target/
+
+PROJECT_HOME=`pwd`
+TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS}
+
+TARGET_SBIN_NAME=${TARGET_EXEC_NAME}
+version=`cat app/version.go | grep Version | grep -v "Apache" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'`
+if [[ ${GOOS} == "windows" ]]; then
+ TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe
+fi
+TARGET_NAME=${TARGET_FOLDER}/${TARGET_SBIN_NAME}
+if [[ $PROFILE == "dev" || $PROFILE == "test" ]]; then
+ # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出
+ # GFLAGS=-gcflags "-N -l" -race -v
+ # GFLAGS="-gcflags \"-N -l\" -v"
+ cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH GO111MODULE=on go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd -
+else
+ # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果),
+ # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试,
+ # -w基本没啥损失。-s的损失就有点大了。
+ cd ${BUILD_PACKAGE} && GOOS=$GOOS GOARCH=$GOARCH GO111MODULE=on go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd -
+fi
+
+TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE}
+
+mkdir -p ${TARGET_FOLDER}/${TAR_NAME}
+
+SBIN_DIR=${TARGET_FOLDER}/${TAR_NAME}/sbin
+BIN_DIR=${TARGET_FOLDER}/${TAR_NAME}
+CONF_DIR=${TARGET_FOLDER}/${TAR_NAME}/conf
+
+mkdir -p ${SBIN_DIR}
+mkdir -p ${CONF_DIR}
+
+mv ${TARGET_NAME} ${SBIN_DIR}
+cp -r assembly/bin ${BIN_DIR}
+cd ${BIN_DIR}/bin/ && mv load.sh load_${TARGET_EXEC_NAME}.sh && cd -
+
+platform=$(uname)
+# modify APPLICATION_NAME
+if [ ${platform} == "Darwin" ]; then
+ sed -i "" "s~APPLICATION_NAME~${TARGET_EXEC_NAME}~g" ${BIN_DIR}/bin/*
+else
+ sed -i "s~APPLICATION_NAME~${TARGET_EXEC_NAME}~g" ${BIN_DIR}/bin/*
+fi
+
+# modify TARGET_CONF_FILE
+if [ ${platform} == "Darwin" ]; then
+ sed -i "" "s~TARGET_CONF_FILE~${TARGET_CONF_FILE}~g" ${BIN_DIR}/bin/*
+else
+ sed -i "s~TARGET_CONF_FILE~${TARGET_CONF_FILE}~g" ${BIN_DIR}/bin/*
+fi
+
+# modify TARGET_LOG_CONF_FILE
+if [ ${platform} == "Darwin" ]; then
+ sed -i "" "s~TARGET_LOG_CONF_FILE~${TARGET_LOG_CONF_FILE}~g" ${BIN_DIR}/bin/*
+else
+ sed -i "s~TARGET_LOG_CONF_FILE~${TARGET_LOG_CONF_FILE}~g" ${BIN_DIR}/bin/*
+fi
+
+cp -r profiles/${PROFILE}/* ${CONF_DIR}
+
+cd ${TARGET_FOLDER}
+
+tar czf ${TAR_NAME}.tar.gz ${TAR_NAME}/*
\ No newline at end of file
diff --git a/golang/registry/etcd/go-client/assembly/mac/dev.sh b/golang/registry/etcd/go-client/assembly/mac/dev.sh
new file mode 100755
index 0000000..d242789
--- /dev/null
+++ b/golang/registry/etcd/go-client/assembly/mac/dev.sh
@@ -0,0 +1,17 @@
+set -e
+
+export GOOS=darwin
+export GOARCH=amd64
+
+export PROFILE="dev"
+
+export PROJECT_HOME=`pwd`
+
+if [ -f "${PROJECT_HOME}/assembly/common/app.properties" ]; then
+ . ${PROJECT_HOME}/assembly/common/app.properties
+fi
+
+
+if [ -f "${PROJECT_HOME}/assembly/common/build.sh" ]; then
+ sh ${PROJECT_HOME}/assembly/common/build.sh
+fi
\ No newline at end of file
diff --git a/golang/registry/etcd/go-client/profiles/dev/client.yml b/golang/registry/etcd/go-client/profiles/dev/client.yml
new file mode 100644
index 0000000..f3b4b17
--- /dev/null
+++ b/golang/registry/etcd/go-client/profiles/dev/client.yml
@@ -0,0 +1,60 @@
+# dubbo client yaml configure file
+
+
+check: true
+# client
+request_timeout : "3s"
+# connect timeout
+connect_timeout : "3s"
+
+# application config
+application:
+ organization : "ikurento.com"
+ name : "BDTService"
+ module : "dubbogo user-info client"
+ version : "0.0.1"
+ owner : "ZX"
+ environment : "dev"
+
+registries :
+ "etcd":
+ protocol: "etcdv3"
+ timeout : "3s"
+ address: "127.0.0.1:2379"
+ username: ""
+ password: ""
+
+
+references:
+ "UserProvider":
+ # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
+ protocol : "dubbo"
+ interface : "com.ikurento.user.UserProvider"
+ cluster: "failover"
+ methods :
+ - name: "GetUser"
+ retries: 3
+
+
+protocol_conf:
+ dubbo:
+ reconnect_interval: 0
+ connection_number: 1
+ heartbeat_period: "5s"
+ session_timeout: "180s"
+ pool_size: 64
+ pool_ttl: 600
+ getty_session_param:
+ compress_encoding: false
+ tcp_no_delay: true
+ tcp_keep_alive: true
+ keep_alive_period: "120s"
+ tcp_r_buf_size: 262144
+ tcp_w_buf_size: 65536
+ pkg_rq_size: 1024
+ pkg_wq_size: 512
+ tcp_read_timeout: "1s"
+ tcp_write_timeout: "5s"
+ wait_timeout: "1s"
+ max_msg_len: 1024000
+ session_name: "client"
diff --git a/golang/registry/etcd/go-client/profiles/dev/log.yml b/golang/registry/etcd/go-client/profiles/dev/log.yml
new file mode 100644
index 0000000..1d68ea3
--- /dev/null
+++ b/golang/registry/etcd/go-client/profiles/dev/log.yml
@@ -0,0 +1,28 @@
+
+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:
\ No newline at end of file
diff --git a/golang/registry/etcd/go-server/app/server.go b/golang/registry/etcd/go-server/app/server.go
new file mode 100644
index 0000000..0a85fa2
--- /dev/null
+++ b/golang/registry/etcd/go-server/app/server.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "os/signal"
+ "syscall"
+ "time"
+)
+import (
+ hessian "github.com/apache/dubbo-go-hessian2"
+ "github.com/apache/dubbo-go/common/logger"
+ "github.com/apache/dubbo-go/config"
+ _ "github.com/apache/dubbo-go/protocol/dubbo"
+ _ "github.com/apache/dubbo-go/registry/protocol"
+
+ _ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
+ _ "github.com/apache/dubbo-go/filter/filter_impl"
+
+ _ "github.com/apache/dubbo-go/cluster/cluster_impl"
+ _ "github.com/apache/dubbo-go/cluster/loadbalance"
+
+ // import etcdv3 to init etcdv3 registry
+ _ "github.com/apache/dubbo-go/registry/etcdv3"
+)
+
+var (
+ survivalTimeout = int(3e9)
+)
+
+// they are necessary:
+// export CONF_PROVIDER_FILE_PATH="xxx"
+// export APP_LOG_CONF_FILE="xxx"
+func main() {
+
+ hessian.RegisterPOJO(&User{})
+ config.Load()
+
+ logger.Info("xxx")
+ initSignal()
+}
+
+func initSignal() {
+ signals := make(chan os.Signal, 1)
+ // It is not possible to block SIGKILL or syscall.SIGSTOP
+ signal.Notify(signals, os.Interrupt, os.Kill, 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/golang/registry/etcd/go-server/app/user.go b/golang/registry/etcd/go-server/app/user.go
new file mode 100644
index 0000000..e094dbc
--- /dev/null
+++ b/golang/registry/etcd/go-server/app/user.go
@@ -0,0 +1,47 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "time"
+)
+
+import (
+ hessian "github.com/apache/dubbo-go-hessian2"
+ "github.com/apache/dubbo-go/config"
+)
+
+func init() {
+ config.SetProviderService(new(UserProvider))
+ // ------for hessian2------
+ hessian.RegisterPOJO(&User{})
+}
+
+type User struct {
+ Id string
+ Name string
+ Age int32
+ Time time.Time
+}
+
+type UserProvider struct {
+}
+
+func (u *UserProvider) GetUser(ctx context.Context, req []interface{}) (*User, error) {
+ println("req:%#v", req)
+ rsp := User{"A001", "Alex Stocks", 18, time.Now()}
+ println("rsp:%#v", rsp)
+ return &rsp, nil
+}
+
+func (u *UserProvider) Reference() string {
+ return "UserProvider"
+}
+
+func (u User) JavaClassName() string {
+ return "com.ikurento.user.User"
+}
+
+func println(format string, args ...interface{}) {
+ fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...)
+}
diff --git a/golang/registry/etcd/go-server/app/version.go b/golang/registry/etcd/go-server/app/version.go
new file mode 100644
index 0000000..60c1a81
--- /dev/null
+++ b/golang/registry/etcd/go-server/app/version.go
@@ -0,0 +1,5 @@
+package main
+
+var (
+ Version = "2.6.0"
+)
diff --git a/golang/registry/etcd/go-server/assembly/bin/load.sh b/golang/registry/etcd/go-server/assembly/bin/load.sh
new file mode 100644
index 0000000..d247297
--- /dev/null
+++ b/golang/registry/etcd/go-server/assembly/bin/load.sh
@@ -0,0 +1,132 @@
+APP_NAME="APPLICATION_NAME"
+APP_ARGS=""
+
+
+PROJECT_HOME=""
+OS_NAME=`uname`
+if [[ ${OS_NAME} != "Windows" ]]; then
+ PROJECT_HOME=`pwd`
+ PROJECT_HOME=${PROJECT_HOME}"/"
+fi
+
+export CONF_PROVIDER_FILE_PATH=${PROJECT_HOME}"TARGET_CONF_FILE"
+export APP_LOG_CONF_FILE=${PROJECT_HOME}"TARGET_LOG_CONF_FILE"
+
+usage() {
+ echo "Usage: $0 start [conf suffix]"
+ echo " $0 stop"
+ echo " $0 term"
+ echo " $0 restart"
+ echo " $0 list"
+ echo " $0 monitor"
+ echo " $0 crontab"
+ exit
+}
+
+start() {
+ arg=$1
+ if [ "$arg" = "" ];then
+ echo "No registry type! Default server.yml!"
+ else
+ export CONF_PROVIDER_FILE_PATH=${CONF_PROVIDER_FILE_PATH//\.yml/\_$arg\.yml}
+ fi
+ if [ ! -f "${CONF_PROVIDER_FILE_PATH}" ];then
+ echo $CONF_PROVIDER_FILE_PATH" is not existing!"
+ return
+ fi
+ APP_LOG_PATH="${PROJECT_HOME}logs/"
+ mkdir -p ${APP_LOG_PATH}
+ APP_BIN=${PROJECT_HOME}sbin/${APP_NAME}
+ chmod u+x ${APP_BIN}
+ # CMD="nohup ${APP_BIN} ${APP_ARGS} >>${APP_NAME}.nohup.out 2>&1 &"
+ CMD="${APP_BIN}"
+ eval ${CMD}
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $2}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $1}'`
+ fi
+ CUR=`date +%FT%T`
+ if [ "${PID}" != "" ]; then
+ for p in ${PID}
+ do
+ echo "start ${APP_NAME} ( pid =" ${p} ") at " ${CUR}
+ done
+ fi
+}
+
+stop() {
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $2}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $1}'`
+ fi
+ if [ "${PID}" != "" ];
+ then
+ for ps in ${PID}
+ do
+ echo "kill -SIGINT ${APP_NAME} ( pid =" ${ps} ")"
+ kill -2 ${ps}
+ done
+ fi
+}
+
+
+term() {
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $2}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{print $1}'`
+ fi
+ if [ "${PID}" != "" ];
+ then
+ for ps in ${PID}
+ do
+ echo "kill -9 ${APP_NAME} ( pid =" ${ps} ")"
+ kill -9 ${ps}
+ done
+ fi
+}
+
+list() {
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{printf("%s,%s,%s,%s\n", $1, $2, $9, $10)}'`
+ if [[ ${OS_NAME} != "Linux" && ${OS_NAME} != "Darwin" ]]; then
+ PID=`ps aux | grep -w ${APP_NAME} | grep -v grep | awk '{printf("%s,%s,%s,%s,%s\n", $1, $4, $6, $7, $8)}'`
+ fi
+
+ if [ "${PID}" != "" ]; then
+ echo "list ${APP_NAME}"
+
+ if [[ ${OS_NAME} == "Linux" || ${OS_NAME} == "Darwin" ]]; then
+ echo "index: user, pid, start, duration"
+ else
+ echo "index: PID, WINPID, UID, STIME, COMMAND"
+ fi
+ idx=0
+ for ps in ${PID}
+ do
+ echo "${idx}: ${ps}"
+ ((idx ++))
+ done
+ fi
+}
+
+opt=$1
+case C"$opt" in
+ Cstart)
+ start $2
+ ;;
+ Cstop)
+ stop
+ ;;
+ Cterm)
+ term
+ ;;
+ Crestart)
+ term
+ start $2
+ ;;
+ Clist)
+ list
+ ;;
+ C*)
+ usage
+ ;;
+esac
\ No newline at end of file
diff --git a/golang/registry/etcd/go-server/assembly/common/app.properties b/golang/registry/etcd/go-server/assembly/common/app.properties
new file mode 100644
index 0000000..8e0d12b
--- /dev/null
+++ b/golang/registry/etcd/go-server/assembly/common/app.properties
@@ -0,0 +1,6 @@
+TARGET_EXEC_NAME="user_info_server"
+# BUILD_PACKAGE="dubbogo-examples/user-info/server/app"
+BUILD_PACKAGE="app"
+
+TARGET_CONF_FILE="conf/server.yml"
+TARGET_LOG_CONF_FILE="conf/log.yml"
\ No newline at end of file
diff --git a/golang/registry/etcd/go-server/assembly/common/build.sh b/golang/registry/etcd/go-server/assembly/common/build.sh
new file mode 100755
index 0000000..9b09954
--- /dev/null
+++ b/golang/registry/etcd/go-server/assembly/common/build.sh
@@ -0,0 +1,62 @@
+rm -rf target/
+
+PROJECT_HOME=`pwd`
+TARGET_FOLDER=${PROJECT_HOME}/target/${GOOS}
+
+TARGET_SBIN_NAME=${TARGET_EXEC_NAME}
+version=`cat app/version.go | grep Version | grep -v "Apache" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}'`
+if [[ ${GOOS} == "windows" ]]; then
+ TARGET_SBIN_NAME=${TARGET_SBIN_NAME}.exe
+fi
+TARGET_NAME=${TARGET_FOLDER}/${TARGET_SBIN_NAME}
+if [[ $PROFILE = "test" ]]; then
+ # GFLAGS=-gcflags "-N -l" -race -x -v # -x会把go build的详细过程输出
+ # GFLAGS=-gcflags "-N -l" -race -v
+ # GFLAGS="-gcflags \"-N -l\" -v"
+ cd ${BUILD_PACKAGE} && GO111MODULE=on go build -gcflags "-N -l" -x -v -i -o ${TARGET_NAME} && cd -
+else
+ # -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了,这个等价于普通C/C++程序被strip的效果),
+ # -w去掉DWARF调试信息,得到的程序就不能用gdb调试了。-s和-w也可以分开使用,一般来说如果不打算用gdb调试,
+ # -w基本没啥损失。-s的损失就有点大了。
+ cd ${BUILD_PACKAGE} && GO111MODULE=on go build -ldflags "-w" -x -v -i -o ${TARGET_NAME} && cd -
+fi
+
+TAR_NAME=${TARGET_EXEC_NAME}-${version}-`date "+%Y%m%d-%H%M"`-${PROFILE}
+
+mkdir -p ${TARGET_FOLDER}/${TAR_NAME}
+
+SBIN_DIR=${TARGET_FOLDER}/${TAR_NAME}/sbin
+BIN_DIR=${TARGET_FOLDER}/${TAR_NAME}
+CONF_DIR=${TARGET_FOLDER}/${TAR_NAME}/conf
+
+mkdir -p ${SBIN_DIR}
+mkdir -p ${CONF_DIR}
+
+mv ${TARGET_NAME} ${SBIN_DIR}
+cp -r assembly/bin ${BIN_DIR}
+# modify APPLICATION_NAME
+# OS=`uname`
+# if [[ $OS=="Darwin" ]]; then
+if [ "$(uname)" == "Darwin" ]; then
+ sed -i "" "s~APPLICATION_NAME~${TARGET_EXEC_NAME}~g" ${BIN_DIR}/bin/*
+else
+ sed -i "s~APPLICATION_NAME~${TARGET_EXEC_NAME}~g" ${BIN_DIR}/bin/*
+fi
+# modify TARGET_CONF_FILE
+if [ "$(uname)" == "Darwin" ]; then
+ sed -i "" "s~TARGET_CONF_FILE~${TARGET_CONF_FILE}~g" ${BIN_DIR}/bin/*
+else
+ sed -i "s~TARGET_CONF_FILE~${TARGET_CONF_FILE}~g" ${BIN_DIR}/bin/*
+fi
+# modify TARGET_LOG_CONF_FILE
+if [ "$(uname)" == "Darwin" ]; then
+ sed -i "" "s~TARGET_LOG_CONF_FILE~${TARGET_LOG_CONF_FILE}~g" ${BIN_DIR}/bin/*
+else
+ sed -i "s~TARGET_LOG_CONF_FILE~${TARGET_LOG_CONF_FILE}~g" ${BIN_DIR}/bin/*
+fi
+
+cp -r profiles/${PROFILE}/* ${CONF_DIR}
+
+cd ${TARGET_FOLDER}
+
+tar czf ${TAR_NAME}.tar.gz ${TAR_NAME}/*
\ No newline at end of file
diff --git a/golang/registry/etcd/go-server/assembly/mac/dev.sh b/golang/registry/etcd/go-server/assembly/mac/dev.sh
new file mode 100755
index 0000000..b1bca8b
--- /dev/null
+++ b/golang/registry/etcd/go-server/assembly/mac/dev.sh
@@ -0,0 +1,17 @@
+set -e
+
+export GOOS=darwin
+export GOARCH=amd64
+
+PROFILE=dev
+
+PROJECT_HOME=`pwd`
+
+if [ -f "${PROJECT_HOME}/assembly/common/app.properties" ]; then
+. ${PROJECT_HOME}/assembly/common/app.properties
+fi
+
+
+if [ -f "${PROJECT_HOME}/assembly/common/build.sh" ]; then
+. ${PROJECT_HOME}/assembly/common/build.sh
+fi
\ No newline at end of file
diff --git a/golang/registry/etcd/go-server/assembly/mac/release.sh b/golang/registry/etcd/go-server/assembly/mac/release.sh
new file mode 100644
index 0000000..9fa8786
--- /dev/null
+++ b/golang/registry/etcd/go-server/assembly/mac/release.sh
@@ -0,0 +1,18 @@
+
+set -e
+
+export GOOS=darwin
+export GOARCH=amd64
+
+PROFILE=release
+
+PROJECT_HOME=`pwd`
+
+if [ -f "${PROJECT_HOME}/assembly/common/app.properties" ]; then
+. ${PROJECT_HOME}/assembly/common/app.properties
+fi
+
+
+if [ -f "${PROJECT_HOME}/assembly/common/build.sh" ]; then
+. ${PROJECT_HOME}/assembly/common/build.sh
+fi
\ No newline at end of file
diff --git a/golang/registry/etcd/go-server/assembly/mac/test.sh b/golang/registry/etcd/go-server/assembly/mac/test.sh
new file mode 100644
index 0000000..83e2d15
--- /dev/null
+++ b/golang/registry/etcd/go-server/assembly/mac/test.sh
@@ -0,0 +1,17 @@
+set -e
+
+export GOOS=darwin
+export GOARCH=amd64
+
+PROFILE=test
+
+PROJECT_HOME=`pwd`
+
+if [ -f "${PROJECT_HOME}/assembly/common/app.properties" ]; then
+. ${PROJECT_HOME}/assembly/common/app.properties
+fi
+
+
+if [ -f "${PROJECT_HOME}/assembly/common/build.sh" ]; then
+. ${PROJECT_HOME}/assembly/common/build.sh
+fi
\ No newline at end of file
diff --git a/golang/registry/etcd/go-server/profiles/dev/log.yml b/golang/registry/etcd/go-server/profiles/dev/log.yml
new file mode 100644
index 0000000..ed4d3cb
--- /dev/null
+++ b/golang/registry/etcd/go-server/profiles/dev/log.yml
@@ -0,0 +1,27 @@
+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:
\ No newline at end of file
diff --git a/golang/registry/etcd/go-server/profiles/dev/server.yml b/golang/registry/etcd/go-server/profiles/dev/server.yml
new file mode 100644
index 0000000..cd78586
--- /dev/null
+++ b/golang/registry/etcd/go-server/profiles/dev/server.yml
@@ -0,0 +1,57 @@
+# dubbo server yaml configure file
+
+
+# application config
+application:
+ organization : "ikurento.com"
+ name : "BDTService"
+ module : "dubbogo user-info server"
+ version : "0.0.1"
+ owner : "ZX"
+ environment : "dev"
+
+registries :
+ "etcd":
+ protocol: "etcdv3"
+ timeout : "3s"
+ address: "127.0.0.1:2379"
+
+services:
+ "UserProvider":
+ # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
+ registry: "etcd"
+ protocol : "dubbo"
+ # 相当于dubbo.xml中的interface
+ interface : "com.ikurento.user.UserProvider"
+ loadbalance: "random"
+ warmup: "100"
+ cluster: "failover"
+ methods:
+ - name: "GetUser"
+ retries: 1
+ loadbalance: "random"
+
+protocols:
+ "dubbo":
+ name: "dubbo"
+ port: 20000
+
+
+protocol_conf:
+ dubbo:
+ session_number: 700
+ session_timeout: "180s"
+ getty_session_param:
+ compress_encoding: false
+ tcp_no_delay: true
+ tcp_keep_alive: true
+ keep_alive_period: "120s"
+ tcp_r_buf_size: 262144
+ tcp_w_buf_size: 65536
+ pkg_rq_size: 1024
+ pkg_wq_size: 512
+ tcp_read_timeout: "1s"
+ tcp_write_timeout: "5s"
+ wait_timeout: "1s"
+ max_msg_len: 1024000
+ session_name: "server"
\ No newline at end of file