(1). 前言

在这一小节,先通过Admin API来对Kong入门.

(2). Nginx配置

http {
	# 1. 定义upstream
    upstream test-service-upstream {
		# 2. 定义Target
        server   172.17.13.119:8090;
    } # end test-service
	
	server {
	    listen       8000;
	    server_name  localhost;
		
		 # 4. 定义路由,并绑定:service
		 location /test-service {
			 # 3. 定义service
			 proxy_pass http://test-service;
		 } # end location
		
	} # end server
} # end http	

(3). Kong API操作

# 1. 配置upstream(upstream与target是:一对多的关联)
[root@tomcat-1 ~]# curl -X POST http://localhost:8001/upstreams --data "name=test-service-upstream"
{
	"created_at": 1623314848,
	"hash_on": "none",
	"id": "b939a556-7e02-4d6f-b49a-a78279683f83",
	"tags": null,
	"name": "test-service-upstream",
	"hash_fallback_header": null,
	"hash_on_cookie": null,
	"healthchecks": {
		"active": {
			"unhealthy": {
				"http_statuses": [429, 404, 500, 501, 502, 503, 504, 505],
				"tcp_failures": 0,
				"timeouts": 0,
				"http_failures": 0,
				"interval": 0
			},
			"type": "http",
			"http_path": "\/",
			"timeout": 1,
			"healthy": {
				"successes": 0,
				"interval": 0,
				"http_statuses": [200, 302]
			},
			"https_sni": null,
			"https_verify_certificate": true,
			"concurrency": 10
		},
		"passive": {
			"unhealthy": {
				"http_failures": 0,
				"http_statuses": [429, 500, 503],
				"tcp_failures": 0,
				"timeouts": 0
			},
			"healthy": {
				"http_statuses": [200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308],
				"successes": 0
			},
			"type": "http"
		}
	},
	"hash_on_cookie_path": "\/",
	"hash_fallback": "none",
	"hash_on_header": null,
	"slots": 10000
}

# 2. 配置target(在upstream下test-service下添加:172.17.13.119:8090)
[root@tomcat-1 ~]# curl -X POST http://localhost:8001/upstreams/test-service-upstream/targets --data "target=172.17.13.119:8090" --data "weight=100"
{
	"created_at": 1623314921.414,
	"upstream": {
		"id": "b939a556-7e02-4d6f-b49a-a78279683f83"
	},
	"id": "1fe9f784-41b0-4474-9140-4aa2270cb25b",
	"target": "172.17.13.119:8090",
	"weight": 100
}


# 根据id,删除target
# curl -X DELETE http://localhost:8001/upstreams/test-service-upstream/targets/1fe9f784-41b0-4474-9140-4aa2270cb25b


# 3. 配置service(为什么是这样呢?是因为:kong里service与route是:多对多的关系)
#    name : service的名称
#    host : upstream的名称
[root@tomcat-1 ~]# curl -X POST http://localhost:8001/services --data "name=test-service" --data "host=test-service-upstream"
{
	"host": "test-service-upstream",
	"created_at": 1623314957,
	"connect_timeout": 60000,
	"id": "386e8052-2429-4216-ad85-fce2002c9f43",
	"protocol": "http",
	"name": "test-service",
	"read_timeout": 60000,
	"port": 80,
	"path": null,
	"updated_at": 1623314957,
	"retries": 5,
	"write_timeout": 60000,
	"tags": null
}


# 4. route绑定service
#  test-service-route  : 路由的唯一名称
[root@tomcat-1 ~]# curl -X POST http://localhost:8001/routes --data "name=test-service-route" --data "paths[]=/test-service" --data "service.id=386e8052-2429-4216-ad85-fce2002c9f43"
{
	"updated_at": 1623315051,
	"created_at": 1623315051,
	"strip_path": true,
	"snis": null,
	"hosts": null,
	"name": "test-service-route",
	"methods": null,
	"sources": null,
	"preserve_host": false,
	"regex_priority": 0,
	"service": {
		"id": "386e8052-2429-4216-ad85-fce2002c9f43"
	},
	"paths": ["\/test-service"],
	"destinations": null,
	"id": "91d7473d-1b84-4fbf-808c-6cfb33a1d1d3",
	"protocols": ["http", "https"],
	"tags": null
}

(4). 测试

# 1. 正常测试
lixin-macbook:~ lixin$ curl http://10.211.55.100:8000/test-service/hello
Hello World!!!

# 2. 关闭(target:172.17.13.119:8090)
lixin-macbook:~ lixin$ curl http://10.211.55.100:8000/test-service/hello
An invalid response was received from the upstream server

(5). 查看数据库信息


kong=> SELECT * FROM routes;
 91d7473d-1b84-4fbf-808c-6cfb33a1d1d3 | 2021-06-10 16:50:51+08 | 2021-06-10 16:50:51+08 | 386e8052-2429-4216-ad85-fce2
002c9f43 | {http,https} |         |       | {/test-service} |              0 | t          | f             | test-service-route |      |         |              |


kong=> SELECT * FROM services;
 386e8052-2429-4216-ad85-fce2002c9f43 | 2021-06-10 16:49:17+08 | 2021-06-10 16:49:17+08 | test-service |       5 | htt
p     | test-service-upstream |   80 |      |           60000 |         60000 |        60000 |


kong=> SELECT * FROM upstreams;
 b939a556-7e02-4d6f-b49a-a78279683f83 | 2021-06-10 16:47:28+08 | test-service-upstream | none    | none          |
            |                      |                | /                   | 10000 | {"active": {"type": "http", "healthy": {"interval": 0, "successes": 0, "http_statuses": [200, 302]}, "timeout": 1, "http_path": "/", "https_sni": null,"unhealthy": {"interval": 0, "timeouts": 0, "tcp_failures": 0, "http_failures": 0, "http_statuses": [429, 404, 500, 501, 502, 503, 504, 505]}, "concurrency": 10, "https_verify_certificate": true}, "passive": {"type": "http", "healthy":{"successes": 0, "http_statuses": [200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308]}, "unhealthy": {"timeouts": 0, "tcp_failures": 0, "http_failures": 0, "http_statuses": [429, 500, 503]}}} |

kong=> SELECT * FROM targets;
 f3ff5119-8ad7-46bf-90b5-62efd090c47a | 2021-06-10 17:00:39.654+08 | b939a556-7e02-4d6f-b49a-a78279683f83 | 172.17.13.119:8090 |    100 |

(6). 总结

Kong官网的介绍还是挺全面的,基本一把测试通过,心中有个想法:在Spring启动时,调用Kong API,完成location的自动注册.