go-micro微服务domain层开发示例详解
本攻略将详细讲解go-micro微服务domain层开发的过程,包括搭建过程、示例说明。
搭建过程
1. 创建go-micro项目
-
创建一个go-micro项目,命名为example。
-
在go.mod文件中添加以下依赖:
require (
github.com/micro/go-micro/v2 v2.9.1
github.com/micro/go-plugins v0.0.0-20220103135708-7d7c7d7d7d7d
)
其中,github.com/micro/go-micro/v2
表示go-micro的核心库,github.com/micro/go-plugins
表示go-micro的插件库。
2. 创建domain层
-
创建一个domain层,命名为example/domain。
-
在domain层中创建一个interface,命名为ExampleService,添加以下代码:
type ExampleService interface {
SayHello(name string) (string, error)
}
其中,SayHello
方法用于向客户端返回一个问候语。
- 在domain层中创建一个struct,命名为exampleService,实现ExampleService接口,添加以下代码:
type exampleService struct{}
func NewExampleService() ExampleService {
return &exampleService{}
}
func (s *exampleService) SayHello(name string) (string, error) {
if name == "" {
return "", errors.New("name is empty")
}
return fmt.Sprintf("Hello, %s!", name), nil
}
其中,NewExampleService
方法用于创建exampleService实例,SayHello
方法用于向客户端返回一个问候语。
3. 创建服务
- 创建一个服务,命名为example-service,添加以下代码:
package main
import (
"example/domain"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/errors"
"log"
)
func main() {
service := micro.NewService(
micro.Name("example-service"),
)
service.Init()
exampleService := domain.NewExampleService()
err := micro.RegisterHandler(service.Server(), &exampleServiceHandler{exampleService})
if err != nil {
log.Fatal(err)
}
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
type exampleServiceHandler struct {
exampleService domain.ExampleService
}
func (h *exampleServiceHandler) SayHello(ctx context.Context, req *example.SayHelloRequest, rsp *example.SayHelloResponse) error {
name := req.GetName()
if name == "" {
return errors.BadRequest("example-service", "name is empty")
}
greeting, err := h.exampleService.SayHello(name)
if err != nil {
return errors.InternalServerError("example-service", err.Error())
}
rsp.SetGreeting(greeting)
return nil
}
其中,micro.NewService
方法用于创建一个go-micro服务,micro.RegisterHandler
方法用于注册一个handler,exampleServiceHandler
实现了example.ExampleServiceHandler
接口。
- 在example/domain中创建一个proto文件,命名为example.proto,添加以下代码:
syntax = "proto3";
package example;
service ExampleService {
rpc SayHello(SayHelloRequest) returns (SayHelloResponse) {}
}
message SayHelloRequest {
string name = 1;
}
message SayHelloResponse {
string greeting = 1;
}
其中,ExampleService
表示服务名称,SayHello
表示服务方法,SayHelloRequest
表示请求参数,SayHelloResponse
表示响应参数。
- 在example/domain中生成go文件,执行以下命令:
protoc --proto_path=. --go_out=. --micro_out=. example.proto
4. 验证服务
- 启动服务,执行以下命令:
go run main.go
- 使用micro命令行工具调用服务,执行以下命令:
micro call example-service ExampleService.SayHello '{"name": "John"}'
- 可以看到服务成功返回了"Hello, John!"。
示例说明
以下是两个示例说明,分别演示了如何使用go-micro微服务domain层开发。
示例一:使用go-micro客户端调用服务
-
创建一个go-micro项目,命名为example-client。
-
在go.mod文件中添加以下依赖:
require (
github.com/micro/go-micro/v2 v2.9.1
github.com/micro/go-plugins v0.0.0-20220103135708-7d7c7d7d7d7d
)
- 创建一个main.go文件,添加以下代码:
package main
import (
"context"
"example/domain"
"github.com/micro/go-micro/v2"
"log"
)
func main() {
service := micro.NewService(
micro.Name("example-client"),
)
service.Init()
exampleService := domain.NewExampleService()
client := example.NewExampleService("example-service", service.Client())
rsp, err := client.SayHello(context.Background(), &example.SayHelloRequest{Name: "John"})
if err != nil {
log.Fatal(err)
}
log.Println(rsp.GetGreeting())
}
其中,micro.NewService
方法用于创建一个go-micro服务,example.NewExampleService
方法用于创建一个go-micro客户端。
- 启动服务和客户端,执行以下命令:
go run main.go
- 可以看到客户端成功调用了服务,并输出了"Hello, John!"。
示例二:使用http客户端调用服务
-
创建一个go-micro项目,命名为example-http-client。
-
在go.mod文件中添加以下依赖:
require (
github.com/micro/go-micro/v2 v2.9.1
github.com/micro/go-plugins v0.0.0-20220103135708-7d7c7d7d7d7d
github.com/micro/go-micro/v2/client/selector v2.9.1
github.com/micro/go-micro/v2/client/selector/static v2.9.1
github.com/micro/go-micro/v2/client/transport v2.9.1
github.com/micro/go-micro/v2/client/transport/http v2.9.1
)
- 创建一个main.go文件,添加以下代码:
package main
import (
"bytes"
"context"
"example/domain"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
exampleService := domain.NewExampleService()
tr := &http.Transport{}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("POST", "http://localhost:8080/example-service/SayHello", bytes.NewBuffer([]byte(`{"name": "John"}`)))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
rsp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer rsp.Body.Close()
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}
其中,http.NewRequest
方法用于创建一个HTTP请求,http.Client
用于发送HTTP请求。
- 启动服务和客户端,执行以下命令:
go run main.go
- 可以看到客户端成功调用了服务,并输出了"Hello, John!"。
总结
使用go-micro微服务domain层开发是一种简单、高效的方式。在实际应用中,我们可以据具体情况选择合适的微服务架构,满足业务需求和技术发展。使用go-micro客户端调用服务可以提高开发效率和代码可读性,使用http客户端调用服务可以方便地与其他系统进行集成。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:go-micro微服务domain层开发示例详解 - Python技术站