Skip to content

Commit 455a752

Browse files
author
pengjiang
committed
update
1 parent a964874 commit 455a752

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

README.md

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,86 @@
1-
### Quick Start
1+
### Background(背景)
22

3-
Prerequisites:
3+
在实际的项目中,我们以go-zero 作为gRpc 网关,由网关将请求路由至后端gRpc 微服务。
44

5-
Download the module:
5+
但面临的问题是: go-zero默认以 ETCD作为服务发现, 而nacos 作为服务发现需要自己去实现,文档在这块也是缺失的,参考的资料凤毛鳞角,给广大开发者带来困拢。
66

7-
```console
8-
go get -u github.com/zeromicro/zero-contrib/zrpc/registry/nacos
9-
```
7+
于是看到开源项目:
8+
9+
https://github.com/zeromicro/zero-contrib/tree/main/zrpc/registry
10+
11+
问题是开源项目只给出了 用户名/密码这种连接nacos的认证方式,并不支持 accessKey/secretKey这种方式。
12+
13+
而阿里云的accessKey/secretKey连接方式安全性较高,是推荐的一种连接方式。
14+
15+
故在开源项目https://github.com/zeromicro/zero-contrib/tree/main/zrpc/registry的基础上,我们作了修改
16+
17+
以布了新的服务。
18+
19+
## 如何引入?
1020

11-
For example:
21+
` _ "github.com/alanjiang/go-zero-rpc-nacos"`
1222

13-
## Service
23+
## 网关代码参见:
1424

1525
- main.go
1626

1727
```go
18-
import _ "github.com/zeromicro/zero-contrib/zrpc/registry/nacos"
28+
package main
29+
30+
import (
31+
"github.com/zeromicro/go-zero/core/logx"
32+
"net/http"
33+
"flag"
34+
"github.com/zeromicro/go-zero/core/conf"
35+
"github.com/zeromicro/go-zero/gateway"
36+
_ "github.com/alanjiang/go-zero-rpc-nacos"
37+
)
38+
39+
var configFile = flag.String("f", "etc/gateway.yaml", "config file")
40+
1941

2042
func main() {
2143
flag.Parse()
2244

23-
var c config.Config
45+
var c gateway.GatewayConf
2446
conf.MustLoad(*configFile, &c)
47+
gw := gateway.MustNewServer(c)
48+
defer gw.Stop()
2549

26-
server := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
27-
28-
})
29-
// register service to nacos
30-
sc := []constant.ServerConfig{
31-
*constant.NewServerConfig("192.168.100.15", 8848),
32-
}
33-
34-
cc := &constant.ClientConfig{
35-
NamespaceId: "public",
36-
TimeoutMs: 5000,
37-
NotLoadCacheAtStart: true,
38-
LogDir: "/tmp/nacos/log",
39-
CacheDir: "/tmp/nacos/cache",
40-
RotateTime: "1h",
41-
MaxAge: 3,
42-
LogLevel: "debug",
43-
}
44-
45-
opts := nacos.NewNacosConfig("nacos.rpc", c.ListenOn, sc, cc)
46-
_ = nacos.RegisterService(opts)
47-
server.Start()
50+
gw.Start()
4851
}
52+
53+
4954
```
5055

51-
## Client
56+
##
5257

53-
- main.go
5458

55-
```go
56-
import _ "github.com/zeromicro/zero-contrib/zrpc/registry/nacos"
57-
```
5859

59-
- etc/\*.yaml
60+
- etc/gateway.yaml
6061

6162
```yaml
62-
# nacos://[user:passwd]@host/service?param=value'
6363

64-
Target: nacos://192.168.100.15:8848/nacos.rpc?namespaceid=public&timeout=5000s
64+
Target: nacos://accessKey:secretKey@nacos服务器域名:8848/logistic.rpc?namespaceid=空间ID&timeout=13000ms
65+
Mappings:
66+
- Method: put
67+
Path: /logistic/query
68+
RpcPath: logistic.LogisticService/Query
69+
```
70+
71+
72+
73+
通过以上的配置就可实现查询 服务名为 :logistic.rpc 的快递服务。
74+
75+
注:网关不需要注册客户端。
76+
77+
引入 _ "github.com/alanjiang/go-zero-rpc-nacos" 会自动执行 builder.go 的 init 方法。
78+
79+
```
80+
func init() {
81+
82+
fmt.Print("----> nacos init <-----")
83+
resolver.Register(&builder{})
84+
}
6585
```
86+

0 commit comments

Comments
 (0)