2

.net core微服务之网关 - 六指狂魔肉球猫

 7 months ago
source link: https://www.cnblogs.com/AsprosL/p/18020704
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

.net core微服务之网关

随笔- 18  文章- 1  评论- 32  阅读-

58892 

一:apisix

  doc:https://apisix.apache.org/zh/docs/apisix/getting-started/README/

  github:https://github.com/apache/apisix

二:Kong

  github:https://github.com/Kong/kong

三:Ocelot

  github:https://github.com/ThreeMammals/Ocelot

四:janus

  github:https://github.com/motiv-labs/janus

前置条件:docker,yaml

microservice.yaml

ContractedBlock.gifExpandedBlockStart.gif

View Code

目前使用docker启动apisix,需要依赖etcd,

    etcd:
        container_name: etcd
        hostname: etcd
        image: bitnami/etcd
        volumes:
          - ./etcd/data:/bitnami/etcd
        environment:
          ETCD_ENABLE_V2: "true"
          ALLOW_NONE_AUTHENTICATION: "yes"
          ETCD_ADVERTISE_CLIENT_URLS: "http://etcd:2379" #https://github.com/apache/apisix-dashboard/issues/2756 需要更换为host域名不能使用0.0.0.0
          ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
        ports:
          - "2379:2379/tcp"
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.8

    apisix:
        container_name: apisix
        hostname: apisix
        image: apache/apisix        
        volumes:
          - ./apisix/log:/usr/local/apisix/logs
          - ./apisix/conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
        depends_on:
          - etcd
        ports:
          - "9088:9088/tcp"
          - "9180:9180/tcp"
          - "127.0.0.1:9090:9090/tcp"
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.9
    
    apisix-dashboard:
        container_name: apisix-dashboard
        image: apache/apisix-dashboard
        depends_on:
          - etcd
        ports:
          - "9188:9188"
        volumes:
          - ./apisix/conf/dashboard.yaml:/usr/local/apisix-dashboard/conf/conf.yaml
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.10

使用apisix dashboard的时候 会出现一个bug,需要在启动etcd的时候设置ETCD_ADVERTISE_CLIENT_URLS为host域名 而不能使用0.0.0.0

apisix的config yaml 

ContractedBlock.gifExpandedBlockStart.gif

View Code

dashboard的config yaml

ContractedBlock.gifExpandedBlockStart.gif

View Code

在文件夹下启动

docker-compose -f microservice.yaml up
620475-20240219105128596-759291405.png
620475-20240219105156431-586132268.png

 本地打开 http://localhost:9188 使用admin ,admin 登录

配置路由信息

620475-20240219105655728-1129978654.png
620475-20240219110018543-71048485.png
620475-20240219110043540-1853638862.png

 这里使用的是nacos作为服务发现,具体查看nacos配置

620475-20240219110145869-620183617.png

 启动以8083端口的服务

dotnet run --urls=http://*:8083
620475-20240219110304296-1837371224.png

 打开浏览器调试下接口

620475-20240219110330905-1496499710.png

 接下来使用网关请求

620475-20240219110359499-1873945067.png

 再启动以8084端口的服务

620475-20240219110519671-1660801782.png

nacos中出现了两个实例

620475-20240219110531856-1997097112.png

继续使用网关请求

在log中的access.log 可以查看到网关请求到不同端口的服务

620475-20240219110949635-1755237336.png
620475-20240219110939571-2073508982.png

 几个注意点:

1.etcd的ETCD_ADVERTISE_CLIENT_URLS 需要更换为host域名不能使用0.0.0.0

620475-20240219111440357-1512910450.png

 2.apisix的nacos配置在 config.yaml中的discovery

620475-20240219111559162-582087727.png

尽量使用host名称 

620475-20240219111627326-272558585.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK