Giter VIP home page Giter VIP logo

go-micro-in-action's People

Contributors

cheerego avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

flyysr

go-micro-in-action's Issues

go-micro 使用 consul 服务发现

go-micro 使用 consul 服务发现

运行本地 consul

consul agent -dev

修改 main.go

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

go-micro入门实战(一)

go-micro 入门实战第一篇

目录结构

├── 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

greeter.proto

syntax = "proto3";

service Greeter {
    rpc Hello(HelloRequest) returns (HelloResponse) {}
}

message HelloRequest {
    string name = 1;
}

message HelloResponse {
    string greeting = 2;
}

gen

protoc --micro_out=. --go_out=. ./proto/greeter.proto

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("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 get 

运行server

go run main.go

client/client.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)
}

运行Client

go run client/client.go

micro cli

//非项目目录
go get github.com/micro/micro

➜  micro list services                  
greeter

micro api 使用

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"}%

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.