GO 微服务GO-Micro(9)-纯个人学习笔记记录:手写一个新的微服务示例

说明

前面的一些记录都是针对我们的使用micro new 创建出来微服务示例模板,后续我们的微服务的编写,会根据自身的实际的情况进行微服务手动的创建。因为有必要的手动创建一个试一试。

步骤

原来项目结构:


image.png
1) 新建一个微服务的文件夹初始化模块

新建一个文件夹doigreeter,cd 进入doigreeter, 然后执行命令行:

D:\code\go\Mi_Onse\doigreeter>go mod init doigreeter
go: creating new go.mod: module doigreeter

D:\code\go\Mi_Onse\doigreeter>

image.png
2) 规划proto文件的存放
D:\code\go\Mi_Onse\doigreeter>mkdir proto

D:\code\go\Mi_Onse\doigreeter>cd proto

D:\code\go\Mi_Onse\doigreeter\proto>mkdir pb

D:\code\go\Mi_Onse\doigreeter\proto>mkdir pbfile

D:\code\go\Mi_Onse\doigreeter\proto>

image.png
3) 编写proto文件内容
syntax = "proto3";

package pb;

//生成go文件的包路径------注意这个目录文件,只读的是生产的文件的存放位置
option go_package = "proto/pb";

// 定义微服务对外提供的接口
service DoiGreeter {

  rpc RunSay(Request) returns (Response) {}
}

// 请求
message Request {
  string name = 1;
}

// 响应
message Response {
  string msg = 1;
}



4) 生成对应的bp.go 和pb.micro.go文件
D:\code\go\Mi_Onse\doigreeter>protoc --proto_path=. --micro_out=. --go_out=. proto/pbfile/dpigreeter.proto
D:\code\go\Mi_Onse\doigreeter>protoc --proto_path=. --micro_out=. --go_out=. proto/pbfile/dpigreeter.proto
D:\code\go\Mi_Onse\doigreeter>protoc --proto_path=. --micro_out=. --go_out=. proto/pbfile/dpigreeter.proto
D:\code\go\Mi_Onse\doigreeter>protoc --proto_path=. --micro_out=. --go_out=. proto/pbfile/dpigreeter.proto

D:\code\go\Mi_Onse\doigreeter>
image.png

XXXXX这些红色提示可以暂时忽略不管!


image.png
5) 编写微服务main文件

因为是新的模块,我们好像还是需要重新拉一个依赖

D:\code\go\Mi_Onse\doigreeter>go get github.com/micro/go-micro/v2

编写main文件:

  • 1:定义一个服务实例的结构体,然后改结构体实现我们之前protoc 生产的协议接口中定义接口(方法)
  • 2 :注意需要加载相关依赖go get github.com/micro/go-micro/v2(之后,发送红色XXXXX没了)
  • 3 :创建一个micro.NewService 对象,且传相关的参数信息,如服务名和版本等信息(服务名不能重名哟!)且进行初始化Init()
  • 4 :将实现了协议接口的结构体进行RegisterGreeterHandler注册;
  • 5 :运行服务(可能有时候,此过程可能会自动去处理上面出现红色XXXXXX)
package main

import (
    "context"
    "doigreeter/proto/pb"
    "fmt"
    "github.com/micro/go-micro/v2"
)

//定义一个服务的结构体
type DoiGreeter struct {}

//定义一个方法,该方法实现对应的接口
func (g *DoiGreeter) RunSay(ctx context.Context, req *pb.Request, rsp *pb.Response) error  {
    //把客户端的请求回射给客户端
    rsp.Msg = req.Name
    return nil
}


func main() {

    // 新创建一个服务,服务名为greeter,服务注册中心会用这个名字来发现服务
    service := micro.NewService(
        micro.Name("doigreeter"),
        micro.Version("1.0.0"),
    )
    // 初始化
    service.Init()
    // 注册处理器
    _ =pb.RegisterDoiGreeterHandler(service.Server(), new(DoiGreeter))

    // 启动服务运行
    if err := service.Run(); err != nil {
        fmt.Println(err)
    }
}
6) 启动我们的编写微服务的情况,在doigreeter目录下:
D:\code\go\Mi_Onse\doigreeter>go run main.go
2021-01-21 16:01:07  [email protected]/service.go:200 level=info Starting [service] doigreeter
2021-01-21 16:01:07  file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:61227
2021-01-21 16:01:07  file=grpc/grpc.go:697 level=info Registry [mdns] Registering node: doigreeter-744e66fc-6940-401f-a273-10e5bdeb587d

7) 查看当前所有的服务列表,在doigreeter目录下:
D:\code\go\Mi_Onse>micro list services
doigreeter(新建的服务)
go.micro.service.greeter(new出来的示例服务)
micro.http.broker

D:\code\go\Mi_Onse>
8) 编写请求doigreeter服务的客户端示例 且运行:
image.png
package main


import (
    "context"
    "doigreeter/proto/pb"
    "fmt"
    "github.com/micro/go-micro/v2"

)

func main() {

    // 创建一个服务(名字区别于我们的服务端名字)
    service := micro.NewService(micro.Name("doigreeter.client"))
    // 初始化
    service.Init()
    // 创建一个微服务的客户端
    greeter := pb.NewDoiGreeterService("doigreeter", service.Client())
    // 调用微服务
    rsp, err := greeter.RunSay(context.TODO(), &pb.Request{Name: "XXXX XIAOZHONGTONGXUE"})
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(rsp.Msg)
}

运行客户端:

D:\code\go\Mi_Onse\doigreeter>go run doigreeter_cli.go
XXXX XIAOZHONGTONGXUE

D:\code\go\Mi_Onse\doigreeter>

9) 使用API网关来代理访问:

(1)查看手动编写的服务详情:

D:\code\go\Mi_Onse\greeter>micro get service  doigreeter
service  doigreeter

version 1.0.0

ID      Address Metadata
doigreeter-3c795c6e-1994-4b41-bacf-aa2e5afb152e 192.168.1.213:62052     server=grpc,transport=grpc,broker=http,protocol=grpc,registry=mdns

Endpoint: DoiGreeter.RunSay

Request: {
        message_state MessageState {
                no_unkeyed_literals NoUnkeyedLiterals
                do_not_compare DoNotCompare
                do_not_copy DoNotCopy
                message_info MessageInfo
        }
        int32 int32
        unknown_fields []uint8
        name string
}

Response: {
        message_state MessageState {
                no_unkeyed_literals NoUnkeyedLiterals
                do_not_compare DoNotCompare
                do_not_copy DoNotCopy
                message_info MessageInfo
        }
        int32 int32
        unknown_fields []uint8
        msg string
}


D:\code\go\Mi_Onse\greeter>

(2)启动API网关:

D:\code\go\Mi_Onse> micro api --namespace=go.micro --type=service
2021-01-21 16:14:16  file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-21 16:14:16  file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-21 16:14:16  [email protected]/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-21 16:14:16  file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:62030
2021-01-21 16:14:17  file=grpc/grpc.go:697 level=info service=api Registry [mdns] Registering node: go.micro.api-0a0b67de-3a9f-48c1-8c39-6028dcffb1ff
::1 - - [21/Jan/2021:16:15:14 +0800] "GET /doigreeter/runsay HTTP/1.1" 500 98 "" "PostmanRuntime/7.26.8"

(3)查看先用启动所有服务列表:

D:\code\go\Mi_Onse>micro list services
doigreeter
go.micro.api

D:\code\go\Mi_Onse>

(4)Postman访问

http://localhost:8080/doigreeter/doigreeter/runsay
image.png

访问不到我们,应该姿势不对!

修改我们的服务启动的是设置服务名称:


image.png

重新再启动服务,查看最新修改后服务列表:

D:\code\go\Mi_Onse>micro list services
go.micro.api
go.micro.service.doigreeter

D:\code\go\Mi_Onse>

再访问我们的

http://localhost:8080/doigreeter/doigreeter/runsay
image.png

问题分析 查看我们的new微服务的示例:

D:\code\go\Mi_Onse>micro call go.micro.service.greeter greeter.call
error calling go.micro.service.greeter.greeter.call: {"id":"go.micro.client","code":500,"detail":"unknown service greeter","status":"Internal Server Error"}
image.png

使用命令行形式调用:

D:\code\go\Mi_Onse>micro call go.micro.service.greeter greeter.call
error calling go.micro.service.greeter.greeter.call: {"id":"go.micro.client","code":500,"detail":"unknown service greeter","status":"Internal Server Error"}

D:\code\go\Mi_Onse>micro call go.micro.service.doigreeter Doigreeter.runsay
error calling go.micro.service.doigreeter.Doigreeter.runsay: {"id":"go.micro.client","code":500,"detail":"unknown service Doigreeter","status":"Internal Server Error"}

