Skip to content

cwdtom/gateway

Repository files navigation

Gateway

Version Build Status License

Overview

  • API proxy gateway base on netty and okHttp.
  1. Support http protocol.
  2. Support https protocol.
  3. Support cors protocol.
  4. Support local flow limits base on token bucket.
  5. Every proxy has independent thread pool.
  6. Load balance
    1. RandomLoadBalance
    2. ConsistentHash
    3. RoundRobinLoadBalance
    4. LeastActiveLoadBalance
  7. Support fuse service which is offline,then retest service in single thread,when it is reachable,take it online.
  8. Use epoll model in Linux,other uses nio model.
  9. Static file mapping.
  10. Support consul auto service discovery.if it is enable,origin mapping config will be failure,every node has same weight.
  11. Support zookeeper auto service discovery.If it is enable,origin mapping config will be failure,every node has same weight,zk priority is higher than consul.
  12. Print detail log in developer mode,and print log which level is higher than 'warn' in production environment.

Download

Download the latest release

Usage

  • start jar package

    java -jar gateway.jar -c /root/config.json
    1. -v version
    2. -c json config file path
    3. -h show help info
  • config.json

    {
      "mode": "dev",
      "http": {
        "port": 8080,
        "redirectHttps": false
      },
      "https": {
        "enable": false,
        "port": 8081,
        "keyPwd": "123456",
        "keyPath": "/Users/xxx/workspace/gateway/ssl/cwd.keystore"
      },
      "threadPool": {
        "core": 150,
        "max": 200,
        "timeout": 5000
      },
      "mapping": {
        "mode": "com.github.cwdtom.gateway.environment.lb.ConsistentHash",
        "list": {
          "127.0.0.1:8080": [
            {
              "url": "123.125.115.110:80",
              "weight": 200
            },
            {
              "url": "220.181.57.216:80",
              "weight": 100
            }
          ]
        }
      },
      "static": {
        "localhost:8080": "/Users/xxx/workspace/gateway"
      },
      "cors": {
        "enable": true,
        "allowMethods": "GET, POST, HEAD, PUT, PATCH, DELETE",
        "whiteList": []
      },
      "flowLimits": {
        "enable": true,
        "timeout": 500,
        "rate": 5,
        "maxSize": 200
      },
      "filter": {
        "before": [
          "com.github.cwdtom.gateway.filter.BeforeTestFilter"
        ],
        "after": [
          "com.github.cwdtom.gateway.filter.AfterTestFilter"
        ]
      },
      "consul": {
        "enable": true,
        "host": "192.168.0.236:8500",
        "mapping": {
          "test": [
            "localhost:8080"
          ]
        }
      },
      "zk": {
        "enable": true,
        "host": "127.0.0.1:2181",
        "mapping": {
          "test": [
            "localhost:8080"
          ]
        }
     }
    }
    1. mode: If it is not 'dev',nothing but log level higher than 'warn' print and shutdown resource leak detector.
    2. http: http config.
      1. port: http service port.
      2. redirectHttps: Whether need redirect https or not.
    3. https: https config.
      1. enable: enable https service.
      2. port: https service port.
      3. keyPwd: SSL certificate password.
      4. keyPath: SSL certificate file path.
    4. threadPool: single proxy thread pool config.
      1. core: core thread count.
      2. max: max thread count.
      3. timeout: thread pool timeout.
    5. mapping: mapping config,every host mapping multiple proxy address.
      1. mode: load balance algorithm,default RandomLoadBalance.
      2. list: mapping list.
        1. key: host.
        2. value: proxy address list.
          1. url: proxy address.
          2. weight: weight.
    6. static: static file mapping.
      1. key: host.
      2. value: local folder.
    7. cors: cors config.
      1. enable: enable cors.
      2. allowMethods: allowed request methods.
      3. whiteList: cors white list,when white list is empty and cors enable,allow all requested.
    8. flowLimits: flow limits config.
      1. enable: enable flow limits.
      2. rate: token production rate,unit millisecond.
      3. maxSize: token bucket size.
    9. filter: request filter,the execution order is same as this config.
      1. before: pre filter.
      2. after: post filter.
    10. consul: consul config.
      1. enable: enable consul,if enable origin mapping will be failure.
      2. host: consul address.
      3. mapping: service mapping.
        1. key: service name(spring.application.name).
        2. value: host list
    11. zk: zookeeper config
      1. enable: enable zookeeper,if enable origin mapping will be failure.
      2. host: zookeeper address
      3. mapping: service mapping.
        1. key: service name(spring.application.name).
        2. value: host list

CUSTOMIZE

  • Load balance algorithm:Support customize algorithm.Algorithm class need extends UrlMapping class.Father class has proxy mapping map variable.

    public class RandomLoadBalance extends UrlMapping {
        @Override
        public Mapper getLoadBalance(String host, String ip) {
            // do something
        }
    }
  • filter:Support customize filter.Filter class need implements BeforeFilter interface or AfterFilter interface.

    • pre filter,return whether continue or not.
    public class TestFilter implements BeforeFilter {
        @Override
        public boolean filter(FullHttpRequest request, byte[] content) {
            // do something
            return false;
        }
    }
    • post filter.
    public class TestFilter implements AfterFilter {  
        @Override
        public void filter(FullHttpResponse response) {
            // do something
        }
    }

About

API proxy gateway base on netty and okHttp.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages