基于customerId来实现

定义两个upstream,他们和service及route的关系如下:
在这里插入图片描述
这里我们使用

0、将下面的这个spring boot项目在192.168.19.50上进行部署

KongDemoApplication.java

package com.example.kongdemo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class KongDemoApplication {
    
    
    @Value("${server.port}")
    private String port;

    @Value("${spring.range}")
    private String range;

    public static void main(String[] args) {
    
    
        SpringApplication.run(KongDemoApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello(){
    
    
        return "hello : " + port + ";range:" + range;
    }

    @GetMapping("/hello/sub")
    public String subHello(){
    
    
        return "sub-hello: " + port + ";range:" + range;
    }

}

applicaton.properties

server.port=8080
spring.range=CN

项目打成jar包后,在192.168.19.50机器按照如下命令运行6个实例:
nohup java -jar kong-demo-0.0.1-SNAPSHOT.jar --server.port=8080 --spring.range=CN > 8080.txt &
nohup java -jar kong-demo-0.0.1-SNAPSHOT.jar --server.port=8081 --spring.range=CN > 8081.txt &
nohup java -jar kong-demo-0.0.1-SNAPSHOT.jar --server.port=8082 --spring.range=CN > 8082.txt &
nohup java -jar kong-demo-0.0.1-SNAPSHOT.jar --server.port=8083 --spring.range=OT > 8083.txt &
nohup java -jar kong-demo-0.0.1-SNAPSHOT.jar --server.port=8084 --spring.range=OT > 8084.txt &
nohup java -jar kong-demo-0.0.1-SNAPSHOT.jar --server.port=8085 --spring.range=OT > 8085.txt &

1、创建cn_upstream及ot_upstream

1.1 创建cn_upstream

[root@min ~]# curl -X POST http://localhost:8001/upstreams \
   --data name=cn_upstream
{
    
    "hash_fallback":"none","hash_fallback_header":null,"hash_fallback_query_arg":null,"hash_fallback_uri_capture":null,"host_header":null,"tags":null,"updated_at":1685570147,"name":"cn_upstream","hash_on":"none","hash_on_header":null,"client_certificate":null,"hash_on_query_arg":null,"hash_on_uri_capture":null,"use_srv_name":false,"slots":10000,"hash_on_cookie":null,"hash_on_cookie_path":"/","healthchecks":{
    
    "active":{
    
    "healthy":{
    
    "interval":0,"successes":0,"http_statuses":[200,302]},"unhealthy":{
    
    "interval":0,"http_failures":0,"timeouts":0,"http_statuses":[429,404,500,501,502,503,504,505],"tcp_failures":0},"http_path":"/","https_sni":null,"type":"http","concurrency":10,"https_verify_certificate":true,"headers":null,"timeout":1},"threshold":0,"passive":{
    
    "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,"http_failures":0,"http_statuses":[429,500,503],"tcp_failures":0},"type":"http"}},"id":"5aae2f66-3fb5-461d-9189-e30fa80a2d61","algorithm":"round-robin","created_at":1685570147}

1.2 创建ot_upstream

[root@min ~]# curl -X POST http://localhost:8001/upstreams   --data name=ot_upstream
{
    
    "hash_fallback":"none","hash_fallback_header":null,"hash_fallback_query_arg":null,"hash_fallback_uri_capture":null,"host_header":null,"tags":null,"updated_at":1685570784,"name":"ot_upstream","hash_on":"none","hash_on_header":null,"client_certificate":null,"hash_on_query_arg":null,"hash_on_uri_capture":null,"use_srv_name":false,"slots":10000,"hash_on_cookie":null,"hash_on_cookie_path":"/","healthchecks":{
    
    "active":{
    
    "healthy":{
    
    "interval":0,"successes":0,"http_statuses":[200,302]},"unhealthy":{
    
    "interval":0,"http_failures":0,"timeouts":0,"http_statuses":[429,404,500,501,502,503,504,505],"tcp_failures":0},"http_path":"/","https_sni":null,"type":"http","concurrency":10,"https_verify_certificate":true,"headers":null,"timeout":1},"threshold":0,"passive":{
    
    "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,"http_failures":0,"http_statuses":[429,500,503],"tcp_failures":0},"type":"http"}},"id":"918d9fec-c274-48df-880d-d522aa0b7482","algorithm":"round-robin","created_at":1685570784}

2、为cn_upstream、ot_upstream添加target

2.1、为cn_upstream添加target

curl -X POST http://localhost:8001/upstreams/cn_upstream/targets \
  --data target='192.168.19.50:8080'
curl -X POST http://localhost:8001/upstreams/cn_upstream/targets \
  --data target='192.168.19.50:8081'
  curl -X POST http://localhost:8001/upstreams/cn_upstream/targets \
  --data target='192.168.19.50:8082'

在这里插入图片描述

2.2、为op_upstream添加target

curl -X POST http://localhost:8001/upstreams/ot_upstream/targets \
  --data target='192.168.19.50:8083'
curl -X POST http://localhost:8001/upstreams/ot_upstream/targets \
  --data target='192.168.19.50:8084'
  curl -X POST http://localhost:8001/upstreams/ot_upstream/targets \
  --data target='192.168.19.50:8085'

3、创建service

3.1、创建cn_service

创建cn_service,并将其host指向cn_upstream

[root@min ~]# curl -i -s -X POST http://localhost:8001/services \
  --data name=cn_service \
  --data host='cn_upstream'
HTTP/1.1 201 Created
Date: Wed, 31 May 2023 22:10:00 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: http://localhost:8002
X-Kong-Admin-Request-ID: sUQcvdGQ4m002ZnQysRNnFaEydvU0edv
vary: Origin
Access-Control-Allow-Credentials: true
Content-Length: 371
X-Kong-Admin-Latency: 9
Server: kong/3.3.0.0-enterprise-edition

{
    
    "connect_timeout":60000,"read_timeout":60000,"protocol":"http","host":"cn_upstream","updated_at":1685571000,"name":"cn_service","retries":5,"write_timeout":60000,"id":"39e44a6e-dfdf-4a32-85ba-db003f424073","tls_verify":null,"port":80,"tls_verify_depth":null,"enabled":true,"client_certificate":null,"path":null,"ca_certificates":null,"created_at":1685571000,"tags":null}

在这里插入图片描述

3.1、创建ot_service

创建ot_service,并将其host指向ot_upstream

 curl -i -s -X POST http://localhost:8001/services \
  --data name=ot_service \
  --data host='ot_upstream'

在这里插入图片描述

4、为cn_service和ot_service创建route

4.1、为cn_service创建route,path为:/cn_service

curl -i -X POST http://localhost:8001/services/cn_service/routes \
  --data 'paths[]=/cn_service' \
  --data name=cn_route

在这里插入图片描述

4.1、为cn_service创建route,path为:/cn_service

curl -i -X POST http://localhost:8001/services/ot_service/routes \
  --data 'paths[]=/ot_service' \
  --data name=ot_route

在这里插入图片描述

5、测试的搭建情况

如果访问192.168.19.50:8000/cn_service/hello将会被代理到cn_upstream上,即会被192.168.19.50:8080/hello、192.168.19.50:8081/hello、192.168.19.50:8082/hello三个服务中的一个处理

如果访问192.168.19.50:8000/ot_service/hello将会被代理到cn_upstream上,即会被192.168.19.50:8083/hello、192.168.19.50:8084/hello、192.168.19.50:8085/hello三个服务中的一个处理

[root@min ~]# curl http://192.168.19.50:8000/cn_service/hello
hello : 8082;range:CN[root@min ~]# curl http://192.168.19.50:8000/cn_service/hello
hello : 8080;range:CN[root@min ~]# curl http://192.168.19.50:8000/cn_service/hello
hello : 8080;range:CN[root@min ~]# curl http://192.168.19.50:8000/cn_service/hello
hello : 8081;range:CN[root@min ~]# curl http://192.168.19.50:8000/cn_service/hello
hello : 8082;range:CN[root@min ~]# 

[root@min ~]# curl http://192.168.19.50:8000/ot_service/hello
hello : 8085;range:OT[root@min ~]# curl http://192.168.19.50:8000/ot_service/hello
hello : 8083;range:OT[root@min ~]# curl http://192.168.19.50:8000/ot_service/hello
hello : 8084;range:OT[root@min ~]# 

6、启用插件

curl -X POST http://localhost:8001/plugins/
–data “name=acl”
–data “config.allow=group1”
–data “config.allow=group2”
–data “config.hide_groups_header=true”

猜你喜欢

转载自blog.csdn.net/zhangshenglu1/article/details/130977934