kong整合Cosul实践(3)

1. 准备工作:

  • 需要.netcore或者java程序一套,引入consul第三方包

    我这里搭建了一个.netcore的webapi项目,引入Consul第三方包,网上可查资料一堆
    image

    image

  • 环境需要kong,konga管理平台,consul

    image

2. 运行程序,注册服务到consul端

  • 找到项目调试debug目录(E:\PersonalProject\Netcore\coreApp3.1\coreApp3.1\bin\Debug\netcoreapp3.1>)

    image

    使用powershell命令,启动程序:

    启动APP1:
    dotnet coreApp3.1.dll --urls="http://:6000" -- ip="192.168.1.6" --port=6000 --weight=10
    启动APP2:
    dotnet coreApp3.1.dll --urls="http://
    :6001" -- ip="192.168.1.6" --port=6001 --weight=10

    image

    进入consul客户端,查看注册services(http://192.168.231.132:8500/)
    image

    使用postman请求webapi,返回正常
    image

3. 通过konga管理工具,配置kong网关信息

  • 查找Consul DNS轮询服务:
      dig @192.168.231.132 -p 8600 xujk.service.dc1.consul ANY
      dig @192.168.231.132 -p 8600 xujk.service.consul ANY
    

    备注:xujk.service.dc1.consul
    格式:【servicename】.【nodename】.【datacenter】.consul

    image

  • 进入Konga管理端,添加Services

    image
    image

  • 给services添加Route信息

    image
    image

    通过http://192.168.231.132:8000/health请求,查看结果
    image

    备注:实际上,Kong网关帮我们路由到了http://192.168.1.6/api/health/getTest

4. 上面操作只路由到了单个api,下面配置路由到项目,可通过项目的api路径访问

  • 添加一个service
    image
    备注:其中的xujk.service.consul,就是上面我们用dig命令查找的服务名称
    image
  • 添加一个Route
    image
  • 通过Kong网关路由访问api:
    通过url:http://192.168.231.132:8000/dns/api/health/getTest访问,其中/api/health/getTest是webapi项目中的路由
    image
  • 测试端口6000节点挂机
    image
  • 通过postman请求,发现还是可以访问的,因为端口6001的api还提供服务
    image
  • 如果两个节点都挂掉,那会提示找不到服务的提示
    image
  • 访问该app下所有的路由,可以通过http://192.168.231.132:8000/dns/ + app项目的路由地址访问
posted @ 2021-03-26 18:26  xjk27400861  阅读(250)  评论(1编辑  收藏  举报