项目作者: dempeZheng

项目描述 :
基于netty轻量的高性能分布式RPC服务框架
高级语言: JavaScript
项目地址: git://github.com/dempeZheng/forest.git
创建时间: 2015-10-15T10:19:56Z
项目社区:https://github.com/dempeZheng/forest

开源协议:Other

关键词:
"dubbo" "motan" "netty-rpc" "spring"

下载


Forest

License
Build Status

Overview

基于netty轻量的高性能分布式RPC服务框架。简单,易用,高效。

Features

  • 服务端支持多种序列化方式:fastjson,hession,kryo
  • 服务端支持多种压缩方式:gzip,snappy
  • 服务端支持同时基于jersey暴露restful服务
  • 支持注解配置,也支持spring xml配置
  • 支持服务发现服务注册
  • client端支持多种负载均衡策略和容灾策略
  • client内置连接池
  • client支持熔断,一个时间段错误次数达到一定阈值,自动熔断
  • 基于netty 4.x版本实现,高性能(win 8cpu单机8w+)

Protocol

Alt text

Quick Start

Add dependencies to pom.

  1. <dependency>
  2. <groupId>com.zhizus</groupId>
  3. <artifactId>forest-rpc</artifactId>
  4. <version>0.0.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.zhizus</groupId>
  8. <artifactId>forest-common</artifactId>
  9. <version>0.0.2</version>
  10. </dependency>

1.定义接口

通过注解@ServiceProvider暴露服务,通过@MethodProvider暴露方法默认配置,如:压缩方式,序列化方式,客户端超时时间

  1. @ServiceProvider(serviceName = "sampleService", haStrategyType = HaStrategyType.FAIL_FAST,
  2. loadBalanceType = LoadBalanceType.RANDOM, connectionTimeout = Constants.CONNECTION_TIMEOUT)
  3. public interface SampleService {
  4. @MethodProvider(methodName = "say")
  5. String say(String str);
  6. @MethodProvider(methodName = "echo", serializeType = SerializeType.Hession2, compressType = CompressType.None)
  7. String echo(String msg);
  8. }

2.实现接口

基于注解@ServiceExport发布服务,基于注解 @MethodExport发布方法,

  1. @Path("/sample")
  2. @ServiceExport
  3. public class SampleServiceImpl implements SampleService {
  4. /**
  5. * 支持jersey,可以通过配置打开,同时启动http服务
  6. *
  7. * @param str
  8. * @return
  9. */
  10. @Path("/hello/{str}")
  11. @GET
  12. @Produces("text/plain")
  13. @MethodExport
  14. @Rate(2)
  15. @Override
  16. public String say(@PathParam("str") String str) {
  17. return "say " + str;
  18. }
  19. @Interceptor("metricInterceptor")
  20. @MethodExport
  21. @Override
  22. public String echo(String msg) {
  23. return "echo>>> " + msg;
  24. }
  25. }

3.服务端开发

spring context 配置:

application.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns="http://www.springframework.org/schema/beans" xmlns:forest="http://api.zhizus.com/schema/forest"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd
  9. http://api.zhizus.com/schema/forest http://api.zhizus.com/schema/forest.xsd">
  10. <context:component-scan base-package="com.zhizus.forest.demo"></context:component-scan>
  11. <context:property-placeholder location="classpath:/*.properties"></context:property-placeholder>
  12. <forest:registry id="registry" regProtocol="local" name="registry" address="127.0.0.1:2181"></forest:registry>
  13. <!--<forest:registry id="registry" regProtocol="zookeeper" name="registry" address="127.0.0.1:2181"></forest:registry>-->
  14. <forest:server id="forestServer" registry="registry" startHttpServer="true"></forest:server>
  15. <forest:interceptors>
  16. <forest:interceptor id="metricInterceptor" class="com.zhizus.forest.support.MetricInterceptor" auto-match="public *(*)"></forest:interceptor>
  17. </forest:interceptors>
  18. </beans>

Server开发

  1. public class SampleServer {
  2. public static void main(String[] args) throws Exception {
  3. new ClassPathXmlApplicationContext(new String[]{"application.xml"});
  4. }
  5. }

4.客户端开发

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns="http://www.springframework.org/schema/beans"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:forest="http://api.zhizus.com/schema/forest"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://api.zhizus.com/schema/forest http://api.zhizus.com/schema/forest.xsd">
  10. <context:component-scan base-package="com.zhizus.forest.demo.client"></context:component-scan>
  11. <forest:registry id="registry" regProtocol="local" name="registry" address="127.0.0.1:9999"></forest:registry>
  12. <forest:referer id="sampleService" interface="com.zhizus.forest.demo.api.SampleService" registry="registry">
  13. <forest:method name="echo" timeout="5000" serializeType="Fastjson"></forest:method>
  14. <forest:method name="say" timeout="5000" serializeType="Fastjson" compressType="GZIP"></forest:method>
  15. </forest:referer>
  16. </beans>
  1. ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application-client.xml"});
  2. SampleService bean = (SampleService) context.getBean("sampleService");
  3. String test = bean.say("hello");

Console输出

  1. 23:10:10.295 [pool-1-thread-1] INFO MetricInterceptor 34 - methodName:/sampleService/say, current tps:83342, avgTime:0, maxTime:63, minTime:0
  2. 23:10:11.298 [pool-1-thread-1] INFO MetricInterceptor 34 - methodName:/sampleService/say, current tps:86271, avgTime:0, maxTime:63, minTime:0
  3. 23:10:12.295 [pool-1-thread-1] INFO MetricInterceptor 34 - methodName:/sampleService/say, current tps:86063, avgTime:0, maxTime:63, minTime:0
  4. 23:10:13.295 [pool-1-thread-1] INFO MetricInterceptor 34 - methodName:/sampleService/say, current tps:84305, avgTime:0, maxTime:63, minTime:0

更多示例

Documents

TODO

  • 跨语言协议支持
  • 服务治理管理后台

License

Forest is released under the Apache License 2.0.