微服务架构
在微服务的架构,我们知道API网关这个东西是必不可少的一部分。那什么是API网关呐?
图示来源:https://github.com/stack-labs/learning-videos/tree/master/docs/micro-api
图示来源:https://github.com/stack-labs/learning-videos/tree/master/docs/micro-api
简单来说API网关就是:统一的一个入口地址,也类似LB,把我们的所以请求统一指向网关,然后由网关统一根据路由进行相关协议转换分发到后端服务上。作为一个网关,我们还可以同时再我们的API网关地方集成我们的相关的鉴权,限流,熔断,降级等服务。
如官网提供的示例图:
通过API网关访问我们的服务
之前我们的也有介绍过关于API网关,https://www.jianshu.com/p/47ab24998f33
之前启动我们的网关的时候使用的形式是:
D:\code\go\Mi_Onse> micro api --namespace=go.micro --type=service
现在我们重新再认识一下这个,查看我们的micro api --help 相关的命令:
D:\code\go\Mi_Onse>micro api -h
NAME:
micro api - Run the api gateway
USAGE:
micro api [command options] [arguments...]
OPTIONS:
--address value Set the api address e.g 0.0.0.0:8080 [%MICRO_API_ADDRESS%]
--handler value Specify the request handler to be used for mapping HTTP requests to services; {api, event, http, rpc} [%MICRO_API_HANDLER%]
--namespace value Set the namespace used by the API e.g. com.example [%MICRO_API_NAMESPACE%]
--type value Set the service type used by the API e.g. api [%MICRO_API_TYPE%]
--resolver value Set the hostname resolver used by the API {host, path, grpc} [%MICRO_API_RESOLVER%]
--enable_rpc Enable call the backend directly via /rpc (default: false) [%MICRO_API_ENABLE_RPC%]
--enable_cors Enable CORS, allowing the API to be called by frontend applications (default: true) [%MICRO_API_ENABLE_CORS%]
--help, -h show help (default: false)
对于上述的列举的命令:
以下参考来源为:https://blog.csdn.net/ztemt_sw2/article/details/106208946
--address value :指定我们的网关地址,默认端口号码是8080
--namespace:设置API使用的名称空间,意思就是可以通过namespace将服务归类,例如对外的公共服务和内部服务,注意这里的域名要跟代码对应起来,比如域名为com.test.api,那么代码中的服务名就要以com.test.api开头。
--handler:网关用到的http handler,不同的handler处理不同的请求,对请求的处理方式也有所不同。
micro支持的handler类型以下几种:
api handler:可以处理任意http请求,并以RPC的方式将请求转发给后端服务,url path为/service/method,例如http://localhost:8080/greeter/hello,则网关会在注册中心中寻找endpoints为Greeter.Hello的服务并转发;
rpc handler:处理http post请求,http body为json或者protobuf格式(Content-Type为application/json或者application/protobuf),url path的要求同api handler;
event handler:将请求做为消息发送到消息中间件上;
proxy handler:用于http反向代理;
启动micro api
启动命令:
D:\code\go\Mi_Onse>micro api
2021-01-26 16:43:39 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-26 16:43:39 file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-26 16:43:39 [email protected]/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-26 16:43:39 file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:53083
2021-01-26 16:43:39 file=grpc/grpc.go:697 level=info service=api Registry [mdns] Registering node: go.micro.api-cf515fc3-d7fa-460f-b434-7328596c42ca
上述看到我们的 API也是一个服务,它启动的时候,默认注册到了MDNS中,此时我们的通过ectd是找不到我们的服务列表。
D:\code\go\Mi_Onse>micro --registry etcd --registry_address 192.168.219.130:2379 list services
go.micro.service.greeter
micro.http.broker
D:\code\go\Mi_Onse>
使用默认查询 MDNS则可以找得到我们的服务列表:
D:\code\go\Mi_Onse>micro list services
go.micro.api
D:\code\go\Mi_Onse>
启动命令把API也注册到我们的ECTD中心上:
命令方式注册:
D:\code\go\Mi_Onse>micro --registry=etcd --registry_address=192.168.219.130:2379 api
结果:
D:\code\go\Mi_Onse>micro --registry=etcd --registry_address=192.168.219.130:2379 api
2021-01-26 16:53:36 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-26 16:53:36 file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-26 16:53:36 [email protected]/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-26 16:53:36 file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:53798
2021-01-26 16:53:36 file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-c7781046-a08f-4678-8606-6fdc3c898845
此时查看我们的服务列表,已经有有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
micro.http.broker
尝试通过我们的网关访问我们的内部的服务:
尴尬了吧!找不到服务!!!好像是我启动的时候没设置具体命名空间!!
修改我们的启动的命令:
命令:
D:\code\go\Mi_Onse>micro --registry=etcd --registry_address=192.168.219.130:2379 api --namespace=go.micro --type=service
结果:
D:\code\go\Mi_Onse>micro --registry=etcd --registry_address=192.168.219.130:2379 api --namespace=go.micro --type=service
2021-01-26 17:00:05 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-26 17:00:05 file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-26 17:00:05 [email protected]/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-26 17:00:05 file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:54307
2021-01-26 17:00:05 file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-4b8268f7-1209-4308-ada6-3d70021977c9
::1 - - [26/Jan/2021:17:00:09 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
再次访问我们的地址:http://localhost:8080/greeter/greeter/call
错误的类型设置:
D:\code\go\Mi_Onse>micro --registry=etcd --registry_address=192.168.219.130:2379 api --namespace=go.micro --type=api
结果:
D:\code\go\Mi_Onse>micro --registry=etcd --registry_address=192.168.219.130:2379 api --namespace=go.micro --type=api
2021-01-26 17:09:57 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-26 17:09:57 file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-26 17:09:57 [email protected]/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-26 17:09:57 file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:55158
2021-01-26 17:09:57 file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-e0f4f67c-7d92-4c32-822b-19a0f06f9836
::1 - - [26/Jan/2021:17:10:01 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 94 "" "PostmanRuntime/7.26.8"
::1 - - [26/Jan/2021:17:10:01 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 94 "" "PostmanRuntime/7.26.8"
::1 - - [26/Jan/2021:17:10:02 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 94 "" "PostmanRuntime/7.26.8"
::1 - - [26/Jan/2021:17:10:03 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 94 "" "PostmanRuntime/7.26.8"
::1 - - [26/Jan/2021:17:10:03 +0800] "GET /greeter/greeter/call HTTP/1.1" 500 94 "" "PostmanRuntime/7.26.8"
设置命名空间的方式:
## Set Namespace
Run the micro API with custom namespace
```
micro api --handler=api --namespace=com.foobar.api
```
or
```
MICRO_API_NAMESPACE=com.foobar.api micro api --handler=api
```
Set service name with the namespace
```
service := micro.NewService(
micro.Name("com.foobar.api.example"),
)
```
注意事项点就是我们的启动的API网关的时候,需要记得设置它的明明空间,不然我们的访问是找不到服务的!!!
网关命名空间测试和猜想:
试一试,修改我们命名空间看看:
此时访问我们的接口,能正常的访问:http://localhost:8080/greeter/greeter/call
更换一个类型:
则这个时候是找不到服务滴:
启动多个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.service22.greeter
micro.http.broker
D:\code\go\Mi_Onse>
启动网关 使用另一个端口启动:
micro --registry=etcd --registry_address=192.168.219.130:2379 api --namespace=go.micro --type=service --address 0.0.0.0:8080
操作:
D:\code\go\Mi_Onse>micro --registry=etcd --registry_address=192.168.219.130:2379 api --namespace=go.micro --type=service22 --address 0.0.0.0:8082
2021-01-26 17:56:03 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-26 17:56:03 file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8082
2021-01-26 17:56:03 [email protected]/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-26 17:56:03 file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:58991
2021-01-26 17:56:04 file=grpc/grpc.go:697 level=info service=api Registry [etcd] Registering node: go.micro.api-e2a61d48-41e0-4ac8-a49c-466f0081f09f
::1 - - [26/Jan/2021:17:56:10 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
::1 - - [26/Jan/2021:17:56:11 +0800] "GET /greeter/greeter/call HTTP/1.1" 200 16 "" "PostmanRuntime/7.26.8"
请求服务,此时都兼容,访问的是
http://localhost:8082/greeter/greeter/call 访问服务service22
http://localhost:8080/greeter/greeter/call 访问服务service
另一种测试1:
另一种测试2:
另一种测试3:
另一种测试4(OK):
》PS: 请求的地址也可以改为:http://localhost:8080/greeter/call 或 http://localhost:8080/greeter/greeter/cal