mirror of
https://github.com/go-micro/go-micro.git
synced 2025-08-04 21:42:57 +02:00
Update README.md (#2750)
This commit is contained in:
52
README.md
52
README.md
@ -51,9 +51,15 @@ To make use of Go Micro import it
|
|||||||
import "go-micro.dev/v5"
|
import "go-micro.dev/v5"
|
||||||
```
|
```
|
||||||
|
|
||||||
Define a handler (protobuf is supported via [protoc-gen-micro](https://github.com/micro/go-micro/tree/master/cmd/protoc-gen-micro))
|
Create a service and register a handler
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go-micro.dev/v5"
|
||||||
|
)
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
@ -62,27 +68,20 @@ type Response struct {
|
|||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Helloworld struct{}
|
type Say struct{}
|
||||||
|
|
||||||
func (h *Helloworld) Greeting(ctx context.Context, req *Request, rsp *Response) error {
|
func (h *Say) Hello(ctx context.Context, req *Request, rsp *Response) error {
|
||||||
rsp.Message = "Hello " + req.Name
|
rsp.Message = "Hello " + req.Name
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
```
|
|
||||||
|
|
||||||
Create, initialise and run the service
|
// create the service
|
||||||
|
|
||||||
```golang
|
|
||||||
// create a new service
|
|
||||||
service := micro.New("helloworld")
|
service := micro.New("helloworld")
|
||||||
|
|
||||||
// register handler
|
// register handler
|
||||||
service.Handle(new(Helloworld))
|
service.Handle(new(Say))
|
||||||
|
|
||||||
// initialise flags
|
// run the service
|
||||||
service.Init()
|
|
||||||
|
|
||||||
// start the service
|
|
||||||
service.Run()
|
service.Run()
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -90,7 +89,6 @@ Optionally set fixed address
|
|||||||
|
|
||||||
```golang
|
```golang
|
||||||
service := micro.NewService(
|
service := micro.NewService(
|
||||||
// set address
|
|
||||||
micro.Address(":8080"),
|
micro.Address(":8080"),
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
@ -100,11 +98,35 @@ Call it via curl
|
|||||||
```
|
```
|
||||||
curl -XPOST \
|
curl -XPOST \
|
||||||
-H 'Content-Type: application/json' \
|
-H 'Content-Type: application/json' \
|
||||||
-H 'Micro-Endpoint: Helloworld.Greeting' \
|
-H 'Micro-Endpoint: Say.Hello' \
|
||||||
-d '{"name": "alice"}' \
|
-d '{"name": "alice"}' \
|
||||||
http://localhost:8080
|
http://localhost:8080
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Micro
|
||||||
|
|
||||||
|
Use the [Micro](https://github.com/micro/micro) cli to call it
|
||||||
|
|
||||||
|
```
|
||||||
|
micro call helloworld Say.Hello '{"name": "Asim"}'
|
||||||
|
```
|
||||||
|
|
||||||
|
Run the micro api and call it via the http
|
||||||
|
|
||||||
|
```
|
||||||
|
micro api
|
||||||
|
```
|
||||||
|
|
||||||
|
Call it via http://localhost:8080/
|
||||||
|
|
||||||
|
```
|
||||||
|
curl -d '{"name": "Asim"}' https://localhost:8080/helloworld/Say/Hello
|
||||||
|
```
|
||||||
|
|
||||||
|
## Protobuf
|
||||||
|
|
||||||
|
Protobuf is supported via [protoc-gen-micro](https://github.com/micro/go-micro/tree/master/cmd/protoc-gen-micro)
|
||||||
|
|
||||||
## Plugins
|
## Plugins
|
||||||
|
|
||||||
See [Plugins](https://github.com/micro/plugins) for implementations of the various core interfaces.
|
See [Plugins](https://github.com/micro/plugins) for implementations of the various core interfaces.
|
||||||
|
Reference in New Issue
Block a user