源码
```go
package main
import (
"context"
"github.com/asim/go-micro/v3"
"github.com/asim/go-micro/v3/errors"
"log"
"microservices/gomicro/02/proto"
)
type Example struct {
}
type Foo struct {
}
func (e *Example) Call(ctx context.Context, req *proto.CallRequest, resp *proto.CallResponse) error {
log.Println("收到Example.Call请求")
if len(req.Name) == 0 {
return errors.BadRequest("go.micro.api.example", "no name")
}
resp.Message = "Exmaple.Call接收到了你的请求" + req.Name
return nil
}
func (f *Foo) Bar(ctx context.Context, req *proto.EmptyRequest, resp *proto.EmptyResponse) error {
log.Println("收到Foo.Bar请求")
return nil
}
func main() {
service := micro.NewService(
micro.Name("go.micro.api.example"),
)
service.Init()
err := proto.RegisterExampleHandler(service.Server(), new(Example))
if err != nil {
println(err.Error())
}
err = proto.RegisterFooHandler(service.Server(), new(Foo))
if err != nil {
println(err.Error())
}
err = service.Run()
if err != nil {
log.Fatal(err.Error())
}
}
```
执行 命令 micro api --handler=api
![image.png](https://static.studygolang.com/210906/13b09b16bd351ea988b45caec72c77cd.png)
然后go run 代码
![image.png](https://static.studygolang.com/210906/343f73eab590101faea8164259f6cfbd.png)
之后访问http://127.0.0.1:8080/example/call 就返回not found
![image.png](https://static.studygolang.com/210906/43abf2f32fa7ffca2beea1766170fe2b.png)
![image.png](https://static.studygolang.com/210906/cb25ace8b8920f1200e1693f797d63f8.png)
是访问的路径不对还是因为啥?
有疑问加站长微信联系(非本文作者)