项目作者: GoldLen

项目描述 :
基于netty&spring ioc实现的轻量分布式RPC框架
高级语言: Java
项目地址: git://github.com/GoldLen/forest.git
创建时间: 2016-12-09T03:58:55Z
项目社区:https://github.com/GoldLen/forest

开源协议:Other

下载


Forest

License
Build Status

Overview

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

Features

  • 服务端支持多种序列化方式:fastjson,hession,kryo
  • 服务端支持多种压缩方式:gzip,snappy
  • 服务端可根据group进行线程隔离,支持基于spring对不同的group配置不同的业务线程池
  • 支持注解配置,也支持spring xml配置
  • 支持服务发现服务注册
  • client端支持多种负载均衡策略和容灾策略
  • client内置连接池
  • 基于netty 4.x版本实现,高性能(win 8cpu单机8w+)

Protocol

Quick Start

1.定义接口

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

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

2.实现接口

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

  1. @ServiceExport
  2. public class SampleServiceImpl implements SampleService {
  3. @MethodExport
  4. @Rate(2)
  5. @Override
  6. public String say(String str) {
  7. return "say " + str;
  8. }
  9. @Interceptor("metricInterceptor")
  10. @MethodExport
  11. @Override
  12. public String echo(String msg) {
  13. return "echo " + msg;
  14. }
  15. }

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"
  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. <context:component-scan base-package="com.zhizus.forest.demo"></context:component-scan>
  10. <bean id="metricInterceptor" class="com.zhizus.forest.core.interceptor.MetricInterceptor"></bean>
  11. <bean id="forestServer" class="com.zhizus.forest.support.spring.ForestServerBean"></bean>
  12. </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.客户端开发

基于spring配置

application-client.xml

可以使用api注解暴露的默认配置,也可以通过spring为每个方法定义配置

  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:util="http://www.springframework.org/schema/util"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  10. <context:component-scan base-package="com.zhizus.forest.demo.client"></context:component-scan>
  11. <bean id="methodConfig" class="com.zhizus.forest.common.config.MethodConfig">
  12. <property name="compressType">
  13. <util:constant static-field="None"></util:constant>
  14. </property>
  15. <property name="serializeType">
  16. <util:constant static-field="Fastjson"></util:constant>
  17. </property>
  18. <property name="timeout" value="5000"></property>
  19. </bean>
  20. <bean id="sampleServiceProxy" class="com.zhizus.forest.support.spring.ForestProxyFactoryBean">
  21. <property name="serviceInterface" value="com.zhizus.forest.demo.api.SampleService"></property>
  22. <!--methodConfMap如果不配置,则使用接口方法注解上面的配置-->
  23. <property name="methodConfigMap">
  24. <map>
  25. <entry key="echo" value-ref="methodConfig"></entry>
  26. <entry key="say" value-ref="methodConfig"></entry>
  27. </map>
  28. </property>
  29. </bean>
  30. </beans>
  1. public class SampleClient {
  2. public static void main(String[] args) throws InterruptedException {
  3. ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application-client.xml"});
  4. SampleService bean = (SampleService) context.getBean("sampleServiceProxy");
  5. String test = bean.say("hello");
  6. System.out.println(test);
  7. }
  8. }

基于默认注解

  1. SampleService sampleService = Forest.from(SampleService.class);

基于代码自定义配置

  1. SampleService sampleService = Forest.from(SampleService.class, ServiceConfig.Builder.newBuilder()
  2. .withMethodConfig("say", MethodConfig.Builder.newBuilder()
  3. .withCompressType(CompressType.none)
  4. .withSerializeType(SerializeType.fastjson)
  5. .build())
  6. .withMethodConfig("echo", MethodConfig.Builder.newBuilder()
  7. .withCompressType(CompressType.none)
  8. .build())
  9. .build());

Console输出

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

更多示例

Documents

TODO

  • 服务降级功能
  • http服务支持
  • 跨语言协议支持
  • 服务治理管理后台

License

Forest is released under the Apache License 2.0.