go-micro examples 中noproto 代碼學習(go-micro 中微服務使用json格式 來傳輸)

對應 examples/noproto 例子:

go- micro 中微服務 都可以使用哪些 傳輸協議?

go -micro 中 微服務中 我不想使用 protobuf 協議,或者在微服務中,我就想使用json 格式來傳輸 編碼?

目錄:

main.go 代碼如下:

package main

import (
	"context"
	"github.com/micro/go-micro"
)

type Greeter struct{}

func (g *Greeter) Hello(ctx context.Context, name *string, msg *string) error {
	*msg = "Hello " + *name
	return nil
}

func main() {
	// create new service
	service := micro.NewService(
		micro.Name("greeter"),
	)

	// initialise command line
	service.Init()

	// set the handler
	micro.RegisterHandler(service.Server(), new(Greeter))

	// run service
	service.Run()
}

client/main.go 代碼如下:

package main

import (
	"context"
	"fmt"
	"github.com/micro/go-micro"
	"github.com/micro/go-micro/client"
)

func main() {
	service := micro.NewService()
	service.Init()
	c := service.Client()

	request := c.NewRequest("greeter", "Greeter.Hello", "john", client.WithContentType("application/json"))
	var response string
	if err := c.Call(context.TODO(), request, &response); err != nil {
		fmt.Println(err)
	}

	fmt.Println(response)
}

go.mod 代碼如下:

module noproto

go 1.13

require github.com/micro/go-micro v1.18.0 // indirect

 

 

 

發佈了161 篇原創文章 · 獲贊 34 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章