D:\code\go\Mi_Onse>micro call go.micro.service.doigreeter Doigreeter.runSay
error calling go.micro.service.doigreeter.Doigreeter.runSay: {"id":"go.micro.client","code":500,"detail":"unknown service Doigreeter","status":"Internal Server Error"}

D:\code\go\Mi_Onse>micro call go.micro.service.doigreeter Doigreeter.RunSay
error calling go.micro.service.doigreeter.Doigreeter.RunSay: {"id":"go.micro.client","code":500,"detail":"unknown service Doigreeter","status":"Internal Server Error"}

D:\code\go\Mi_Onse>micro call go.micro.service.greeter Greeter.call
error calling go.micro.service.greeter.Greeter.call: {"id":"go.micro.client","code":500,"detail":"unknown service Greeter.call","status":"Internal Server Error"}

D:\code\go\Mi_Onse>

奇怪new也出现错误:
再次重新启动网关API ,又好了!!!可能是我们的修改了什么需要重启!!!!

D:\code\go\Mi_Onse> micro api --namespace=go.micro --type=service
2021-01-21 16:46:35  file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-21 16:46:35  file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-21 16:46:35  [email protected]/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-21 16:46:35  file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:64066
2021-01-21 16:46:35  file=grpc/grpc.go:697 level=info service=api Registry [mdns] Registering node: go.micro.api-0f3c307f-be98-484b-8c61-dbc88275b44a

image.png
image.png

此时再去postman再访问?却涛声依旧!!!!

image.png

一个代码访问(估计没修改访问的服务):

D:\code\go\Mi_Onse\doigreeter>go run doigreeter_cli.go
{"id":"go.micro.client","code":500,"detail":"service doigreeter: not found","status":"Internal Server Error"}
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x28 pc=0xef6804]
image.png
image.png

卧槽最后这样可以:所以确定了 最终需要这样才能访问!!!!!但是为啥new出来默认全部小写了呐?????

http://localhost:8080/doigreeter/DoiGreeter/RunSay
image.png
  • POSTMAN请求传递参数传递大小写问题:
image.png
image.png

分析两个服务的不同:

  • go.micro.service.greeter
D:\code\go\Mi_Onse>micro get service go.micro.service.greeter
service  go.micro.service.greeter

version latest

ID      Address Metadata
go.micro.service.greeter-52924ad0-5302-4d9d-8b2c-ea92c97a2d10   192.168.1.213:63439     broker=http,protocol=grpc,registry=mdns,server=grpc,transport=grpc

Endpoint: Greeter.Call

Request: {
        message_state MessageState {
                no_unkeyed_literals NoUnkeyedLiterals
                do_not_compare DoNotCompare
                do_not_copy DoNotCopy
                message_info MessageInfo
        }
        int32 int32
        unknown_fields []uint8
        name string
}

Response: {
        message_state MessageState {
                no_unkeyed_literals NoUnkeyedLiterals
                do_not_compare DoNotCompare
                do_not_copy DoNotCopy
                message_info MessageInfo
        }
        int32 int32
        unknown_fields []uint8
        msg string
}

  • go.micro.service.doigreeter
D:\code\go\Mi_Onse>micro get service go.micro.service.doigreeter
service  go.micro.service.doigreeter

version 1.0.0

ID      Address Metadata
go.micro.service.doigreeter-41ad5768-0f7d-4f75-9e8e-328198f46192        192.168.1.213:62729     broker=http,protocol=grpc,registry=mdns,server=grpc,transport=grpc

Endpoint: DoiGreeter.RunSay

Request: {
        message_state MessageState {
                no_unkeyed_literals NoUnkeyedLiterals
                do_not_compare DoNotCompare
                do_not_copy DoNotCopy
                message_info MessageInfo
        }
        int32 int32
        unknown_fields []uint8
        name string
}

Response: {
        message_state MessageState {
                no_unkeyed_literals NoUnkeyedLiterals
                do_not_compare DoNotCompare
                do_not_copy DoNotCopy
                message_info MessageInfo
        }
        int32 int32
        unknown_fields []uint8
        msg string
}


D:\code\go\Mi_Onse>

你可能感兴趣的:(GO 微服务GO-Micro(9)-纯个人学习笔记记录:手写一个新的微服务示例)