Skip to content
/ sgw Public

A simple non-blocking micro-service gateway built on Netty. Support Zookeeper for service discovery and Thrift protocol

License

Notifications You must be signed in to change notification settings

hongjic/sgw

Repository files navigation

SGW

SGW is a simple non-blocking micro-service gateway built upon Netty, it stands between different types of http clients and all micro-service backends (mostly using faster RPC protocols).

The main purpose of this project is to build a protocol transition and request dispatching layer, and aims to handle large throughput (which results in using Netty as the base framework).

Other features include:

  • Service discovery/load balancing
  • Pooled connections
  • Zuul style Filters
  • Spring style router configuration

中文文档

Dependencies

  • use Gradle to build project.
  • Netty 4.1
  • Thrift 0.10.0
  • CuratorFramework

Target functions

  1. Non-blocking IO

  2. Dynamic Routers

    • Router configuration using "routing.yaml"

       thriftServices:
         - http: POST /echo # http请求
       	convertor: # 解析请求和生成响应的类
       	service: echoservice # 下游服务名(zookeeper中的名字)
           method: echo # 下游服务方法名
           clazz: # thrift生成的类名
         - http: ....
         ...
       xxxServices:
      
      
    • Spring style router configuration

       @ThriftRouter(http = {"POST", "/echo"}, service = "echoservice", method = "echo", args = EchoService.echo_args.class, result = EchoService.echo_result.class)
      public class EchoConvertor{
       	
       	@RequestParser
       	public Object[] parse(FullHttpRequest request) {
       		return new Object[] {request.content().toString(CharsetUtil.UTF_8)};
       	}
      
       	@ResponseGenerator
       	public String generate(String result) {
       		return result;
       	}
       }
      
    • Routing templates:

       	@RequestParser
          	public Object[] parse(FullHttpRequest request, @PathVar("id") int id) {
          		...
          	}
      
    • Routing configuration hot replacement (under development)

  3. protocol conversion from HTTP to Thrift

    currently hard coded, plan to enable configuration on thrift protocols.

    • protocol:TMultiplexedProtocol, TCompactProtocol
    • transport: TFramedTransport

    Also planed to provide user interfaces to help support more protocols.

  4. service discovery/load balancing

    • cache service node metadata in memory
    • receive Zookeeper update in real time
    • using round-robin load balancing strategy. (plan to support customization)
  5. filters

    • preRouting filters: filter requests when requests arrive.
    • postRouting filters: filter responses before sending response.
    • routing filters: filter requests before sending to backend services.
  6. circuit breaker (under development)

    behave like hystrix.

    • built purely on filters

Request - Response lifecycle

  1. decoding http requests from network
  2. pre-routing filters
  3. Routing: find the matched backend service and use load balancing to get the target service node.
  4. routing filter
  5. convert http request to thrift request (@RequestParser)
  6. get a service connection (either established or new) and send the encoded thrift request to the backend.
  7. decode thrift response
  8. convert thrift response back to http response (@ResponseGenerator)
  9. post-routing filter
  10. send http response back to client.

About Extension

Demo

sgw.demo.DemoServer

About

A simple non-blocking micro-service gateway built on Netty. Support Zookeeper for service discovery and Thrift protocol

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages