cheerego / go-micro-in-action Goto Github PK
View Code? Open in Web Editor NEWgo micro 实战
go micro 实战
consul agent -dev
package main
import (
"context"
"fmt"
"github.com/micro/go-micro"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/consul"
proto "watching/proto"
)
type Greeter struct{}
func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error {
rsp.Greeting = "Hello " + req.Name
return nil
}
func main() {
// Create a new service. Optionally include some options here.
service := micro.NewService(
micro.Name("greeter"),
micro.Registry(consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{
"127.0.0.1:8500",
}
})),
)
// Init will parse the command line flags.
service.Init()
// Register handler
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
// Run the server
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
MICRO_REGISTRY=consul go run main.go
go run main.go --registry=consul
├── client
│ └── client.go
├── go.mod
├── go.sum
├── main.go
└── proto
├── greeter.micro.go 生成文件
├── greeter.pb.go 生成文件
└── greeter.proto
brew install consul
brew install protobuf
// 非项目目录执行
go get github.com/micro/protoc-gen-micro
go get github.com/golang/protobuf/protoc-gen-go
syntax = "proto3";
service Greeter {
rpc Hello(HelloRequest) returns (HelloResponse) {}
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string greeting = 2;
}
protoc --micro_out=. --go_out=. ./proto/greeter.proto
package main
import (
"context"
"fmt"
"github.com/micro/go-micro"
proto "watching/proto"
)
type Greeter struct{}
func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error {
rsp.Greeting = "Hello " + req.Name
return nil
}
func main() {
// Create a new service. Optionally include some options here.
service := micro.NewService(
micro.Name("greeter"),
)
// Init will parse the command line flags.
service.Init()
// Register handler
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
// Run the server
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
go get
go run main.go
package main
import (
"context"
"fmt"
"github.com/micro/go-micro"
proto "watching/proto"
)
func main() {
// Create a new service. Optionally include some options here.
service := micro.NewService(micro.Name("greeter.client"))
service.Init()
// Create new greeter client
greeter := proto.NewGreeterService("greeter", service.Client())
// Call the greeter
rsp, err := greeter.Hello(context.TODO(), &proto.HelloRequest{Name: "John"})
if err != nil {
fmt.Println(err)
}
// Print response
fmt.Println(rsp.Greeting)
}
go run client/client.go
//非项目目录
go get github.com/micro/micro
➜ micro list services
greeter
package main
import (
"context"
"fmt"
"github.com/micro/go-micro"
proto "watching/proto"
)
type Greeter struct{}
func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error {
rsp.Greeting = "Hello " + req.Name
return nil
}
func main() {
// Create a new service. Optionally include some options here.
service := micro.NewService(
micro.Name("go.micro.api.greeter"),
//micro.Registry(consul.NewRegistry(func(options *registry.Options) {
// options.Addrs = []string{
// "127.0.0.1:8500",
// }
//})),
)
// Init will parse the command line flags.
service.Init()
// Register handler
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
// Run the server
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
直接使用默认的服务发现 mdns
go run main.go
micro api
curl -H 'Content-Type: application/json' -d '{"name": "Asim"}' http://127.0.0.1:8080/greeter/hello
也可是使用postman 访问
curl -d 'service=go.micro.api.greeter' \
-d 'method=Greeter.Hello' \
-d 'request={"name": "Bob"}' \
http://localhost:8080/rpc
{"greeting":"Hello Bob"}%
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.