@@ -7,30 +7,20 @@ next: ./quick-start/
7
7
<img src =" ../img/logo.png " width = " 300 " alt =" logo " />
8
8
9
9
## Gone是什么
10
- 首先,Gone是Golang的一个轻量级的** 依赖注入框架** ,目前依赖注入的装配流程是通过反射来实现的;虽然golang的反射一直被人诟病太慢,但是在Gone中依赖注入只是程序的启动阶段,不影响运行阶段的速度,最多就是给启动过程增加几毫秒的时间,带来的好处却是不再需关心各种依赖如何创建 。
10
+ 首先,Gone是Golang的一个轻量级的** 依赖注入框架** ,关于为什么需要依赖注入,我之前写了一篇文章如果感兴趣可以看看: [ 为什么我们需要依赖注入? - “拒绝喝牛奶从种草养牛开始” ] ( https://blog.csdn.net/waitdeng/article/details/138972583 ) 。
11
11
12
- 其次,为了方便用户能够快速上手,我们开发了一系列内置组件 ,比如 xorm、redis、schedule、tracer、logrus、 gin、cmux、zap、viper 等等,后续我们还会支持更多微服务中间件的接入;用户可以利用这些组件快速开发出一个云原生的微服务,所以Gone ** 一套微服务解决方案 ** 。
12
+ 其次,基于 ** Gone ** 的依赖注入机制,我们开发了一系列组件 ,比如` goner/config ` 和 ` goner/viper ` 可以用于读取配置文件, ` goner/ xorm` 和 ` goner/gorm ` 可以用于访问数据库, ` goner/ gin` 可以用于提供web服务, ` goner/grpc ` 和 ` goner/urllib ` 可以用于调用RPC服务, ` goner/logrus ` 和 ` goner/zap ` 可以用于日志输出...
13
13
14
+ 最后,为了减少模式代码编写、测试、编译、运行,我们开发了一个命令行工具:[ gonectr] ( https://github.com/gone-io/gonectr ) 。
14
15
15
16
## 特性
16
- - 定义Goner接口,对依赖进行抽象
17
- - 依赖注入
18
- - 注入Goners
19
- - 注入函数参数
20
- - 模块化,可拆卸设计
21
- - 启动流程控制
22
- - 测试支持
23
- - 内置组件
24
- - goner/config,支持配置参数的依赖注入
25
- - goner/tracer,给调用链路增加TraceId,支持链路追踪
26
- - goner/logrus、goner/zap,支持日志记录
27
- - goner/gin,集成gin框架,提供HTTP请求参数的依赖注入
28
- - goner/viper,用于解析多种配置文件
29
- - ...
17
+ - 依赖注入,支持对 ** 结构体属性** 和 ** 函数参数** 的自动注入
18
+ - Gonectr,生成项目、生成辅助代码、编译和启动项目
19
+ - 单元测试方案,基于接口的mock测试
20
+ - 多种组件,可插拔,支持云原生、微服务
21
+ <img src =" ../img/architecture.png " width = " 600 " alt =" architecture " />
30
22
31
-
32
-
33
- ## 小试牛刀
23
+ ## 例子
34
24
35
25
下面使用** Gone** 来编写一个简单的Web服务,更多例子在[ 快速开始] ( https://goner.fun/zh/quick-start/ ) 和 [ example] ( https://github.com/gone-io/gone/tree/main/example ) 。
36
26
@@ -45,121 +35,52 @@ import (
45
35
46
36
// 实现一个Goner,什么是Goner? => https://goner.fun/zh/guide/core-concept.html#goner-%E9%80%9D%E8%80%85
47
37
type controller struct {
48
- gone.Flag // goner 标记,匿名嵌入后,一个结构体就实现了Goner
49
- gone.RouteGroup ` gone:"gone-gin-router "` // 注入根路由
38
+ gone.Flag // goner 标记,匿名嵌入后,一个结构体就实现了Goner
39
+ gone.RouteGroup ` gone:"* "` // 注入根路由
50
40
}
51
41
52
- // 实现 Mount 方法,挂载路由 ;框架会自动执行该方法
42
+ // Mount 用于挂载路由 ;框架会自动执行该方法
53
43
func (ctr *controller ) Mount () gone .GinMountError {
54
-
55
44
// 定义请求结构体
56
45
type Req struct {
57
46
Msg string ` json:"msg"`
58
47
}
59
48
60
49
// 注册 `POST /hello` 的 处理函数
61
- ctr.POST (" /hello" , func (in struct {
62
- to string ` gone:"http,query"` // 注入http请求Query参数To
63
- req *Req ` gone:"http,body"` // 注入http请求Body
64
- }) any {
65
- return fmt.Sprintf (" to %s msg is: %s " , in.to , in.req .Msg )
66
- })
50
+ ctr.
51
+ POST (" /hello" , func (in struct {
52
+ to string ` gone:"http,query"` // 注入http请求Query参数To
53
+ req *Req ` gone:"http,body"` // 注入http请求Body
54
+ }) any {
55
+ return fmt.Sprintf (" to %s msg is: %s " , in.to , in.req .Msg )
56
+ }).
57
+ GET (" /hello" , func () string {
58
+ return " hello"
59
+ })
67
60
68
61
return nil
69
62
}
70
63
71
64
func main () {
72
- // 启动服务
73
- gone.Serve (func (cemetery gone.Cemetery ) error {
74
- // 调用框架内置组件,加载gin框架
75
- _ = goner.GinPriest (cemetery)
76
-
77
- // 将 一个controller类型的Goner注册到Gone
78
- // 关于Bury => https://goner.fun/zh/guide/core-concept.html#bury-%E5%9F%8B%E8%91%AC
79
- // 关于cemetery => https://goner.fun/zh/guide/core-concept.html#cemetery-%E5%A2%93%E5%9B%AD
80
- cemetery.Bury (&controller{})
81
- return nil
82
- })
65
+ gone.
66
+ Default.
67
+ LoadPriest (goner.GinPriest ). // 加载 `goner/gin` 用于提供web服务
68
+ Load (&controller{}). // 加载我们前面定义的controller
69
+ Serve () // 启动服务
83
70
}
84
71
```
85
72
86
73
运行上面代码:` go run main.go ` ,程序将监听` 8080 ` 端口,使用curl测试:
87
74
``` bash
88
- curl -X POST ' http://localhost:8080/hello' \
89
- -H ' Content-Type: application/json' \
90
- --data-raw ' {"msg": "你好呀?"}'
91
- ```
92
- 结果如下:
93
- ``` bash
94
- {" code" :0," data" :" to msg is: 你好呀?" }
95
- ```
96
-
97
- ## 概念与启动流程
98
- ### 人话版本
99
- > 代码写完了,跑得起来才行。
75
+ curl -X POST ' http://localhost:8080/hello?to=Tom' -H ' Content-Type: application/json' --data-raw ' {"msg": "hello"}'
100
76
101
- 在Gone框架中,组件叫住Goner,Goner的属性可以是:interface、slice、map,程序启动时他们将被自动装配。在Gone启动前,需要将所有的Goners注册到Gone框架,启动时Gone会对所有Goners的属性进行自动装配,完成依赖注入,编写组件不必关心依赖的实现和依赖的接口是如何来的,只需要直接使用就可以了。
77
+ # 结果:{"code":0,"data":"to Tom msg is: hello"}
102
78
103
- ### 鬼话版本
104
- > 我们编写的代码终究只是死物,除非他们被运行起来。
105
-
106
- 在Gone中,组件被抽象为 Goner(逝者),Goner 属性可以注入其他的 Goner 。Gone启动前,需要将所有 Goners 注册 到 墓园;Gone启动后,会 复活 所有 Goners,建立一个 天国,“天国的所有人都不再残缺,他们想要的必定得到满足”。
107
-
108
-
109
- ### 代码版本
110
- ``` go
111
- package main
112
-
113
- import (
114
- " fmt"
115
- " github.com/gone-io/gone"
116
- )
117
-
118
- // Worker 接口
119
- type Worker interface {
120
- Do ()
121
- }
122
- type Boss struct {
123
- gone.Flag
124
- workers []Worker ` gone:"*"` // Boss 依赖 Worker接口,通过 gone标签标记为需要依赖注入
125
- }
126
-
127
- func (b *Boss ) Do () {
128
- for _ , w := range b.workers {
129
- w.Do ()
130
- }
131
- }
132
-
133
- // WorkerX 实现 Worker 接口
134
- type WorkerX struct {
135
- gone.Flag
136
- }
137
-
138
- func (w *WorkerX ) Do () {
139
- fmt.Println (" WorkerX Do" )
140
- }
141
-
142
- func main () {
143
- gone.
144
- // Goner启动前的准备工作
145
- Prepare (func (cemetery gone.Cemetery ) error {
146
- cemetery.
147
- // 注册Boss
148
- Bury (&Boss{}).
149
-
150
- // 注册多个Worker Goners
151
- Bury (&WorkerX{}).
152
- Bury (&WorkerX{}).
153
- Bury (&WorkerX{})
154
- return nil
155
- }).
156
- // Goner启动
157
- Run (func (boss *Boss) { // Run 方法中的参数被自动注入
158
- boss.Do ()
159
- })
160
- }
79
+ curl ' http://localhost:8080/hello'
80
+ # 结果: {"code":0,"data":"hello"}
161
81
```
162
82
83
+
163
84
了解更多,请阅读 [ 开发指南] ( https://goner.fun/zh/guide/ )
164
85
165
86
## 关于Logo
0 commit comments