com.zhizus:thrift-demo

Forest Thrift

License

License

GroupId

GroupId

com.zhizus
ArtifactId

ArtifactId

thrift-demo
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Forest Thrift

Download thrift-demo

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
com.zhizus : thrift-client jar 0.0.1
com.zhizus : thrift-server jar 0.0.1
org.apache.thrift : libthrift jar 0.6.1
com.google.guava : guava jar 18.0
org.slf4j : slf4j-api jar 1.7.5

Project Modules

There are no modules declared in this project.

#forest-thrift thrift服务的简单的封装

##thrift-client

池化的高可用thrift客户端

future

  • 支持连接池
  • 支持thrift描述文件的ping校验
  • 支持负载均衡策略
  • 支持容灾策略
  • 支持自动熔断策略
  • 支持基于zk的服务发现

quick start

maven依赖

<dependency>
    <groupId>com.zhizus</groupId>
    <artifactId>thrift-client</artifactId>
    <version>0.0.1</version>
</dependency>

Alt text

使用示例:

// 连接池配置,
    GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();

    // 校验接口,连接池的validateObject方法会调用到这里的ping方法
    // 这里主要用于对thrift描述文件层面的心跳校验支持
    PingValidate pingValidate = new PingValidate() {
        @Override
        public boolean ping(ServerInfo key, TTransport transport) {
            return true;
        }
    };
    // 客户端熔断策略,默认1min中10次异常则自动熔断,恢复时间也为1min中
    IsolationStrategy<ServerInfo> infoIsolationStrategy = new IsolationStrategy<>();

    DefaultThriftClient thriftClient = new DefaultThriftClient(LoadBalanceType.RANDOM, HAStrategyType.FAILED_FAST,
            new ConfRegistry("localhost:9999"), poolConfig, pingValidate, infoIsolationStrategy);
    //每次使用client请调用iface接口,这里通过代理模式包装了异常统计和回池操作,一个 iface生成的代理对象调用多次会出现问题
    //  thriftClient.iface(YourThrift.class);

thrift-server

更多示例

user guide

1.根据thrift的描述文件生成thrift静态代码

thrift-0.6.1.exe -r -gen java sample.thrift

2.继承AbstractThriftServer,实现getProcessor()方法:

  @Override
    protected TProcessor getProcessor() {
        return new Sample.Processor(new Sample.Iface() {

            @Override
            public String hello(String para) throws TException {
                return "hello " + para;
            }

            @Override
            public boolean ping() throws TException {
                return true;
            }
        });
    }

调用start()方法,启动server

3.基于DefaultThriftClient构造client,并且调用iface方法生成对应的代理对象。

Versions

Version
0.0.1