Skip to content

Commit aed967e

Browse files
Fix: add some samples (#295)
* Fix: add some samples * fix: fmt * Fix: update tag
1 parent f1b5bdb commit aed967e

File tree

47 files changed

+972
-38
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+972
-38
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="error-triple-hessian-client" type="GoApplicationRunConfiguration" factoryName="Go Application" folderName="error">
3+
<module name="dubbo-go-samples" />
4+
<working_directory value="$PROJECT_DIR$" />
5+
<envs>
6+
<env name="DUBBO_GO_CONFIG_PATH" value="$PROJECT_DIR$/error/triple/hessian2/go-client/conf/dubbogo.yml" />
7+
</envs>
8+
<kind value="PACKAGE" />
9+
<package value="github.com/apache/dubbo-go-samples/error/triple/hessian2/go-client/cmd" />
10+
<directory value="$PROJECT_DIR$" />
11+
<filePath value="$PROJECT_DIR$/rpc/triple/pb/dubbogo-grpc/grpc-client/main.go" />
12+
<method v="2" />
13+
</configuration>
14+
</component>
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="error-triple-hessian-server" type="GoApplicationRunConfiguration" factoryName="Go Application" folderName="error">
3+
<module name="dubbo-go-samples" />
4+
<working_directory value="$PROJECT_DIR$" />
5+
<envs>
6+
<env name="DUBBO_GO_CONFIG_PATH" value="$PROJECT_DIR$/error/triple/hessian2/go-server/conf/dubbogo.yml" />
7+
</envs>
8+
<kind value="PACKAGE" />
9+
<package value="github.com/apache/dubbo-go-samples/error/triple/hessian2/go-server/cmd" />
10+
<directory value="$PROJECT_DIR$" />
11+
<filePath value="$PROJECT_DIR$/rpc/triple/pb/dubbogo-grpc/grpc-server/main.go" />
12+
<method v="2" />
13+
</configuration>
14+
</component>

.run/error-triple-pb-client.run.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="error-triple-pb-client" type="GoApplicationRunConfiguration" factoryName="Go Application" folderName="error">
3+
<module name="dubbo-go-samples" />
4+
<working_directory value="$PROJECT_DIR$" />
5+
<envs>
6+
<env name="DUBBO_GO_CONFIG_PATH" value="$PROJECT_DIR$/error/triple/pb/go-client/conf/dubbogo.yml" />
7+
</envs>
8+
<kind value="PACKAGE" />
9+
<package value="github.com/apache/dubbo-go-samples/error/triple/pb/go-client/cmd" />
10+
<directory value="$PROJECT_DIR$" />
11+
<filePath value="$PROJECT_DIR$/rpc/triple/pb/dubbogo-grpc/grpc-client/main.go" />
12+
<method v="2" />
13+
</configuration>
14+
</component>

.run/error-triple-pb-server.run.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="error-triple-pb-server" type="GoApplicationRunConfiguration" factoryName="Go Application" folderName="error">
3+
<module name="dubbo-go-samples" />
4+
<working_directory value="$PROJECT_DIR$" />
5+
<envs>
6+
<env name="DUBBO_GO_CONFIG_PATH" value="$PROJECT_DIR$/error/triple/pb/go-server/conf/dubbogo.yml" />
7+
</envs>
8+
<kind value="PACKAGE" />
9+
<package value="github.com/apache/dubbo-go-samples/error/triple/pb/go-server/cmd" />
10+
<directory value="$PROJECT_DIR$" />
11+
<filePath value="$PROJECT_DIR$/rpc/triple/pb/dubbogo-grpc/grpc-server/main.go" />
12+
<method v="2" />
13+
</configuration>
14+
</component>

api/generate.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export GO111MODULE="on"
22
export GOPROXY="http://goproxy.io"
3-
go get -u github.com/dubbogo/tools/cmd/protoc-gen-go-triple
3+
go install github.com/dubbogo/tools/cmd/protoc-gen-go-triple@v1.0.2
44
protoc --go_out=. --go-triple_out=. samples_api.proto

api/samples_api.pb.go

+7-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/samples_api_triple.pb.go

+14-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

context/triple/go-client/cmd/client.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ import (
2222
)
2323

2424
import (
25+
"dubbo.apache.org/dubbo-go/v3/common/constant"
2526
"dubbo.apache.org/dubbo-go/v3/common/logger"
2627
"dubbo.apache.org/dubbo-go/v3/config"
2728
_ "dubbo.apache.org/dubbo-go/v3/imports"
28-
29-
tripleConstant "github.com/dubbogo/triple/pkg/common/constant"
3029
)
3130

3231
import (
@@ -51,8 +50,13 @@ func main() {
5150
Name: "laurence",
5251
}
5352
ctx := context.Background()
54-
// set user defined context attachment
55-
ctx = context.WithValue(ctx, tripleConstant.CtxAttachmentKey, "user-defined-value")
53+
// set user defined context attachment, map value can be string or []string, otherwise it is not to be transferred
54+
userDefinedValueMap := make(map[string]interface{})
55+
userDefinedValueMap["key1"] = "user defined value 1"
56+
userDefinedValueMap["key2"] = "user defined value 2"
57+
userDefinedValueMap["key3"] = []string{"user defined value 3.1", "user defined value 3.2"}
58+
userDefinedValueMap["key4"] = []string{"user defined value 4.1", "user defined value 4.2"}
59+
ctx = context.WithValue(ctx, constant.AttachmentKey, userDefinedValueMap)
5660
reply, err := grpcGreeterImpl.SayHello(ctx, req)
5761
if err != nil {
5862
logger.Error(err)

context/triple/go-server/cmd/server.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ package main
1919

2020
import (
2121
"context"
22+
"fmt"
23+
"strings"
2224
)
2325

2426
import (
27+
"dubbo.apache.org/dubbo-go/v3/common/constant"
2528
"dubbo.apache.org/dubbo-go/v3/common/logger"
2629
"dubbo.apache.org/dubbo-go/v3/config"
2730
_ "dubbo.apache.org/dubbo-go/v3/imports"
28-
29-
tripleConstant "github.com/dubbogo/triple/pkg/common/constant"
3031
)
3132

3233
import (
@@ -38,9 +39,24 @@ type GreeterProvider struct {
3839
}
3940

4041
func (s *GreeterProvider) SayHello(ctx context.Context, in *api.HelloRequest) (*api.User, error) {
41-
logger.Infof("get triple user attachment = %s", ctx.Value(tripleConstant.CtxAttachmentKey))
42+
// map must be assert to map[string]interface, because of dubbo limitation
43+
attachments := ctx.Value(constant.AttachmentKey).(map[string]interface{})
44+
45+
// value must be assert to []string[0], because of http2 header limitation
46+
logger.Infof("get triple attachment key1 = %s", attachments["key1"].([]string)[0])
47+
logger.Infof("get triple attachment key2 = %s", attachments["key2"].([]string)[0])
48+
logger.Infof("get triple attachment key3 = %s and %s", attachments["key3"].([]string)[0],
49+
attachments["key3"].([]string)[1])
50+
logger.Infof("get triple attachment key4 = %s and %s", attachments["key4"].([]string)[0],
51+
attachments["key4"].([]string)[1])
4252
logger.Infof("Dubbo3 GreeterProvider get user name = %s\n", in.Name)
43-
return &api.User{Name: "Hello " + in.Name, Id: "12345", Age: 21}, nil
53+
rspAttachment := make(map[string]interface{})
54+
for k, v := range attachments {
55+
if strings.HasPrefix(k, "key") {
56+
rspAttachment[k] = v
57+
}
58+
}
59+
return &api.User{Name: fmt.Sprintf("%s", rspAttachment), Id: "12345", Age: 21}, nil
4460
}
4561

4662
func main() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package main
19+
20+
import (
21+
"context"
22+
)
23+
24+
import (
25+
"dubbo.apache.org/dubbo-go/v3/common/logger"
26+
"dubbo.apache.org/dubbo-go/v3/config"
27+
_ "dubbo.apache.org/dubbo-go/v3/imports"
28+
29+
hessian "github.com/apache/dubbo-go-hessian2"
30+
31+
tripleCommon "github.com/dubbogo/triple/pkg/common"
32+
)
33+
34+
var errorResponseProvider = new(ErrorResponseProvider)
35+
36+
func init() {
37+
config.SetConsumerService(errorResponseProvider)
38+
hessian.RegisterPOJO(&User{})
39+
}
40+
41+
// need to setup environment variable "CONF_CONSUMER_FILE_PATH" to "conf/client.yml" before run
42+
func main() {
43+
if err := config.Load(); err != nil {
44+
panic(err)
45+
}
46+
testErrorService()
47+
testService()
48+
}
49+
50+
func testErrorService() {
51+
if user, err := errorResponseProvider.GetUser(context.TODO(), &User{Name: "laurence"}); err != nil {
52+
logger.Infof("response result: %v, error = %s", user, err)
53+
logger.Infof("error details = %+v", err.(tripleCommon.TripleError).Stacks())
54+
}
55+
}
56+
57+
func testService() {
58+
if user, err := errorResponseProvider.GetUserWithoutError(context.TODO(), &User{Name: "laurence"}); err != nil {
59+
logger.Infof("response result: %v, error = %s", user, err)
60+
logger.Infof("error details = %+v", err.(tripleCommon.TripleError).Stacks())
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package main
19+
20+
import (
21+
"context"
22+
)
23+
24+
type User struct {
25+
Id string
26+
Name string
27+
Age int32
28+
}
29+
30+
type ErrorResponseProvider struct {
31+
GetUser func(ctx context.Context, req *User) (*User, error)
32+
GetUserWithoutError func(ctx context.Context, req *User) (*User, error)
33+
}
34+
35+
func (u *User) JavaClassName() string {
36+
return "com.apache.dubbo.sample.basic.User"
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
dubbo:
2+
registries:
3+
demoZK:
4+
protocol: zookeeper
5+
timeout: 3s
6+
address: 127.0.0.1:2181
7+
consumer:
8+
references:
9+
ErrorResponseProvider:
10+
protocol: tri
11+
serialization: hessian2
12+
interface: com.apache.dubbo.sample.basic.ErrorResponseProvider # must be compatible with grpc or dubbo-java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package main
19+
20+
import (
21+
"context"
22+
)
23+
24+
import (
25+
"dubbo.apache.org/dubbo-go/v3/common/logger"
26+
27+
"github.com/pkg/errors"
28+
)
29+
30+
type User struct {
31+
Id string
32+
Name string
33+
Age int32
34+
}
35+
36+
func (u *User) JavaClassName() string {
37+
return "com.apache.dubbo.sample.basic.User"
38+
}
39+
40+
type ErrorResponseProvider struct {
41+
}
42+
43+
func (u *ErrorResponseProvider) GetUser(ctx context.Context, usr *User) (*User, error) {
44+
logger.Infof("req:%#v", usr)
45+
rsp := User{"12345", "Hello " + usr.Name, 18}
46+
myError := errors.Errorf("user defined error")
47+
logger.Infof("rsp:%#v, err = %s", rsp, myError)
48+
return &rsp, myError
49+
}
50+
51+
func (u *ErrorResponseProvider) GetUserWithoutError(ctx context.Context, usr *User) (*User, error) {
52+
logger.Infof("req:%#v", usr)
53+
rsp := User{"12345", "Hello " + usr.Name, 18}
54+
logger.Infof("rsp:%#v, err = %s", rsp, nil)
55+
return &rsp, nil
56+
}

0 commit comments

Comments
 (0)