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
- use Gradle to build project.
- Netty 4.1
- Thrift 0.10.0
- CuratorFramework
-
Non-blocking IO
-
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)
-
-
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.
-
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)
-
filters
- preRouting filters: filter requests when requests arrive.
- postRouting filters: filter responses before sending response.
- routing filters: filter requests before sending to backend services.
-
circuit breaker (under development)
behave like hystrix.
- built purely on filters
- decoding http requests from network
- pre-routing filters
- Routing: find the matched backend service and use load balancing to get the target service node.
- routing filter
- convert http request to thrift request (
@RequestParser
) - get a service connection (either established or new) and send the encoded thrift request to the backend.
- decode thrift response
- convert thrift response back to http response (
@ResponseGenerator
) - post-routing filter
- send http response back to client.