Skip to content

Commit c79cf96

Browse files
committed
Merge remote-tracking branch 'origin/master' into events
2 parents e784106 + 11b7eb0 commit c79cf96

File tree

5 files changed

+355
-0
lines changed

5 files changed

+355
-0
lines changed

.github/workflows/docs.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Sample workflow for building and deploying a Jekyll site to GitHub Pages
2+
name: Deploy Jekyll with GitHub Pages dependencies preinstalled
3+
4+
on:
5+
# Runs on pushes targeting the default branch
6+
push:
7+
branches: ["master"]
8+
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
11+
12+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
13+
permissions:
14+
contents: read
15+
pages: write
16+
id-token: write
17+
18+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
19+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
20+
concurrency:
21+
group: "pages"
22+
cancel-in-progress: false
23+
24+
jobs:
25+
# Build job
26+
build:
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v4
31+
- name: Setup Pages
32+
uses: actions/configure-pages@v5
33+
- name: Build with Jekyll
34+
uses: actions/jekyll-build-pages@v1
35+
with:
36+
source: ./internal/docs
37+
destination: ./_site
38+
- name: Upload artifact
39+
uses: actions/upload-pages-artifact@v3
40+
41+
# Deployment job
42+
deploy:
43+
environment:
44+
name: github-pages
45+
url: ${{ steps.deployment.outputs.page_url }}
46+
runs-on: ubuntu-latest
47+
needs: build
48+
steps:
49+
- name: Deploy to GitHub Pages
50+
id: deployment
51+
uses: actions/deploy-pages@v4

internal/docs/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Docs
2+
3+
Documentation for the Go Micro framework
4+
5+
## Overview
6+
7+
Go Micro is a framework for microservices development.
8+
It's built on a powerful pluggable architecture using
9+
Go interfaces. Go Micro defines the foundations for
10+
distributed systems development which includes
11+
service discovery, client/server rpc and pubsub.
12+
Additionally Go Micro contains other primitives
13+
such as auth, caching and storage. All of this
14+
is encapsulated in a high level service interface.
15+
16+
## Learn More
17+
18+
To get started follow the getting started guide.
19+
Otherwise continue to read the docs for more information
20+
about the framework.
21+
22+
## Contents
23+
24+
- [Getting Started](getting-started)
25+
- [Architecture](architecture)
26+
- Registry
27+
- Broker
28+
- Client/Server
29+
- Transport
30+
- Store

internal/docs/_config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
title: Docs
2+
description: "A Go microservices framework"
3+
baseurl: "/docs" # the subpath of your site, e.g. /blog
4+
url: "" # the base hostname & protocol for your site, e.g. http://example.com
5+
6+
theme: jekyll-theme-primer

internal/docs/architecture.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Architecture
2+
3+
An overview of the Go Micro architecture
4+
5+
## Overview
6+
7+
Go Micro abstracts away the details of distributed systems. Here are the main features.
8+
9+
- **Authentication** - Auth is built in as a first class citizen. Authentication and authorization enable secure
10+
zero trust networking by providing every service an identity and certificates. This additionally includes rule
11+
based access control.
12+
13+
- **Dynamic Config** - Load and hot reload dynamic config from anywhere. The config interface provides a way to load application
14+
level config from any source such as env vars, file, etcd. You can merge the sources and even define fallbacks.
15+
16+
- **Data Storage** - A simple data store interface to read, write and delete records. It includes support for many storage backends
17+
in the plugins repo. State and persistence becomes a core requirement beyond prototyping and Micro looks to build that into the framework.
18+
19+
- **Service Discovery** - Automatic service registration and name resolution. Service discovery is at the core of micro service
20+
development. When service A needs to speak to service B it needs the location of that service. The default discovery mechanism is
21+
multicast DNS (mdns), a zeroconf system.
22+
23+
- **Load Balancing** - Client side load balancing built on service discovery. Once we have the addresses of any number of instances
24+
of a service we now need a way to decide which node to route to. We use random hashed load balancing to provide even distribution
25+
across the services and retry a different node if there's a problem.
26+
27+
- **Message Encoding** - Dynamic message encoding based on content-type. The client and server will use codecs along with content-type
28+
to seamlessly encode and decode Go types for you. Any variety of messages could be encoded and sent from different clients. The client
29+
and server handle this by default. This includes protobuf and json by default.
30+
31+
- **RPC Client/Server** - RPC based request/response with support for bidirectional streaming. We provide an abstraction for synchronous
32+
communication. A request made to a service will be automatically resolved, load balanced, dialled and streamed.
33+
34+
- **Async Messaging** - PubSub is built in as a first class citizen for asynchronous communication and event driven architectures.
35+
Event notifications are a core pattern in micro service development. The default messaging system is a HTTP event message broker.
36+
37+
- **Pluggable Interfaces** - Go Micro makes use of Go interfaces for each distributed system abstraction. Because of this these interfaces
38+
are pluggable and allows Go Micro to be runtime agnostic. You can plugin any underlying technology.
39+
40+
## Design
41+
42+
We will share more on architecture soon

internal/docs/getting-started.md

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
# Getting Started
2+
3+
To make use of Go Micro
4+
5+
```golang
6+
go get "go-micro.dev/v5"
7+
```
8+
9+
## Create a service
10+
11+
This is a basic example of how you'd create a service and register a handler in pure Go.
12+
13+
```
14+
mkdir helloworld
15+
cd helloworld
16+
go mod init
17+
go get go-micro.dev/v5@latest
18+
```
19+
20+
Write the following into `main.go`
21+
22+
```golang
23+
package main
24+
25+
import (
26+
"go-micro.dev/v5"
27+
)
28+
29+
type Request struct {
30+
Name string `json:"name"`
31+
}
32+
33+
type Response struct {
34+
Message string `json:"message"`
35+
}
36+
37+
type Say struct{}
38+
39+
func (h *Say) Hello(ctx context.Context, req *Request, rsp *Response) error {
40+
rsp.Message = "Hello " + req.Name
41+
return nil
42+
}
43+
44+
func main() {
45+
// create the service
46+
service := micro.New("helloworld")
47+
48+
// initialise service
49+
service.Init()
50+
51+
// register handler
52+
service.Handle(new(Say))
53+
54+
// run the service
55+
service.Run()
56+
}
57+
```
58+
59+
Now run the service
60+
61+
```
62+
go run main.go
63+
```
64+
65+
Take a note of the address with the log line
66+
67+
```
68+
Transport [http] Listening on [::]:35823
69+
```
70+
71+
Now you can call the service
72+
73+
```
74+
curl -XPOST \
75+
-H 'Content-Type: application/json' \
76+
-H 'Micro-Endpoint: Say.Hello' \
77+
-d '{"name": "alice"}' \
78+
http://localhost:35823
79+
```
80+
81+
## Set a fixed address
82+
83+
To set a fixed address by specifying it as an option to service, note the change from `New` to `NewService`
84+
85+
```golang
86+
service := micro.NewService(
87+
micro.Name("helloworld"),
88+
micro.Address(":8080"),
89+
)
90+
```
91+
92+
Alternatively use `MICRO_SERVER_ADDRESS=:8080` as an env var
93+
94+
```
95+
curl -XPOST \
96+
-H 'Content-Type: application/json' \
97+
-H 'Micro-Endpoint: Say.Hello' \
98+
-d '{"name": "alice"}' \
99+
http://localhost:8080
100+
```
101+
102+
## Protobuf
103+
104+
If you want to define services with protobuf you can use [protoc-gen-micro](https://github.com/micro/micro/tree/master/cmd/protoc-gen-micro)
105+
106+
```
107+
cd helloworld
108+
mkdir proto
109+
```
110+
111+
Edit a file `proto/helloworld.proto`
112+
113+
```
114+
syntax = "proto3";
115+
116+
package greeter;
117+
option go_package = "/proto;helloworld";
118+
119+
service Say {
120+
rpc Hello(Request) returns (Response) {}
121+
}
122+
123+
message Request {
124+
string name = 1;
125+
}
126+
127+
message Response {
128+
string message = 1;
129+
}
130+
```
131+
132+
You can now generate a client/server like so
133+
134+
```
135+
protoc --proto_path=. --micro_out=. --go_out=. helloworld.proto
136+
```
137+
138+
In your `main.go` update the code to reference the generated code
139+
140+
```
141+
package main
142+
143+
import (
144+
"go-micro.dev/v5"
145+
146+
pb "github.com/micro/helloworld/proto"
147+
)
148+
149+
type Say struct{}
150+
151+
func (h *Say) Hello(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
152+
rsp.Message = "Hello " + req.Name
153+
return nil
154+
}
155+
156+
func main() {
157+
// create the service
158+
service := micro.New("helloworld")
159+
160+
// initialise service
161+
service.Init()
162+
163+
// register handler
164+
proto.RegisterSayHandler(service.Server(), &Say{})
165+
166+
// run the service
167+
service.Run()
168+
}
169+
```
170+
171+
Now I can run this again
172+
173+
```
174+
go run main.go
175+
```
176+
177+
## Call via a client
178+
179+
The generated code provides us a client
180+
181+
```
182+
package main
183+
184+
import (
185+
"context"
186+
"fmt"
187+
188+
"go-micro.dev/v5"
189+
pb "github.com/micro/helloworld/proto"
190+
)
191+
192+
package main() {
193+
service := micro.New("helloworld)
194+
service.Init()
195+
196+
say := pb.NewSayService("helloworld", service.Client())
197+
198+
rsp, err := say.Hello(context.TODO(), &pb.Request{
199+
Name: "John",
200+
})
201+
if err != nil {
202+
fmt.Println(err)
203+
return
204+
}
205+
206+
fmt.Println(rsp.Message)
207+
}
208+
```
209+
210+
## Command line
211+
212+
If you'd like to use a command line checkout [Micro](https://github.com/micro/micro)
213+
214+
```
215+
go get github.com/micro/micro/v5@latest
216+
```
217+
218+
```
219+
micro call helloworld Say.Hello '{"name": "John"}'
220+
```
221+
222+
Alternative using the dynamic CLI commands
223+
224+
```
225+
micro helloworld say hello --name="John"
226+
```

0 commit comments

Comments
 (0)