首页 > 其他分享 >.net core微服务之网关

.net core微服务之网关

时间:2024-02-19 11:23:18浏览次数:27  
标签:core 网关 0.0 nacos rabbitmq etcd MYSQL net apisix

网关:

一: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

version: "3.8"
networks:
  caseor_bridge:
    driver: bridge
    ipam:
      config:
        - subnet: 172.0.10.0/24
  
services:

    mysql:
        container_name: mysql
        image: mysql
        privileged: true
        command: --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --max_connections=2000 --max_allowed_packet=64M
        environment:
          - TZ=Asia/Shanghai
          - MYSQL_ROOT_PASSWORD=123456
        volumes:
          - ./mysql:/var/lib/mysql
        ports:
          - "3306:3306"
        healthcheck:
          test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
          interval: 5s
          timeout: 10s
          retries: 10
        networks:
          caseor_bridge:
            ipv4_address: 172.0.10.3

    redis:
        image: redis
        container_name: "redis"
        ports:
            - "6379:6379"
        volumes:
            - ./redis/data:/data
            - ./redis/conf:/usr/local/etc/redis
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.4

    nacos1:
        container_name: nacos1
        hostname: nacos1
        image: nacos/nacos-server
        environment:
            - MODE=cluster
            - PREFER_HOST_MODE=hostname
            - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
            - SPRING_DATASOURCE_PLATFORM=mysql
            - MYSQL_SERVICE_HOST=172.0.10.3
            - MYSQL_SERVICE_PORT=3306
            - MYSQL_SERVICE_USER=root
            - MYSQL_SERVICE_PASSWORD=123456
            - MYSQL_SERVICE_DB_NAME=nacos
            - MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true  
            - JVM_XMS=128m
            - JVM_XMX=128m
            - JVM_XMN=128m
        volumes: 
            - ./nacos/cluster-logs/nacos1:/home/nacos/logs
            - ./nacos/init.d:/home/nacos/init.d
        ports: 
            - 8850:8848
            - 7850:7848     
            - 9870:9848
            - 9852:9849
        depends_on:
          - mysql
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.5

    nacos2:
        container_name: nacos2
        hostname: nacos2
        image: nacos/nacos-server
        environment:             
            - MODE=cluster
            - PREFER_HOST_MODE=hostname
            - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
            - SPRING_DATASOURCE_PLATFORM=mysql
            - MYSQL_SERVICE_HOST=172.0.10.3
            - MYSQL_SERVICE_PORT=3306
            - MYSQL_SERVICE_USER=root
            - MYSQL_SERVICE_PASSWORD=123456
            - MYSQL_SERVICE_DB_NAME=nacos
            - MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true  
            - JVM_XMS=128m
            - JVM_XMX=128m
            - JVM_XMN=128m
        volumes: 
            - ./nacos/cluster-logs/nacos2:/home/nacos/logs
            - ./nacos/init.d:/home/nacos/init.d
        ports: 
            - 8849:8848
            - 7849:7848     
            - 9869:9848
            - 9851:9849
        depends_on:
          - mysql
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.6

    nacos3:
        container_name: nacos3
        hostname: nacos3
        image: nacos/nacos-server
        environment:             
            - MODE=cluster
            - PREFER_HOST_MODE=hostname
            - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
            - SPRING_DATASOURCE_PLATFORM=mysql
            - MYSQL_SERVICE_HOST=172.0.10.3
            - MYSQL_SERVICE_PORT=3306
            - MYSQL_SERVICE_USER=root
            - MYSQL_SERVICE_PASSWORD=123456
            - MYSQL_SERVICE_DB_NAME=nacos
            - MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true  
            - JVM_XMS=128m
            - JVM_XMX=128m
            - JVM_XMN=128m
        volumes: 
            - ./nacos/cluster-logs/nacos3:/home/nacos/logs
            - ./nacos/init.d:/home/nacos/init.d
        ports: 
            - 8848:8848
            - 7848:7848     
            - 9848:9848
            - 9849:9849
        depends_on:
          - mysql
        networks:
          caseor_bridge:
                ipv4_address: 172.0.10.7

    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

    rabbitmq01:
      image: rabbitmq
      container_name: rabbitmq01
      hostname: rabbitmq01
      environment:
        - TZ=Asia/Shanghai
        - RABBITMQ_DEFAULT_USER=root   #自定义登录账号
        - RABBITMQ_DEFAULT_PASS=123456 #自定义登录密码
        - RABBITMQ_ERLANG_COOKIE='secret_cookie'
      ports:
        - "15672:15672"
        - "5672:5672"
      volumes:
        - ./rabbitmq/mq1/data:/var/lib/rabbitmq
        - ./rabbitmq/mq1/conf:/etc/rabbitmq
      command:  bash -c "sleep 10; rabbitmq-server;"
      networks:
        caseor_bridge:
            ipv4_address: 172.0.10.11

    rabbitmq02:
      image: rabbitmq
      container_name: rabbitmq02
      hostname: rabbitmq02
      environment:
        - TZ=Asia/Shanghai
        - RABBITMQ_DEFAULT_USER=root   #自定义登录账号
        - RABBITMQ_DEFAULT_PASS=123456 #自定义登录密码
        - RABBITMQ_ERLANG_COOKIE='secret_cookie'
      ports:
        - "15673:15672"
        - "5673:5672"
      depends_on:
          - rabbitmq01
      volumes:
        - ./rabbitmq/mq2/data:/var/lib/rabbitmq
        - ./rabbitmq/mq2/conf:/etc/rabbitmq
      command:  bash -c "sleep 10; rabbitmq-server;"
      networks:
        caseor_bridge:
            ipv4_address: 172.0.10.12

    rabbitmq03:
      image: rabbitmq
      container_name: rabbitmq03
      hostname: rabbitmq03
      environment:
        - TZ=Asia/Shanghai
        - RABBITMQ_DEFAULT_USER=root   #自定义登录账号
        - RABBITMQ_DEFAULT_PASS=123456 #自定义登录密码
        - RABBITMQ_ERLANG_COOKIE='secret_cookie'
      ports:
        - "15674:15672"
        - "5674:5672"
      depends_on:
          - rabbitmq01
      volumes:
        - ./rabbitmq/mq3/data:/var/lib/rabbitmq
        - ./rabbitmq/mq3/conf:/etc/rabbitmq
      command:  bash -c "sleep 10; rabbitmq-server;"
      networks:
        caseor_bridge:
            ipv4_address: 172.0.10.13

# 开启web管理 
# rabbitmq-plugins enable rabbitmq_management 

# # 加入rabbitmq集群

# # rabbit1
# rabbitmqctl stop_app
# rabbitmqctl reset
# rabbitmqctl start_app

# # rabbit2
# rabbitmqctl stop_app
# rabbitmqctl reset
# rabbitmqctl join_cluster --ram rabbit@rabbit1
# rabbitmqctl start_app

# # rabbit3
# rabbitmqctl stop_app
# rabbitmqctl reset
# rabbitmqctl join_cluster --ram rabbit@rabbit1
# rabbitmqctl start_app
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 

apisix:
    node_listen:
        - port: 9088
    enable_ipv6: true
    enable_control: true
    control:
        ip: "[::]"
        port: 9090
discovery:
  nacos:
    host:
      - "http://172.0.10.7:8848"
deployment:
    role: traditional
    role_traditional:
        config_provider: etcd
    admin:
        admin_listen:
            port: 9180
        allow_admin:
            - 0.0.0.0/0
        admin_key:
            - name: "admin"
              key: b848941cd4e1003f2f961a7786ecf75f
              role: admin
            - name: "viewer"
              key: dd3bc5bde63f272f554b91336bfcfcb3
              role: viewer
    etcd:
       host:
           - http://etcd:2379
       prefix: /apisix
       timeout: 30
#plugin_attr:
#    prometheus:
#        export_addr:
#            ip: "0.0.0.0"
#            port: 9091
#END
View Code

dashboard的config yaml

conf:
  listen:
    host: 0.0.0.0     # `manager api` listening ip or host name
    port: 9188          # `manager api` listening port
  allow_list:           # If we don't set any IP list, then any IP access is allowed by default.
    - 0.0.0.0/0
  etcd:
    endpoints:          # supports defining multiple etcd host addresses for an etcd cluster
      - "http://etcd:2379"
                          # yamllint disable rule:comments-indentation
                          # etcd basic auth info
    # username: "root"    # ignore etcd username if not enable etcd auth
    # password: "123456"  # ignore etcd password if not enable etcd auth
    mtls:
      key_file: ""          # Path of your self-signed client side key
      cert_file: ""         # Path of your self-signed client side cert
      ca_file: ""           # Path of your self-signed ca cert, the CA is used to sign callers' certificates
    # prefix: /apisix     # apisix config's prefix in etcd, /apisix by default
  log:
    error_log:
      level: warn       # supports levels, lower to higher: debug, info, warn, error, panic, fatal
      file_path:
        logs/error.log  # supports relative path, absolute path, standard output
                        # such as: logs/error.log, /tmp/logs/error.log, /dev/stdout, /dev/stderr
    access_log:
      file_path:
        logs/access.log  # supports relative path, absolute path, standard output
                         # such as: logs/access.log, /tmp/logs/access.log, /dev/stdout, /dev/stderr
                         # log example: 2020-12-09T16:38:09.039+0800    INFO    filter/logging.go:46    /apisix/admin/routes/r1 {"status": 401, "host": "127.0.0.1:9000", "query": "asdfsafd=adf&a=a", "requestId": "3d50ecb8-758c-46d1-af5b-cd9d1c820156", "latency": 0, "remoteIP": "127.0.0.1", "method": "PUT", "errs": []}
authentication:
  secret:
    secret              # secret for jwt token generation.
                        # NOTE: Highly recommended to modify this value to protect `manager api`.
                        # if it's default value, when `manager api` start, it will generate a random string to replace it.
  expire_time: 3600     # jwt token expire time, in second
  users:                # yamllint enable rule:comments-indentation
    - username: admin   # username and password for login `manager api`
      password: admin
    - username: user
      password: user

plugins:                          # plugin list (sorted in alphabetical order)
  - api-breaker
  - authz-keycloak
  - basic-auth
  - batch-requests
  - consumer-restriction
  - cors
  # - dubbo-proxy
  - echo
  # - error-log-logger
  # - example-plugin
  - fault-injection
  - grpc-transcode
  - hmac-auth
  - http-logger
  - ip-restriction
  - jwt-auth
  - kafka-logger
  - key-auth
  - limit-conn
  - limit-count
  - limit-req
  # - log-rotate
  # - node-status
  - openid-connect
  - prometheus
  - proxy-cache
  - proxy-mirror
  - proxy-rewrite
  - redirect
  - referer-restriction
  - request-id
  - request-validation
  - response-rewrite
  - serverless-post-function
  - serverless-pre-function
  # - skywalking
  - sls-logger
  - syslog
  - tcp-logger
  - udp-logger
  - uri-blocker
  - wolf-rbac
  - zipkin
  - server-info
  - traffic-split
View Code

在文件夹下启动

docker-compose -f microservice.yaml up

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

配置路由信息

 

 

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

 启动以8083端口的服务

dotnet run --urls=http://*:8083

 打开浏览器调试下接口

 接下来使用网关请求

 再启动以8084端口的服务

nacos中出现了两个实例

 

继续使用网关请求

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

 

 

 几个注意点:

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

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

 

尽量使用host名称 

标签:core,网关,0.0,nacos,rabbitmq,etcd,MYSQL,net,apisix
From: https://www.cnblogs.com/AsprosL/p/18020704

相关文章

  • 二机制安装Kubernetes 1.29 高可用集群(5)--node节点配置
    1.1所有k8s-node节点安装cri-dockerrpm-ivhcri-dockerd-0.3.10-3.el8.x86_64.rpm1.2修改cri-docker启动文件,将默认的pod启动镜像地址修改为国内仓库源#vi/usr/lib/systemd/system/cri-docker.service将如下部分ExecStart=/usr/bin/cri-dockerd--container-runtime-en......
  • 二机制安装Kubernetes 1.29 高可用集群(4)--master节点配置
    1.1在k8s-master01上解压kubernetes可执行文件到/usr/local/bin目录tar-zxfkubernetes-server-linux-amd64.tar.gz--strip-components=3-C/usr/local/binkubernetes/server/bin/kube{let,ctl,-apiserver,-controller-manager,-scheduler,-proxy}注:--strip-components=3......
  • 二机制安装Kubernetes 1.29 高可用集群(3)--etcd集群配置
    1.在所有etcd节点解压安装包tar-zxfetcd-v3.5.12-linux-amd64.tar.gzcpetcd-v3.5.12-linux-amd64/etcd/usr/local/bin/&&cpetcd-v3.5.12-linux-amd64/etcdctl/usr/local/bin/#查看版本信息#etcdctlversionetcdctlversion:3.5.12APIversion:3.52.1在所有et......
  • 二机制安装Kubernetes 1.29 高可用集群(2)--haproxy节点配置
    1.1在所有haproxy节点安装haproxyyum-yinstallhaproxy1.2在所有haproxy节点创建haproxy配置文件cat>/etc/haproxy/haproxy.cfg<<EOFglobalmaxconn3000ulimit-n16384log127.0.0.1local0errstatstimeout30sde......
  • 二机制安装Kubernetes 1.29 高可用集群(1)--系统初始配置
    软件环境描述说明:OS:openEuler22.03LTSSP3kubernetes:1.29.2etcd:3.5.12calico:3.27.0cri-docker:0.3.10cfssl/cfssljson/cfssl-certinfo:1.64haproxy:2.2.6keepalived:2.2.4网段规划:物理主机:192.168.83.0/24      ServiceIP:10.66.0.0/16   PodIP:172.31.0.0......
  • 二机制安装Kubernetes 1.29 高可用集群(6)--calico网络组件和CoreDNS配置
    1.部署Calico网络组件1.1k8s-master节点上下载calico的创建文件下载地址:https://docs.tigera.io/calico/latest/getting-started/kubernetes/quickstartwgethttps://raw.githubusercontent.com/projectcalico/calico/v3.27.2/manifests/tigera-operator.yamlwgethttps://ra......
  • .NET周刊【2月第1期 2024-02-04】
    祝大家新年快乐,龙年大吉~国内文章C#/.NET/.NETCore优秀项目和框架2024年1月简报https://www.cnblogs.com/Can-daydayup/p/18000401本文介绍了公众号“追逐时光者”定期分享的C#/.NET/.NETCore优秀项目和框架,包括项目介绍、功能特点、使用方式和功能截图,并提供了源码地址。文......
  • dotnet 异步编程
    异步与多线程是不同的概念异步并不意味着多线程,单线程同样可以异步。异步默认借助线程池。多线程经常会有阻塞的操作,而异步要求不阻塞。异步与多线程适用场景不同多线程:适合CPU密集型操作适合长期运行的任务线程的创建与销毁的开销是比较大的提供更底层的控制,操作线程、......
  • 从零开始的 dbt 入门教程 (dbt core 开发进阶篇)
    引在上一篇文章中,我们花了专门的篇幅介绍了dbt更多实用的命令,那么我们继续按照之前的约定来聊dbt中你可能会遇到的疑惑以及有用的概念,如果你是dbt初学者,我相信如下知识点一定会对你有极大的帮助:了解dbt_project配置文件,以及不同字符的作用了解dbt工程化,为dev以及......
  • Netty
    Netty官网:https://netty.io/Netty是一个NIO异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。Java的NIO和Netty对比NIO缺点:NIO的类库和API繁杂,学习成本高,你需要熟练掌握Selector、ServerSocketChannel、SocketChannel、ByteBuffer等。需要......