Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

micro api 使用 #3

Open
cheerego opened this issue Jun 17, 2019 · 0 comments
Open

micro api 使用 #3

cheerego opened this issue Jun 17, 2019 · 0 comments

Comments

@cheerego
Copy link
Owner

cheerego commented Jun 17, 2019

micro api 使用

main.go

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

请求

http

curl -H 'Content-Type: application/json' -d '{"name": "Asim"}' http://127.0.0.1:8080/greeter/hello

也可是使用postman 访问

http rpc

 curl -d 'service=go.micro.api.greeter' \
        -d 'method=Greeter.Hello' \
        -d 'request={"name": "Bob"}' \
        http://localhost:8080/rpc
{"greeting":"Hello Bob"}%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant