目录

通过 Admin API 添加 Service

请求

响应

为 Service 添加路由(Route)

请求

响应

通过 Kong 转发请求

请求

响应

总结

参考


通过 Admin API 添加 Service

请求

curl -i -X POST \--url http://localhost:8001/services/ \--data 'name=example-service' \--data 'url=http://mockbin.org'

创建了一个名字为 example-service 的 Service,它指向的链接为 Mockbin by Kong

响应

% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.1 201 Created
Date: Wed, 25 Mar 2020 09:16:20 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/2.0.2
Content-Length: 296
X-Kong-Admin-Latency: 202{"host":"mockbin.org","created_at":1585127780,"connect_timeout":60000,"id":"b9303edc-37ca-456c-a696-5bfae3046f36","protocol":"http","name":"example-    service","read_timeout":60000,"port":80,"path":null,"updated_at":1585127780........
100   339  100   296  100    43   1450    210 --:--:-- --:--:-- --:--:--  1661

可以看到 HTTP/1.1 201 Created ,表示创建成功

为 Service 添加路由(Route)

请求

$ curl -i -X POST \--url http://localhost:8001/services/example-service/routes \--data 'hosts[]=example.com'

为 Service example-service 添加了example.com 相关的路由

响应

% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100   448  100   429  100    19  47666   2111 --:--:-- --:--:-- --:--:-- 49777
HTTP/1.1 201 Created
Date: Wed, 25 Mar 2020 09:27:25 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/2.0.2
Content-Length: 429
X-Kong-Admin-Latency: 7{"id": "bacbfa08-619c-4012-9a46-597fed48123c","path_handling": "v0","paths": null,"destinations": null,"headers": null,"protocols": ["http","https"],"methods": null,"snis": null,"service": {"id": "b9303edc-37ca-456c-a696-5bfae3046f36"},"name": null,"strip_path": true,"preserve_host": false,"regex_priority": 0,"updated_at": 1585128445,"sources": null,"hosts": ["example.com"],"https_redirect_status_code": 426,"tags": null,"created_at": 1585128445
}

可以看到 HTTP/1.1 201 Created,表示创建成功

两点疑问:

  1. 从响应看 service 节点是数组,是否表示一个Route 可以被绑定至多个 Service?
  2. 但是从创建时的参数 hosts[] 来看,一个 Route 可以映射多个域名的请求,如果是多个Service,如何路由?

通过 Kong 转发请求

请求

$ curl -i -X GET \--url http://localhost:8000/ \--header 'Host: example.com'

响应

% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 10695
Connection: keep-alive
Server: Cowboy
Etag: W/"29c7-XG+PICJmz/J+UYWt5gkKqqAUXjc"
Vary: Accept-Encoding
Date: Wed, 25 Mar 2020 09:54:45 GMT
Via: kong/2.0.2
X-Kong-Upstream-Status: 200
X-Kong-Upstream-Latency: 547
X-Kong-Proxy-Latency: 292
Kong-Cloud-Request-ID: 86da8249946028e910052385d55aaa5a<!DOCTYPE html><html><head><meta charset="utf-8"><title>Mockbin by Kong</title><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"><link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" media="all"><link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Open+Sans:400,600|Source+Code+Pro:200,300,400,500,600,700,900" media="all"><link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.css" media="all"><link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/github.min.css" media="all"><link rel="stylesheet" type="text/css" href="/static/main.css" media="all"><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.2.0/ZeroClipboard.min.js"></script><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="robots" content="index,follow"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"><meta itemprop="name" content="Mockbin by Kong"><meta property="og:title" content="Mockbin by Kong"><meta name="twitter:title" content="Mockbin by Kong"><link rel="author" href="https://www.mashape.com/"><meta name="author" content="Kong"><meta name="twitter:creator" content="@thekonginc"><meta name="description" content="Mockbin allows you to generate custom endpoints to test, mock, and track HTTP requests &amp; responses between libraries, sockets and APIs. Made with Love by Kong."><meta itemprop="description" content="Mockbin allows you to generate custom endpoints to test, mock, and track HTTP requests &amp; responses between libraries, sockets and APIs. Made with Love by Kong."><meta property="og:description" content="Mockbin allows you to generate custom endpoints to test, mock, and track HTTP requests &amp; responses between libraries, sockets and APIs. Made with Love by Kong."><meta name="twitter:description" content="Mockbin allows you to generate custom endpoints to test, mock, and track HTTP requests &amp; responses between libraries, sockets and APIs. Made with Love by Kong."><meta itemprop="image" content="https://mockbin.org/public/share.png"><meta property="og:image" content="https://mockbin.org/public/share.png"><meta name="twitter:image:src" content="https://mockbin.org/public/share.png"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:site" content="@mashape"><meta name="twitter:domain" content="mockbin.org"><link rel="canonical" href="http://mockbin.org/"><meta name="twitter:url" content="http://mockbin.org/"><meta property="og:url" content="http://mockbin.org/"><meta property="og:type" content="website"><meta property="og:site_name" content="Mockbin by Kong"><meta property="fb:admins" content="227304446"><meta property="fb:admins" content="576641408"><link rel="shortcut icon" href="/public/favicon.ico"><link rel="icon" type="image/x-icon" href="/public/favicon.ico"><link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,800,700,600,300|Source+Code+Pro:200,300,400,500,600,700,900" media="all"><link rel="100 10695  100 10695    0     0   9071      0  0:00:01  0:00:01 --:--:--  9071
tylesheet" type="text/css" href="/public/style.css" media="all"><meta name="google-site-verification" content="OIx3DxcNRJ_Kyd7hAtGRhZnggKpv6DRWutY7Ih9R3Ww"></head><body><header><nav class="navbar navbar-default"><div class="container"><div class="navbar-header"><button type="button" data-toggle="collapse" data-target="#navbar" class="navbar-toggle collapsed"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><div class="navbar-brand logo"><span><a href="/"><span class="logo fa fa-terminal"></span> mockbin</a> <span class="text-muted">by <a href="https://www.konghq.com">Kong</a></span></span></div></div><div id="navbar" class="collapse navbar-collapse"><ul class="nav navbar-nav navbar-right"><li><a href="/docs">Docs</a></li><li><a href="/bin/create">Create Bin</a></li><li><a href="https://github.com/Kong/mockbin">Github</a></li></ul></div></div></nav></header><div class="home"><div class="showcase"><div class="container"><h1>Mockbin</h1><p class="col-lg-offset-2 col-lg-8 lead">Mockbin allows you to generate <a href="/bin/create">custom endpoints</a> to test, mock, and track HTTP requests &amp; responses between libraries, sockets and APIs.</p></div></div><div class="container"><div class="btn-toolbar"><a href="/bin/bbe7f656-12d6-4877-9fa8-5cd61f9522a9/view" class="btn btn-primary">View Sample Bin</a><a href="/bin/create" class="btn btn-success">Create Bin</a><a href="#example" class="btn btn-primary hidden-xs">Send a Request</a></div><hr><h2 class="text-center">Feature Highlights</h2><div class="row features"><div class="col-md-6"><div class="media"><div class="media-left"><img src="/public/friendly.png" class="media-object"></div><div class="media-body"><h4 class="media-heading">Mock Custom Endpoints</h4><p>Mock custom endpoints using any <a href="https://ahmadnassri.github.io/har-resources/" target="_blank">HTTP Archive (HAR)</a> response object <em>(can be used as webhooks, api mocks, or anything you want!)</em></p><p><a href="/docs">Learn More <span class="fa fa-angle-right"></span></a></p></div></div><div class="media"><div class="media-left"><img src="/public/formats.png" class="media-object"></div><div class="media-body"><h4 class="media-heading">JSON, XML, YAML, HTML</h4><p>Don't like JSON? No problem! Mockbin supports output in JSON, YAML and XML, as well as an HTML view for in-browser testing</p><p><a href="/docs#content-negotiation">Learn More <span class="fa fa-angle-right"></span></a></p></div></div><div class="media"><div class="media-left"><img src="/public/history.png" class="media-object"></div><div class="media-body"><h4 class="media-heading">Log and Inspect Calls</h4><p>Log and inspect incoming calls to your custom endpoints <em>(get detailed view to how clients are calling your api/webhook)</em></p><p><a href="/docs">Learn More <span class="fa fa-angle-right"></span></a></p></div></div></div><div class="col-md-6"><div class="media"><div class="media-left"><img src="/public/mock.png" class="media-object"></div><div class="media-body"><h4 class="media-heading">Custom HTTP Method</h4><p>No longer are you limited to <code>GET</code> &amp; <code>POST</code>, Mockbin accepts all standard Methods and allows method overriding</p><p><a href="/docs#http-methods">Learn More <span class="fa fa-angle-right"></span></a></p></div></div><div class="media"><div class="media-left"><img src="/public/inspect.png" class="media-object"></div><div class="media-body"><h4 class="media-heading">CORS Headers</h4><p>Debug your front-end JavaScript HTTP calls from any domain, Mockbin will dynamically generate Cross-Origin resource sharing headers</p><p><a href="/docs">Learn More <span class="fa fa-angle-right"></span></a></p></div></div><div class="media"><div class="media-left"><img src="/public/har.png" class="media-object"></div><div class="media-body"><h4 class="media-heading">HTTP Archive (HAR)</h4><p>Mockbin relies on the popular <a href="https://ahmadnassri.github.io/har-resources/" target="_blank">HTTP Archive (HAR)</a> format to create mock endpoints (Bins), import data and describe HTTP call logs.</p><p><a href="/docs">Learn More <span class="fa fa-angle-right"></span></a></p></div></div></div></div><hr><h2 class="text-center">Test using your preferred language:</h2><iframe id="example" src="https://api.apiembed.com/?source=http://mockbin.org/public/samples/request.json&amp;targets=all" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" width="100%" height="500" seamless class="embed"></iframe></div></div><footer class="hidden-xs"><nav class="navbar navbar-default navbar-fixed-bottom"><div class="container"><div class="navbar-text"><a href="https://github.com/Kong/mockbin" data-icon="octicon-star" data-count-href="/Kong/mockbin/stargazers" data-count-api="/repos/Kong/mockbin#stargazers_count" class="github-button">Star</a><span>&nbsp;</span><a href="https://github.com/Kong/mockbin" data-icon="octicon-eye" data-count-href="/Kong/mockbin/watchers" data-count-api="/repos/Kong/mockbin#subscribers_count" class="github-button">Watch</a><span>&nbsp;</span><a href="https://github.com/Kong/mockbin/issues" data-icon="octicon-issue-opened" data-count-api="/repos/Kong/mockbin#open_issues_count" class="github-button">Issue</a></div><div class="nav navbar-right navbar-text hidden-xs"><a href="https://twitter.com/share" data-url="http://mockbin.org" data-via="thekonginc" data-related="thekonginc" data-hashtags="Mock, Test, Track, HTTP" data-dnt="true" class="twitter-share-button">Tweet</a><span>&nbsp;</span></div></div></nav></footer><script type="text/javascript" id="twitter-wjs" src="https://platform.twitter.com/widgets.js" async defer></script><script type="text/javascript" id="github-bjs" src="https://buttons.github.io/buttons.js" async defer></script><script type="text/javascript" src="//Kong.github.io/notification-bar/embed.js" async defer></script><script type="text/javascript">!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.0.1";analytics.load('tUiM2iBCz991uF4rDF0a4WSr6NEjiVuU');analytics.page()
}}();</script></body></html>

响应的 html 内容是 Service 配置的 mockbin.org 网站的页面,

  • 证明 Route 已经将来自 example.com 的请求转发至配置的服务(mockbin.org)
  • Route 也将响应转发至了请求发起方

总结

  • 每个 Service 对应一个隐藏在其后的 API(创建参数 url 指定的地址)
  • 每个 Service 可以定义多个 Route ,进而将符合这些 Route 定义的请求通过 Serive 转发至 API

参考

https://docs.konghq.com/2.0.x/getting-started/configuring-a-service/(Kong官方文档)

https://docs.konghq.com/2.0.x/admin-api/(Kong Admin API 文档)

Mockbin by Kong(Mockbin 官网)

Kong的Service 配置相关推荐

  1. kong笔记——Service、Route路由功能

    kong笔记 目录导航 在上两篇文章,我们主要介绍了kong的概念以及kong的部署,相信大家对kong已经有了基本认识,那么我们该如何使用它呢? kong的功能有许多,我们一个一个来,先从kong的 ...

  2. centos 7.x systemd service 配置方法整理

    一.存放路径 /etc/systemd/system 二.service配置整理 2.1 zookeeper.service [Unit] Description=ZooKeeper Service ...

  3. CAS单点登录(五)——Service配置及管理

    在上一节我们讲述了CAS中关于自定义认证登录策略,对CAS中关于自定义登录配置的方案,校验策略有了一定的了解,如果忘记了可以去复习一下------CAS单点登录(四)--自定义认证登录策略.这节本来该 ...

  4. FluorineFX开源库 使用教程(service配置xml说明)

    FluorineFX开源库 使用教程 字体大小: 小 中 大 作者: ddv | 分类: 网站开发 | 浏览: 401 | 评论: 暂时没有评论 转载请保留出处: DDV=敌敌畏 www.dplaye ...

  5. 辅助DNS SERVICE 配置工作任务-winserver2

    赛题               2.2.4.辅助DNS SERVICE 配置工作任务 1.辅助DNS配置区域        正向区域        反向区域 2.主DNS权限设置 赛题 2.2.4. ...

  6. DNS SERVICE配置-winserver1

    赛题               2.1.4.DNS SERVICE 配置工作任务 1.配置正反向记录 2.配置TXT记录 赛题 2.1.4.DNS SERVICE 配置工作任务 安装及配置 DNS ...

  7. k8s删除deployment_VPGAME k8s迁移实践(5) Deployment 与 Service 配置

    作者:Charl 在 CI/CD 流程中完成 docker 镜像的打包任务之后需要将服务所对应的镜像部署到 k8s集群中.k8s 提供了多种可以编排调度的资源对象.首先,我们简单了解一下 k8s 中的 ...

  8. linux postgre服务名,linux中service配置之postgresql

    在Mandriva 2010.2中,安装了postgresql之后,一般很少能直接在/etc/init.d/下生成postgresql的service 文档,这样,就不能在chkconfig中配置po ...

  9. kong 安装与配置

    安装kong $ curl -Lo kong-2.5.0.amd64.rpm $( rpm --eval "https://download.konghq.com/gateway-2.x-c ...

最新文章

  1. 上手必备!不可错过的TensorFlow、PyTorch和Keras样例资源
  2. 最简单的React和Redux整合的例子
  3. 第一节 Memcached分布式缓存入门
  4. 上网课的心得体会1000字_网络学习心得体会 (1000字)
  5. 解决python中 .to_csv() 的乱码问题
  6. Delphi中Chrome Chromium、Cef3学习笔记(三)
  7. 图片hover且设置transform其父级border-radius失效
  8. [抽奖系统下载]多进程现场抽奖展示系统V2-双屏PPT版-专业抽奖展示工具-具有强劲引擎的抽奖系统-PPT双屏技术深度应用,国内唯一
  9. k8s集群搭建(一主多从)
  10. 计算机主机并行接口,计算机并行接口技术原理.ppt
  11. 【机器学习基础】正规方程法(Normal equation)(正则化和非正则化)——吴恩达课程笔记
  12. Spring Boot:The field file exceeds its maximum permitted size of 1048576 bytes.
  13. 解决在onedrive里无法打开onenote笔记本的问题
  14. pythonsqlite加锁_Python SQLite:数据库被锁定
  15. 检测某个地图某个怪物的数量的脚本
  16. 中文自动文本摘要生成指标计算,Rouge/Bleu/BertScore/QA代码实现
  17. C语言——归并排序,单线程,多线程(Linux系统下实现)两种实现方法
  18. SRS云服务器:起步、购买和入门
  19. 两台云服务器怎么共享文件夹,云服务器怎么共享文件夹
  20. 推荐 :一文带你了解协同过滤的前世今生

热门文章

  1. sudo,普通用户的提权操作,越权跟踪
  2. 基于51 手机遥控的蓝牙小车(HC-05)
  3. 图灵、图灵机和图灵测试
  4. STM32C8T6编码器电机测速与arduino光电模块测速
  5. HADOOP |MapReduce篇 (08) MapReduce特性
  6. osi七层语言html,OSI分为哪七层 OSI七层功能介绍【详解】
  7. 阿里面试:分析为什么B+树更适合作为索引的结构以及索引原理
  8. 告别BIOS EFI时代来临
  9. 使用Numpy创建数组_总结
  10. python中文词典构建_python-构建英语学习词典