GO 微服务GO-Micro(15)-串联我们的整个微服务(前篇-无营养,纯记录错误!自定义Micro api网关错误问题)

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

原架构:
  • micro api(localhost:8080) - 作为http入口点
  • api服务(go.micro.api.greeter) - 为面向公众提供服务(本身也是一个SRV,但是可以聚合SRV)
  • 后端服务(go.micro.srv.greeter,go.micro.srv.user) - 内部范围服务


    image.png
三层架构表示:
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"

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容