|
1 |
| -<div> |
2 |
| - < img src= "https://raw.githubusercontent.com/go-spring/go-spring/master/[email protected]" width= "140" height= "*" alt= "logo"/> |
3 |
| - <br/> |
4 |
| - <img src="https://img.shields.io/github/license/go-spring/spring-core" alt="license"/> |
5 |
| - <img src="https://img.shields.io/github/go-mod/go-version/go-spring/spring-core" alt="go-version"/> |
6 |
| - <img src="https://img.shields.io/github/v/release/go-spring/spring-core?include_prereleases" alt="release"/> |
7 |
| - <img src="https://codecov.io/gh/go-spring/spring-core/branch/main/graph/badge.svg" alt="test-coverage"/> |
8 |
| -</div> |
9 |
| - |
10 |
| -# 介绍 |
11 |
| - |
12 |
| -Go-Spring 是为 Go 开发者打造的轻量级微服务框架,灵感来源于 Java 的 Spring 和 Spring Boot。 |
13 |
| -它旨在降低开发门槛、提高项目结构的清晰度和可维护性。主要特点包括: |
14 |
| - |
15 |
| -- 秒启动:借助 init 机制主动注册 bean,做到容器秒启动。 |
16 |
| -- 易用性:通过注解标签和链式调用注册 Bean 与配置,降低手写样板代码。 |
17 |
| -- 扩展性:支持动态刷新属性和 Bean,在运行时调整配置而无需重启应用。 |
18 |
| -- 微服务支持:内置启动框架及丰富的扩展接口,可快速构建多种微服务应用。 |
19 |
| -- 测试友好:提供丰富的单元测试工具和 mock 能力,保证代码质量。 |
20 |
| -- 启动阶段反射:仅在启动阶段借助反射完成 bean 的注入过程,运行时无反射。 |
21 |
| -- 零侵入:对业务代码零侵入,bean 生命周期管理不要求必须实现某个接口。 |
22 |
| - |
23 |
| -# 快速开始 |
24 |
| - |
25 |
| -### 安装 |
26 |
| - |
27 |
| -通过 Go Modules 获取最新版本: |
28 |
| - |
29 |
| -``` |
30 |
| -go get github.com/go-spring/spring-core@develop |
31 |
| -``` |
32 |
| - |
33 |
| -### 最小示例 |
34 |
| - |
35 |
| -下面是一个最简单的示例,展示了如何注册一个 Bean、绑定属性、动态属性以及启动应用: |
36 |
| - |
37 |
| -```go |
38 |
| -package main |
39 |
| - |
40 |
| -import ( |
41 |
| - "fmt" |
42 |
| - "net/http" |
43 |
| - "time" |
44 |
| - |
45 |
| - "github.com/go-spring/spring-core/gs" |
46 |
| - "github.com/go-spring/spring-core/util/sysconf" |
47 |
| - "github.com/go-spring/spring-core/util/syslog" |
48 |
| -) |
49 |
| - |
50 |
| -func init() { |
51 |
| - // Register the Service struct as a bean. |
52 |
| - gs.Object(&Service{}) |
53 |
| - |
54 |
| - // Provide a [*http.ServeMux] as a bean. |
55 |
| - gs.Provide(func(s *Service) *http.ServeMux { |
56 |
| - http.HandleFunc("/echo", s.Echo) |
57 |
| - http.HandleFunc("/refresh", s.Refresh) |
58 |
| - return http.DefaultServeMux |
59 |
| - }) |
60 |
| -} |
61 |
| - |
62 |
| -const timeLayout = "2006-01-02 15:04:05.999 -0700 MST" |
63 |
| - |
64 |
| -type Service struct { |
65 |
| - StartTime time.Time `value:"${start-time}"` |
66 |
| - RefreshTime gs.Dync[time.Time] `value:"${refresh-time}"` |
67 |
| -} |
68 |
| - |
69 |
| -func (s *Service) Echo(w http.ResponseWriter, r *http.Request) { |
70 |
| - str := fmt.Sprintf("start-time: %s refresh-time: %s", |
71 |
| - s.StartTime.Format(timeLayout), |
72 |
| - s.RefreshTime.Value().Format(timeLayout)) |
73 |
| - _, _ = w.Write([]byte(str)) |
74 |
| -} |
75 |
| - |
76 |
| -func (s *Service) Refresh(w http.ResponseWriter, r *http.Request) { |
77 |
| - _ = sysconf.Set("refresh-time", time.Now().Format(timeLayout)) |
78 |
| - _ = gs.RefreshProperties() |
79 |
| - _, _ = w.Write([]byte("OK!")) |
80 |
| -} |
81 |
| - |
82 |
| -func main() { |
83 |
| - _ = sysconf.Set("start-time", time.Now().Format(timeLayout)) |
84 |
| - _ = sysconf.Set("refresh-time", time.Now().Format(timeLayout)) |
85 |
| - |
86 |
| - // Start the Go-Spring application. If it fails, log the error. |
87 |
| - if err := gs.Run(); err != nil { |
88 |
| - syslog.Errorf("app run failed: %s", err.Error()) |
89 |
| - } |
90 |
| -} |
91 |
| -``` |
92 |
| - |
93 |
| -当你运行这个程序时,它将启动一个 HTTP 服务器,并注册两个处理器:一个处理 "/echo" 请求, |
94 |
| -返回当前时间和刷新时间;另一个处理 "/refresh" 请求,用于刷新配置并返回 "OK!"。 |
95 |
| - |
96 |
| -运行这个程序,你可以访问 "/echo" 和 "/refresh",并观察到它们返回的当前时间和刷新时间。 |
97 |
| - |
98 |
| -```shell |
99 |
| -➜ ~ curl http://127.0.0.1:9090/echo |
100 |
| -start-time: 2025-03-14 13:32:51.608 +0800 CST refresh-time: 2025-03-14 13:32:51.608 +0800 CST% |
101 |
| -➜ ~ curl http://127.0.0.1:9090/refresh |
102 |
| -OK!% |
103 |
| -➜ ~ curl http://127.0.0.1:9090/echo |
104 |
| -start-time: 2025-03-14 13:32:51.608 +0800 CST refresh-time: 2025-03-14 13:33:02.936 +0800 CST% |
105 |
| -➜ ~ curl http://127.0.0.1:9090/refresh |
106 |
| -OK!% |
107 |
| -➜ ~ curl http://127.0.0.1:9090/echo |
108 |
| -start-time: 2025-03-14 13:32:51.608 +0800 CST refresh-time: 2025-03-14 13:33:08.88 +0800 CST% |
109 |
| -``` |
0 commit comments