首页 >> 大全

花椒直播Kong应用实践

2023-12-09 大全 25 作者:考证青年

什么是Kong

Kong 是面向现代架构(混合云,混合组织)的下一代 API 网关平台,具有云原生、高性能,易用、可扩展等特性。

适用于 Api , , Mesh 等场景。

主要特性有:

为什么使用Kong

目前我们需要解决的问题

统一入口:服务端微服务框架中,接口权限验证,ip限制,限流等在各个服务中都单独实现。没有统一入口,不方便统一管理。

易用性,扩展性:服务端技术栈主要是LNMP开发,目前在逐步转型到基于 Boot、 微服务技术栈上开发。这是一个灰度迁移的过程,我们需要Proxy能操作简单,管理方便。

持续集成发布:互联网 2C 产品,用户无时不刻不在使用服务,同时产品还在不断的迭代,服务每时每刻都可以发布,所以必须要热部署能力,并且是自动化的。但是基于 Boot的服务启动 15 -30 秒,我们需要 Kong 的蓝绿发布功能。

Kong 可以完美的解决以上问题,解决方案如下:

统一入口:可以作为微服务统一入口,将限流,权限验证,日志,ip 限制等统一实现

_花椒直播答题是真的吗_花椒直播功能特色

易用性,扩展性:提供了操作方式,并且有管理工具

# 创建一个名称 hello 的 upstream
curl -X POST http://localhost:8001/upstreams --data "name=hello"# 为 hello 添加两个负载均衡节点
curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=localhost:8080" --data "weight=100"curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=localhost:8081" --data "weight=100"# 配置一个service
curl -X POST http://localhost:8001/services --data "name=hello" --data "host=hello"# 为service 配置路由
curl -X POST http://localhost:8001/routes --data "paths[]=/hello" --data "service.id={$service.id 配置上面service返回的}"

工具截图

持续集成发布:与 CI/CD 配合,代码提交代码库后,自动打包,运行测试用例,蓝绿部署。

_花椒直播答题是真的吗_花椒直播功能特色

我们如何使用Kong

Kong集群

所有节点连接到数据中心

节点有本地缓存,可以设置缓存过期时间

支持动态扩容,参考上方架构图,Kong集群在Lvs后面

支持 gRPC

# /etc/kong/kong.conf
proxy_listen = 0.0.0.0:8000, 0.0.0.0:8443 ssl, 0.0.0.0:9080 http2

服务监控

注意事项

kong//.lua 模板文件统一

对于kong日志要做好切割,我们使用

Kong插件

花椒直播答题是真的吗__花椒直播功能特色

基本流程

1.下载模板

base简单版

simple-plugin
├── handler.lua
└── schema.lua

高级版

complete-plugin
├── api.lua
├── daos.lua
├── handler.lua
├── migrations
│   ├── cassandra.lua
│   └── postgres.lua
└── schema.lua

_花椒直播功能特色_花椒直播答题是真的吗

必要文件就是 .lua 和 .lua

2.修改 .lua 文件,有很多函数,自定义逻辑就是在这些函数中写kong会在一些特定阶段调用对应的函数

local BasePlugin = require "kong.plugins.base_plugin"
-- The actual logic is implemented in those moduleslocal access = require "kong.plugins.my-custom-plugin.access"local body_filter = require "kong.plugins.my-custom-plugin.body_filter"
local CustomHandler = BasePlugin:extend()
function CustomHandler:new()    CustomHandler.super.new(self, "my-custom-plugin")end
function CustomHandler:access(config)    CustomHandler.super.access(self)-- Execute any function from the module loaded in `access`,    -- for example, `execute()` and passing it the plugin's configuration.    access.execute(config)end
function CustomHandler:body_filter(config)    CustomHandler.super.body_filter(self)-- Execute any function from the module loaded in `body_filter`,    -- for example, `execute()` and passing it the plugin's configuration.    body_filter.execute(config)end
return CustomHandler

3.修改 .lua 文件,主要是配置一些参数,以及参数检查

return {no_consumer = true, -- this plugin will only be applied to Services or Routes,fields = {-- Describe your plugin's configuration's schema here.},self_check = function(schema, plugin_t, dao, is_updating)-- perform any custom verificationreturn trueend
}

4. 配置部署

插件文件在

/data/kong/plugins/simple-plugin/

则配置kong.conf

lua_package_path = /data/?.lua;($default);
plugins = bundled,simple-plugin

然后重新 kong 如果lua插件没有错误,就可以在后台看到加载出来了

线上碰到问题

返回的内容太大,需要加大 ( cache error) 修改配置

nginx_proxy_proxy_buffer_size=128k
nginx_proxy_proxy_buffers=4 256k
nginx_proxy_proxy_busy_buffers_size=256k

需要注意配置属性

如果设置为 true, paths 设置有值,那么请求将会被替换掉
{ "paths": ["/service"], "strip_path": true, "service": { "id": "..." }}
请求:GET /service/path/to/resource HTTP/1.1Host: …
Proxy: GET /path/to/resource HTTP/1.1{ "paths": ["/version/\d+/service"], "strip_path": true, "service": { "id": "..." }}
请求:GET /version/1/service/path/to/resource HTTP/1.1
Proxy: GET /path/to/resource HTTP/1.1

如果设置为 true,代理后仍然保留 header 请求 host
请求:GET / HTTP/1.1Host: service.com
Proxy: GET / HTTP/1.1Host: service.com设置为false,将不保留header host
GET / HTTP/1.1Host: service.com
GET / HTTP/1.1Host: 

总结

Kong 是行业内较为成熟的网关开源产品,无论是性能,易用,扩展方面都表现不错。

Kong 就是服务治理的翅膀,可以更优雅,更便捷,更智能的实现服务降级,熔断,流量调度等工作。

让我们在自建 私有云,Hulk云,阿里云等复杂的架构中任意翱翔。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了