File tree Expand file tree Collapse file tree 6 files changed +242
-0
lines changed Expand file tree Collapse file tree 6 files changed +242
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ ## Example Usage
3
+
4
+ Here's a minimal Go Micro service demonstrating the architecture:
5
+
6
+ ``` go
7
+ package main
8
+
9
+ import (
10
+ " go-micro.dev/v5"
11
+ " log"
12
+ )
13
+
14
+ func main () {
15
+ service := micro.NewService (
16
+ micro.Name (" example" ),
17
+ )
18
+ service.Init ()
19
+ if err := service.Run (); err != nil {
20
+ log.Fatal (err)
21
+ }
22
+ }
23
+ ```
1
24
## Architecture
2
25
3
26
An overview of the Go Micro architecture
Original file line number Diff line number Diff line change
1
+ # Broker
2
+
3
+ The broker provides pub/sub messaging for Go Micro services.
4
+
5
+ ## Features
6
+ - Publish messages to topics
7
+ - Subscribe to topics
8
+ - Multiple broker implementations
9
+
10
+ ## Implementations
11
+ Supported brokers include:
12
+ - Memory (default)
13
+ - NATS
14
+ - RabbitMQ
15
+
16
+ Configure the broker when creating your service as needed.
17
+
18
+ ## Example Usage
19
+
20
+ Here's how to use the broker in your Go Micro service:
21
+
22
+ ``` go
23
+ package main
24
+
25
+ import (
26
+ " go-micro.dev/v5"
27
+ " go-micro.dev/v5/broker"
28
+ " log"
29
+ )
30
+
31
+ func main () {
32
+ service := micro.NewService ()
33
+ service.Init ()
34
+
35
+ // Publish a message
36
+ if err := broker.Publish (" topic" , &broker.Message {Body: []byte (" hello world" )}); err != nil {
37
+ log.Fatal (err)
38
+ }
39
+
40
+ // Subscribe to a topic
41
+ _ , err := broker.Subscribe (" topic" , func (p broker.Event ) error {
42
+ log.Printf (" Received message: %s " , string (p.Message ().Body ))
43
+ return nil
44
+ })
45
+ if err != nil {
46
+ log.Fatal (err)
47
+ }
48
+
49
+ // Run the service
50
+ if err := service.Run (); err != nil {
51
+ log.Fatal (err)
52
+ }
53
+ }
54
+ ```
Original file line number Diff line number Diff line change
1
+ # Client/Server
2
+
3
+ Go Micro uses a client/server model for RPC communication between services.
4
+
5
+ ## Client
6
+ The client is used to make requests to other services.
7
+
8
+ ## Server
9
+ The server handles incoming requests.
10
+
11
+ Both client and server are pluggable and support middleware wrappers for additional functionality.
12
+
13
+ ## Example Usage
14
+
15
+ Here's how to define a simple handler and register it with a Go Micro server:
16
+
17
+ ``` go
18
+ package main
19
+
20
+ import (
21
+ " context"
22
+ " go-micro.dev/v5"
23
+ " log"
24
+ )
25
+
26
+ type Greeter struct {}
27
+
28
+ func (g *Greeter ) Hello (ctx context.Context, req *struct{}, rsp *struct {Msg string }) error {
29
+ rsp.Msg = " Hello, world!"
30
+ return nil
31
+ }
32
+
33
+ func main () {
34
+ service := micro.NewService (
35
+ micro.Name (" greeter" ),
36
+ )
37
+ service.Init ()
38
+ micro.RegisterHandler (service.Server (), new (Greeter))
39
+ if err := service.Run (); err != nil {
40
+ log.Fatal (err)
41
+ }
42
+ }
43
+ ```
Original file line number Diff line number Diff line change
1
+ # Registry
2
+
3
+ The registry is responsible for service discovery in Go Micro. It allows services to register themselves and discover other services.
4
+
5
+ ## Features
6
+ - Service registration and deregistration
7
+ - Service lookup
8
+ - Watch for changes
9
+
10
+ ## Implementations
11
+ Go Micro supports multiple registry backends, including:
12
+ - MDNS (default)
13
+ - Consul
14
+ - Etcd
15
+ - NATS
16
+
17
+ You can configure the registry when initializing your service.
18
+
19
+ ## Example Usage
20
+
21
+ Here's how to use a custom registry (e.g., Consul) in your Go Micro service:
22
+
23
+ ``` go
24
+ package main
25
+
26
+ import (
27
+ " go-micro.dev/v5"
28
+ " go-micro.dev/v5/registry/consul"
29
+ )
30
+
31
+ func main () {
32
+ reg := consul.NewRegistry ()
33
+ service := micro.NewService (
34
+ micro.Registry (reg),
35
+ )
36
+ service.Init ()
37
+ service.Run ()
38
+ }
39
+ ```
Original file line number Diff line number Diff line change
1
+ # Store
2
+
3
+ The store provides a pluggable interface for data storage in Go Micro.
4
+
5
+ ## Features
6
+ - Key-value storage
7
+ - Multiple backend support
8
+
9
+ ## Implementations
10
+ Supported stores include:
11
+ - Memory (default)
12
+ - File
13
+ - MySQL
14
+ - Redis
15
+
16
+ Configure the store as needed for your application.
17
+
18
+ ## Example Usage
19
+
20
+ Here's how to use the store in your Go Micro service:
21
+
22
+ ``` go
23
+ package main
24
+
25
+ import (
26
+ " go-micro.dev/v5"
27
+ " go-micro.dev/v5/store"
28
+ " log"
29
+ )
30
+
31
+ func main () {
32
+ service := micro.NewService ()
33
+ service.Init ()
34
+
35
+ // Write a record
36
+ if err := store.Write (&store.Record {Key: " foo" , Value: []byte (" bar" )}); err != nil {
37
+ log.Fatal (err)
38
+ }
39
+
40
+ // Read a record
41
+ recs , err := store.Read (" foo" )
42
+ if err != nil {
43
+ log.Fatal (err)
44
+ }
45
+ log.Printf (" Read value: %s " , string (recs[0 ].Value ))
46
+ }
47
+ ```
Original file line number Diff line number Diff line change
1
+ # Transport
2
+
3
+ The transport layer is responsible for communication between services.
4
+
5
+ ## Features
6
+ - Pluggable transport implementations
7
+ - Secure and efficient communication
8
+
9
+ ## Implementations
10
+ Supported transports include:
11
+ - TCP (default)
12
+ - gRPC
13
+
14
+ You can specify the transport when initializing your service.
15
+
16
+ ## Example Usage
17
+
18
+ Here's how to use a custom transport (e.g., gRPC) in your Go Micro service:
19
+
20
+ ``` go
21
+ package main
22
+
23
+ import (
24
+ " go-micro.dev/v5"
25
+ " go-micro.dev/v5/transport/grpc"
26
+ )
27
+
28
+ func main () {
29
+ t := grpc.NewTransport ()
30
+ service := micro.NewService (
31
+ micro.Transport (t),
32
+ )
33
+ service.Init ()
34
+ service.Run ()
35
+ }
36
+ ```
You can’t perform that action at this time.
0 commit comments