Forest RPC


License

License

GroupId

GroupId

com.zhizus
ArtifactId

ArtifactId

forest
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

pom
Description

Description

Forest RPC
Forest RPC
Project URL

Project URL

https://github.com/dempeZheng/forest
Source Code Management

Source Code Management

https://github.com/dempeZheng/forest

Download forest

Filename Size
forest-0.0.1.pom 9 KB
Browse

How to add to project

<!-- https://jarcasting.com/artifacts/com.zhizus/forest/ -->
<dependency>
    <groupId>com.zhizus</groupId>
    <artifactId>forest</artifactId>
    <version>0.0.1</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/com.zhizus/forest/
implementation 'com.zhizus:forest:0.0.1'
// https://jarcasting.com/artifacts/com.zhizus/forest/
implementation ("com.zhizus:forest:0.0.1")
'com.zhizus:forest:pom:0.0.1'
<dependency org="com.zhizus" name="forest" rev="0.0.1">
  <artifact name="forest" type="pom" />
</dependency>
@Grapes(
@Grab(group='com.zhizus', module='forest', version='0.0.1')
)
libraryDependencies += "com.zhizus" % "forest" % "0.0.1"
[com.zhizus/forest "0.0.1"]

Dependencies

compile (23)

Group / Artifact Type Version
org.apache.maven.plugins : maven-gpg-plugin jar 1.6
io.netty : netty-buffer jar 4.1.0.Final
io.netty : netty-common jar 4.1.0.Final
io.netty : netty-codec jar 4.1.0.Final
io.netty : netty-transport jar 4.1.0.Final
io.netty : netty-handler jar 4.1.0.Final
io.netty : netty-codec-http jar 4.1.0.Final
org.springframework : spring-context jar 4.2.5.RELEASE
org.springframework : spring-aop jar 4.2.5.RELEASE
org.apache.commons : commons-lang3 jar 3.1
log4j : log4j jar 1.2.16
org.slf4j : slf4j-api jar 1.7.5
ch.qos.logback : logback-classic jar 1.0.13
org.slf4j : jcl-over-slf4j jar 1.7.5
org.aeonbits.owner : owner jar 1.0.8
com.alibaba : fastjson jar 1.1.36
org.apache.commons : commons-pool2 jar 2.2
com.google.guava : guava jar 17.0
org.xerial.snappy : snappy-java jar 1.1.1.6
com.caucho : hessian jar 4.0.38
com.esotericsoftware.kryo : kryo jar 2.24.0
org.apache.curator : curator-x-discovery jar 2.7.1
redis.clients : jedis jar 2.4.2

Project Modules

  • forest-demo
  • forest-rpc
  • forest-common

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.

<dependency>
    <groupId>com.zhizus</groupId>
    <artifactId>forest-rpc</artifactId>
    <version>0.0.2</version>
</dependency>

<dependency>
    <groupId>com.zhizus</groupId>
    <artifactId>forest-common</artifactId>
    <version>0.0.2</version>
</dependency>

1.定义接口

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

@ServiceProvider(serviceName = "sampleService", haStrategyType = HaStrategyType.FAIL_FAST,
        loadBalanceType = LoadBalanceType.RANDOM, connectionTimeout = Constants.CONNECTION_TIMEOUT)
public interface SampleService {

    @MethodProvider(methodName = "say")
    String say(String str);

    @MethodProvider(methodName = "echo", serializeType = SerializeType.Hession2, compressType = CompressType.None)
    String echo(String msg);
}

2.实现接口

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

@Path("/sample")
@ServiceExport
public class SampleServiceImpl implements SampleService {

    /**
     * 支持jersey,可以通过配置打开,同时启动http服务
     *
     * @param str
     * @return
     */
    @Path("/hello/{str}")
    @GET
    @Produces("text/plain")
    @MethodExport
    @Rate(2)
    @Override
    public String say(@PathParam("str") String str) {
        return "say " + str;
    }

    @Interceptor("metricInterceptor")
    @MethodExport
    @Override
    public String echo(String msg) {
        return "echo>>> " + msg;
    }

}

3.服务端开发

spring context 配置:

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans" xmlns:forest="http://api.zhizus.com/schema/forest"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://api.zhizus.com/schema/forest http://api.zhizus.com/schema/forest.xsd">

    <context:component-scan base-package="com.zhizus.forest.demo"/>

    <context:property-placeholder location="classpath:/*.properties"/>


    <forest:registry id="registry" regProtocol="local"  name="registry" address="127.0.0.1:2181"/>
    <!--<forest:registry id="registry" regProtocol="zookeeper" name="registry" address="127.0.0.1:2181"/>-->

    <forest:server id="forestServer" registry="registry" startHttpServer="true"/>

    <forest:interceptors>
        <forest:interceptor id="metricInterceptor" class="com.zhizus.forest.support.MetricInterceptor" auto-match="public *(*)"/>
    </forest:interceptors>


</beans>

Server开发

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

4.客户端开发

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:forest="http://api.zhizus.com/schema/forest"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        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">

    <context:component-scan base-package="com.zhizus.forest.demo.client"/>

    <forest:registry id="registry" regProtocol="local" name="registry" address="127.0.0.1:9999"/>

    <forest:referer id="sampleService" interface="com.zhizus.forest.demo.api.SampleService" registry="registry">
        <forest:method name="echo" timeout="5000" serializeType="Fastjson"/>
        <forest:method name="say" timeout="5000" serializeType="Fastjson" compressType="GZIP"/>
    </forest:referer>


</beans>
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application-client.xml"});
     SampleService bean = (SampleService) context.getBean("sampleService");
     String test = bean.say("hello");

Console输出

23:10:10.295 [pool-1-thread-1] INFO MetricInterceptor 34 - methodName:/sampleService/say, current tps:83342, avgTime:0, maxTime:63, minTime:0
23:10:11.298 [pool-1-thread-1] INFO MetricInterceptor 34 - methodName:/sampleService/say, current tps:86271, avgTime:0, maxTime:63, minTime:0
23:10:12.295 [pool-1-thread-1] INFO MetricInterceptor 34 - methodName:/sampleService/say, current tps:86063, avgTime:0, maxTime:63, minTime:0
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.

Versions

Version
0.0.1