Skip to content

Commit

Permalink
Ftr/3.0msgpack and triple codec extension samples (apache#138)
Browse files Browse the repository at this point in the history
* fix: updata go get dubbo-gen script

* fix

* fix: add etcd test

* fix: add msgpack sample

* fix

* fix

* fix: fix conflict

* fix: add test

* Fix: split import block

* fix: delete unused block
  • Loading branch information
LaurenceLiZhixin committed Jun 14, 2021
1 parent 848b52b commit da8780a
Show file tree
Hide file tree
Showing 36 changed files with 1,119 additions and 7 deletions.
15 changes: 15 additions & 0 deletions .run/triple-codec-extension-client.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="triple-codec-extension-client" type="GoApplicationRunConfiguration" factoryName="Go Application" folderName="triple">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/general/dubbo3/codec-extension/go-client/conf/log.yml" />
<env name="CONF_CONSUMER_FILE_PATH" value="$PROJECT_DIR$/general/dubbo3/codec-extension/go-client/conf/client.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" />
<package value="github.com/apache/dubbo-go-samples/general/dubbo3/codec-extension/go-client/cmd" />
<directory value="github.com/apache/dubbo-go-samples/general/dubbo3/pb/dubbogo-grpc/server/dubbogo-server/cmd" />
<method v="2" />
</configuration>
</component>
15 changes: 15 additions & 0 deletions .run/triple-codec-extension-server.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="triple-codec-extension-server" type="GoApplicationRunConfiguration" factoryName="Go Application" folderName="triple">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/general/dubbo3/codec-extension/go-server/conf/log.yml" />
<env name="CONF_PROVIDER_FILE_PATH" value="$PROJECT_DIR$/general/dubbo3/codec-extension/go-server/conf/server.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" />
<package value="github.com/apache/dubbo-go-samples/general/dubbo3/codec-extension/go-server/cmd" />
<directory value="github.com/apache/dubbo-go-samples/general/dubbo3/pb/dubbogo-grpc/server/dubbogo-server/cmd" />
<method v="2" />
</configuration>
</component>
15 changes: 15 additions & 0 deletions .run/triple-msgpack-client.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="triple-msgpack-client" type="GoApplicationRunConfiguration" factoryName="Go Application" folderName="triple">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/general/dubbo3/msgpack/go-client/conf/log.yml" />
<env name="CONF_CONSUMER_FILE_PATH" value="$PROJECT_DIR$/general/dubbo3/msgpack/go-client/conf/client.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" />
<package value="github.com/apache/dubbo-go-samples/general/dubbo3/msgpack/go-client/cmd" />
<directory value="github.com/apache/dubbo-go-samples/general/dubbo3/pb/dubbogo-grpc/server/dubbogo-server/cmd" />
<method v="2" />
</configuration>
</component>
15 changes: 15 additions & 0 deletions .run/triple-msgpack-server.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="triple-msgpack-server" type="GoApplicationRunConfiguration" factoryName="Go Application" folderName="triple">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/general/dubbo3/msgpack/go-server/conf/log.yml" />
<env name="CONF_PROVIDER_FILE_PATH" value="$PROJECT_DIR$/general/dubbo3/msgpack/go-server/conf/server.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" />
<package value="github.com/apache/dubbo-go-samples/general/dubbo3/msgpack/go-server/cmd" />
<directory value="github.com/apache/dubbo-go-samples/general/dubbo3/pb/dubbogo-grpc/server/dubbogo-server/cmd" />
<method v="2" />
</configuration>
</component>
64 changes: 64 additions & 0 deletions general/dubbo3/codec-extension/go-client/cmd/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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"
"os"
"time"
)

import (
_ "dubbo.apache.org/dubbo-go/v3/cluster/cluster_impl"
_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
_ "dubbo.apache.org/dubbo-go/v3/common/proxy/proxy_factory"
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3"
_ "dubbo.apache.org/dubbo-go/v3/protocol/grpc"
_ "dubbo.apache.org/dubbo-go/v3/registry/protocol"
_ "dubbo.apache.org/dubbo-go/v3/registry/zookeeper"
"github.com/dubbogo/gost/log"
)

import (
_ "github.com/apache/dubbo-go-samples/general/dubbo3/codec-extension/go-client/codec"
"github.com/apache/dubbo-go-samples/general/dubbo3/codec-extension/go-client/pkg"
)

var userProvider = new(pkg.UserProvider)

func init() {
config.SetConsumerService(userProvider)
}

// need to setup environment variable "CONF_CONSUMER_FILE_PATH" to "conf/client.yml" before run
func main() {
config.Load()
time.Sleep(3 * time.Second)

gxlog.CInfo("\n\n\nstart to test dubbo")
user := &pkg.User{}
err := userProvider.GetUser(context.TODO(), &pkg.User{Name: "laurence"}, user)
if err != nil {
gxlog.CError("error: %v\n", err)
os.Exit(1)
return
}
gxlog.CInfo("response result: %v\n", user)
}
29 changes: 29 additions & 0 deletions general/dubbo3/codec-extension/go-client/codec/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package codec

import (
"encoding/json"
)

import (
triCommon "github.com/dubbogo/triple/pkg/common"
)


func init() {
triCommon.SetTripleCodec("json", NewJSONCodec)
}

func NewJSONCodec() triCommon.Codec {
return &JSONCodec{}
}

type JSONCodec struct {
}

func (j *JSONCodec) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

func (j *JSONCodec) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
33 changes: 33 additions & 0 deletions general/dubbo3/codec-extension/go-client/conf/client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# dubbo client yaml configure file

check: true
# client
request_timeout: "3s"
# connect timeout
connect_timeout: "3s"

# application config
application:
organization: "dubbo.io"
name: "UserProvider"
module: "dubbo-go3.0 client"
version: "0.0.1"
environment: "dev"

# registry config
registries:
"demoZk":
protocol: "zookeeper"
timeout: "3s"
address: "127.0.0.1:2181"
username: ""
password: ""

# reference config
references:
"UserProvider":
registry: "demoZk"
protocol: "tri"
serialization: "json"
# url: "dubbo://127.0.0.1:20000" # target server ip:port
interface: "com.apache.dubbo.sample.basic.IGreeter"
27 changes: 27 additions & 0 deletions general/dubbo3/codec-extension/go-client/conf/log.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
level: "info"
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: "capital"
timeEncoder: "iso8601"
durationEncoder: "seconds"
callerEncoder: "short"
nameEncoder: ""

outputPaths:
- "stderr"
errorOutputPaths:
- "stderr"
initialFields:
36 changes: 36 additions & 0 deletions general/dubbo3/codec-extension/go-client/pkg/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 pkg

import (
"context"
)

type User struct {
ID string
Name string
Age int32
}

type UserProvider struct {
GetUser func(ctx context.Context, req *User, rsp *User) error
}

func (u *UserProvider) Reference() string {
return "UserProvider"
}
83 changes: 83 additions & 0 deletions general/dubbo3/codec-extension/go-server/cmd/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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/cluster/cluster_impl"
_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
"dubbo.apache.org/dubbo-go/v3/common/logger"
_ "dubbo.apache.org/dubbo-go/v3/common/proxy/proxy_factory"
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3"
_ "dubbo.apache.org/dubbo-go/v3/registry/nacos"
_ "dubbo.apache.org/dubbo-go/v3/registry/protocol"
_ "dubbo.apache.org/dubbo-go/v3/registry/zookeeper"
_ "github.com/dubbogo/triple/pkg/triple"
)

import (
_ "github.com/apache/dubbo-go-samples/general/dubbo3/codec-extension/go-client/codec"
"github.com/apache/dubbo-go-samples/general/dubbo3/codec-extension/go-server/pkg"
)

var (
survivalTimeout = int(3 * time.Second)
)

func init() {
config.SetProviderService(new(pkg.UserProvider))
}

// need to setup environment variable "CONF_PROVIDER_FILE_PATH" to "conf/server.yml" before run
func main() {
config.Load()
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.Sleep(time.Second * 5)
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
}
}
}
25 changes: 25 additions & 0 deletions general/dubbo3/codec-extension/go-server/codec/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package codec

import (
"encoding/json"
triCommon "github.com/dubbogo/triple/pkg/common"
)

func init() {
triCommon.SetTripleCodec("json", NewJSONCodec)
}

func NewJSONCodec() triCommon.Codec {
return &JSONCodec{}
}

type JSONCodec struct {
}

func (j *JSONCodec) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

func (j *JSONCodec) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
Loading

0 comments on commit da8780a

Please sign in to comment.