GO 微服务GO-Micro(15)-串联我们的整个微服务(前篇

2021-01-28  本文已影响0人  小钟钟同学

关于自定义网关前提需要了解的三层架构

原架构:
三层架构表示:
image.png

一个服务的示例调用流程

1.流程简图

image.png

2.流程明细简图

image.png

自定义网关在windows下的启动自定义服务网关

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --namespace=go.micro --type=service --address 0.0.0.0:8080

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 --server_name go.micro.api api --address=0.0.0.0:9000 --namespace=go.micro --type=service

错误过程示例

使用此方式启动的网关没有问题:

D:\code\go\Mi_Onse\Htypp>micro --registry=etcd --registry_address=192.168.219.130:2379  api  --namespace=go.micro --type=service --address 0.0.0.0:8080
2021-01-28 15:14:27  file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-28 15:14:27  file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-28 15:14:27  file=v2@v2.9.1/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-28 15:14:27  file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:64571
2021-01-28 15:14:27  file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-980caac6-d550-4d36-956f-84186cb69a33
::1 - - [28/Jan/2021:15:14:32 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:14:33 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:14:34 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:14:34 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:14:35 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:14:36 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"

使用下面的代码的方式启动:

PS:代码的方式启动网关的时候,需要注意的事项:后面也需要制定启动的类型 感谢一个zcw(275107947) 大佬的答疑解惑!

定制Micro Aai代码方式启动 main.go:

package main

import (

    "github.com/micro/go-micro/v2"
    "github.com/micro/go-micro/v2/registry"
    "github.com/micro/go-micro/v2/registry/etcd"
    "github.com/micro/micro/v2/cmd"
)
func init() {
    //token := &token.Token{}
    //token.InitConfig("127.0.0.1:8500", "micro", "config", "jwt-key", "key")

    //plugin.Register(cors.)

}

func main() {

    // PrometheusBoot()
    // registry
    reg := etcd.NewRegistry(registry.Addrs("192.168.219.130:2379"))

    cmd.Init(
        micro.Name("go.micro.api"), // no effect
        micro.Registry(reg),
        micro.Address(":8888"),

        //micro.WrapClient(micro_hystrix.NewClientWrapper()),
    )

}

启动:

D:\code\go\micro-greeter\micro>go run main.go api --handler=api
或
D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --handler=api

结果

D:\code\go\micro-greeter\micro>go run main.go api --handler=api
2021-01-28 15:18:48  file=api/api.go:233 level=info service=api Registering API Request Handler at /
2021-01-28 15:18:48  file=api/api.go:308 level=fatal service=api listen tcp :8080: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
exit status 1

代码里知道了注册到的服务中心的,如果没有的话也需要加上:

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --handler=api


查询服务列表:

D:\code\go\Mi_Onse>micro --registry etcd --registry_address 192.168.219.130:2379 list services
go.micro.api
go.micro.service.greeter
go.micro.service.user

D:\code\go\Mi_Onse>

但是问题是,请求不到我们的服务了!!!!!


image.png

这种方式启动的话,则没有响应提内容返回:

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --handler=api  --namespace=go.micro --type=service

image.png

另一种方式:

go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --handler=api  --namespace=go.micro

结果500:

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --handler=api  --namespace=go.micro
2021-01-28 15:24:11  file=api/api.go:233 level=info service=api Registering API Request Handler at /
2021-01-28 15:24:11  file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-28 15:24:11  file=v2@v2.9.1/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-28 15:24:11  file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:8888
2021-01-28 15:24:11  file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-c2fe9b2a-b4f0-4090-ba53-141002b7cdf8
::1 - - [28/Jan/2021:15:24:14 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 94 "" "PostmanRuntime/7.26.8"
500 500 500 500 500 500 
500 500 500 
500 500 

另一种方式:

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --handler=http  --namespace=go.micro

结果500:

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --handler=http  --namespace=go.micro
2021-01-28 15:25:26  file=api/api.go:259 level=info service=api Registering API HTTP Handler at /{service:[a-zA-Z0-9]+}
2021-01-28 15:25:26  file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-28 15:25:26  file=v2@v2.9.1/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-28 15:25:26  file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:8888
2021-01-28 15:25:26  file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-22d538ab-59d8-43a4-ad60-cbff35da4708
::1 - - [28/Jan/2021:15:25:28 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:25:29 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:25:30 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:25:31 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:25:31 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:25:32 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"

错误:

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 --server_name api-gateway api --address=0.0.0.0:9000 --namespace=go.micro.api --handler=http


2021-01-28 15:37:29  file=api/api.go:259 level=info service=api Registering API HTTP Handler at /{service:[a-zA-Z0-9]+}
2021-01-28 15:37:29  file=http/http.go:90 level=info service=api HTTP API Listening on [::]:9000
2021-01-28 15:37:29  file=v2@v2.9.1/service.go:200 level=info service=api Starting [service] api-gateway
2021-01-28 15:37:29  file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:50848
2021-01-28 15:37:29  file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: api-gateway-8587a0ab-a3a9-4240-9b5d-5a982c339c4a
::1 - - [28/Jan/2021:15:37:30 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:37:32 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"

错误:

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 --server_name go.micro.api api --address=0.0.0.0:9000 --namespace=go.micro.api --type=qpi --handler=http


2021-01-28 15:39:40  file=api/api.go:259 level=info service=api Registering API HTTP Handler at /{service:[a-zA-Z0-9]+}
2021-01-28 15:39:40  file=http/http.go:90 level=info service=api HTTP API Listening on [::]:9000
2021-01-28 15:39:40  file=v2@v2.9.1/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-28 15:39:40  file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:51150
2021-01-28 15:39:40  file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-e593e92d-dc54-4204-b4d8-de2877efd68b
::1 - - [28/Jan/2021:15:39:44 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:39:45 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:39:45 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:39:46 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:39:46 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 0 "" "PostmanRuntime/7.26.8"
2021-01-28 15:39:48  file=grpc/grpc.go:791 level=info service=api Deregistering node: go.micro.api-e593e92d-dc54-4204-b4d8-de2877efd68b
2021-01-28 15:39:48  file=grpc/grpc.go:959 level=info service=api Broker [http] Disconnected from 127.0.0.1:0

另一个可以正常跑起来的!!!

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --namespace=go.micro --type=service --address 0.0.0.0:8080

或:

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 --server_name go.micro.api api --address=0.0.0.0:9000 --namespace=go.micro --type=service

结果

D:\code\go\micro-greeter\micro>go run main.go --registry=etcd --registry_address=192.168.219.130:2379 api --namespace=go.micro --type=service --address 0.0.0.0:8080
2021-01-28 15:31:54  file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-28 15:31:54  file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-28 15:31:54  file=v2@v2.9.1/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-28 15:31:54  file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:50183
2021-01-28 15:31:54  file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-8bd24488-b860-490c-a33c-2533b955c50c
::1 - - [28/Jan/2021:15:31:57 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:31:58 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:32:00 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:32:00 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [28/Jan/2021:15:32:01 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"

上一篇下一篇

猜你喜欢

热点阅读