Skip to content

Commit fdaedb8

Browse files
authored
feat: tls sample (#469)
* tls sample * tls sample
1 parent e7a8938 commit fdaedb8

Some content is hidden

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

43 files changed

+2494
-0
lines changed

tls/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Use TLS encryption in Dubbo go
2+
3+
## Usage
4+
5+
0. Generate the required certificate and secret key
6+
7+
This example provides the generated certificate and secret key under the directory `tls/x509`
8+
9+
1. Configure dubbogo.yaml
10+
11+
Client TLS configuration:
12+
13+
```yaml
14+
dubbo:
15+
tls_config:
16+
ca-cert-file: ../../../x509/server_ca_cert.pem
17+
tls-cert-file: ../../../x509/client2_cert.pem
18+
tls-key-file: ../../../x509/client2_key.pem
19+
tls-server-name: dubbogo.test.example.com
20+
```
21+
22+
Server TLS configuration:
23+
24+
```yaml
25+
dubbo:
26+
tls_config:
27+
ca-cert-file: ../../../x509/client_ca_cert.pem
28+
tls-cert-file: ../../../x509/server2_cert.pem
29+
tls-key-file: ../../../x509/server2_key.pem
30+
tls-server-name: dubbogo.test.example.com
31+
```
32+
33+
2. Startup example
34+
35+
This example provides TLS encryption examples of Dubbo, Grpc and Triple communication modes, respectively located in
36+
37+
`tls/dubbo` 、`tls/grpc` 、`tls/triple`。 Enter the folder to launch the sample.
38+
39+
Take tls/dubbo as an example:
40+
41+
Start the server:
42+
43+
Enter 'tls/dubbo/go server/cmd' and start 'server.go`
44+
45+
The TLS configuration takes effect when you see the following logs
46+
47+
```
48+
2022-12-01T23:39:30.690+0800 INFO getty/getty_ server. go:78 Getty Server initialized the TLSConfig configuration
49+
```
50+
51+
Start client:
52+
53+
Enter 'tls/dubbo/go client/cmd' and start 'client.go`
54+
55+
The TLS configuration takes effect when you see the following logs
56+
57+
```
58+
2022-12-01T23:40:05.998+0800 INFO grpc/client. go:90 Grpc Client initialized the TLSConfig configuration
59+
```

tls/README_zh.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 在Dubbo-go中使用TLS加密
2+
3+
## 使用方法
4+
0.生成所需要的证书和秘钥
5+
本示例提供已经生成好的证书和秘钥,在目录`tls/x509`
6+
7+
1.配置dubbogo.yaml
8+
9+
客户端TLS配置:
10+
11+
```yaml
12+
dubbo:
13+
tls_config:
14+
ca-cert-file: ../../../x509/server_ca_cert.pem
15+
tls-cert-file: ../../../x509/client2_cert.pem
16+
tls-key-file: ../../../x509/client2_key.pem
17+
tls-server-name: dubbogo.test.example.com
18+
```
19+
20+
服务端TLS配置:
21+
22+
```yaml
23+
dubbo:
24+
tls_config:
25+
ca-cert-file: ../../../x509/client_ca_cert.pem
26+
tls-cert-file: ../../../x509/server2_cert.pem
27+
tls-key-file: ../../../x509/server2_key.pem
28+
tls-server-name: dubbogo.test.example.com
29+
```
30+
2. 启动示例
31+
32+
本示例提供了Dubbo、Grpc、Triple三种通信方式的TLS加密示例,分别位于`tls/dubbo` 、`tls/grpc` 、`tls/triple`。进入文件夹即可启动示例。
33+
34+
以tls/dubbo为例:
35+
36+
启动服务端:
37+
38+
进入`tls/dubbo/go-server/cmd`,启动`server.go`
39+
40+
看到如下日志,则TLS配置生效
41+
42+
```
43+
2022-12-01T23:39:30.690+0800 INFO getty/getty_server.go:78 Getty Server initialized the TLSConfig configuration
44+
```
45+
46+
启动客户端:
47+
48+
进入`tls/dubbo/go-client/cmd`,启动`client.go`
49+
50+
看到如下日志,则TLS配置生效
51+
52+
```
53+
2022-12-01T23:40:05.998+0800 INFO grpc/client.go:90 Grpc Client initialized the TLSConfig configuration
54+
```

tls/dubbo/go-client/cmd/client.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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/config"
26+
_ "dubbo.apache.org/dubbo-go/v3/imports"
27+
"github.com/dubbogo/gost/log/logger"
28+
29+
hessian "github.com/apache/dubbo-go-hessian2"
30+
)
31+
32+
import (
33+
"github.com/apache/dubbo-go-samples/tls/dubbo/go-client/pkg"
34+
)
35+
36+
var (
37+
userProvider = &pkg.UserProvider{}
38+
)
39+
40+
// need to setup environment variable "DUBBO_GO_CONFIG_PATH" to "conf/dubbogo.yaml" before run
41+
func main() {
42+
hessian.RegisterJavaEnum(pkg.Gender(pkg.MAN))
43+
hessian.RegisterJavaEnum(pkg.Gender(pkg.WOMAN))
44+
hessian.RegisterPOJO(&pkg.User{})
45+
46+
config.SetConsumerService(userProvider)
47+
48+
err := config.Load()
49+
if err != nil {
50+
panic(err)
51+
}
52+
53+
logger.Infof("\n\ntest")
54+
test()
55+
}
56+
57+
func test() {
58+
logger.Infof("\n\n\nstart to test dubbo")
59+
reqUser := &pkg.User{}
60+
reqUser.ID = "003"
61+
user, err := userProvider.GetUser(context.TODO(), reqUser)
62+
if err != nil {
63+
panic(err)
64+
}
65+
logger.Infof("response result: %v", user)
66+
67+
logger.Infof("\n\n\nstart to test dubbo - enum")
68+
gender, err := userProvider.GetGender(context.TODO(), 1)
69+
if err != nil {
70+
panic(err)
71+
}
72+
logger.Infof("response result: %v", gender)
73+
74+
logger.Infof("\n\n\nstart to test dubbo - GetUser0")
75+
ret, err := userProvider.GetUser0("003", "Moorse")
76+
if err != nil {
77+
panic(err)
78+
}
79+
logger.Infof("response result: %v", ret)
80+
81+
logger.Infof("\n\n\nstart to test dubbo - GetUsers")
82+
ret1, err := userProvider.GetUsers([]string{"002", "003"})
83+
if err != nil {
84+
panic(err)
85+
}
86+
logger.Infof("response result: %v", ret1)
87+
88+
logger.Infof("\n\n\nstart to test dubbo - getUser")
89+
90+
var i int32 = 1
91+
user, err = userProvider.GetUser2(context.TODO(), i)
92+
if err != nil {
93+
panic(err)
94+
}
95+
logger.Infof("response result: %v", user)
96+
97+
logger.Infof("\n\n\nstart to test dubbo - getErr")
98+
reqUser.ID = "003"
99+
_, err = userProvider.GetErr(context.TODO(), reqUser)
100+
if err == nil {
101+
panic("err is nil")
102+
}
103+
logger.Infof("getErr - error: %v", err)
104+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# dubbo client yaml configure file
2+
3+
dubbo:
4+
consumer:
5+
references:
6+
UserProvider:
7+
url: dubbo://localhost:20000
8+
protocol: dubbo
9+
interface: org.apache.dubbo.sample.UserProvider
10+
logger:
11+
zap-config:
12+
level: info
13+
tls_config:
14+
ca-cert-file: ../../../x509/server_ca_cert.pem
15+
tls-cert-file: ../../../x509/client1_cert.pem
16+
tls-key-file: ../../../x509/client1_key.pem
17+
tls-server-name: dubbogo.test.example.com

tls/dubbo/go-client/pkg/user.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 pkg
19+
20+
import (
21+
"context"
22+
"fmt"
23+
"strconv"
24+
"time"
25+
)
26+
27+
import (
28+
hessian "github.com/apache/dubbo-go-hessian2"
29+
)
30+
31+
type Gender hessian.JavaEnum
32+
33+
const (
34+
MAN hessian.JavaEnum = iota
35+
WOMAN
36+
)
37+
38+
var genderName = map[hessian.JavaEnum]string{
39+
MAN: "MAN",
40+
WOMAN: "WOMAN",
41+
}
42+
43+
var genderValue = map[string]hessian.JavaEnum{
44+
"MAN": MAN,
45+
"WOMAN": WOMAN,
46+
}
47+
48+
func (g Gender) JavaClassName() string {
49+
return "org.apache.dubbo.sample.Gender"
50+
}
51+
52+
func (g Gender) String() string {
53+
s, ok := genderName[hessian.JavaEnum(g)]
54+
if ok {
55+
return s
56+
}
57+
58+
return strconv.Itoa(int(g))
59+
}
60+
61+
func (g Gender) EnumValue(s string) hessian.JavaEnum {
62+
v, ok := genderValue[s]
63+
if ok {
64+
return v
65+
}
66+
67+
return hessian.InvalidJavaEnum
68+
}
69+
70+
type User struct {
71+
// !!! Cannot define lowercase names of variable
72+
ID string `hessian:"id"`
73+
Name string
74+
Age int32
75+
Time time.Time
76+
Sex Gender // notice: java enum Object <--> go string
77+
}
78+
79+
func (u User) String() string {
80+
return fmt.Sprintf(
81+
"User{ID:%s, Name:%s, Age:%d, Time:%s, Sex:%s}",
82+
u.ID, u.Name, u.Age, u.Time, u.Sex,
83+
)
84+
}
85+
86+
func (u *User) JavaClassName() string {
87+
return "org.apache.dubbo.sample.User"
88+
}
89+
90+
type UserProvider struct {
91+
GetUsers func(req []string) ([]*User, error)
92+
GetErr func(ctx context.Context, req *User) (*User, error)
93+
94+
GetUser func(ctx context.Context, req *User) (*User, error)
95+
96+
GetUserNew func(ctx context.Context, req1, req2 *User) (*User, error)
97+
98+
GetUser0 func(id string, name string) (User, error)
99+
GetUser2 func(ctx context.Context, req int32) (*User, error) `dubbo:"getUser"`
100+
GetUser3 func() error
101+
GetGender func(ctx context.Context, i int32) (Gender, error)
102+
Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used
103+
}

0 commit comments

Comments
 (0)