GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - cyfonly/ThriftJ: 负载均衡和高可用的池化 Thrift Client

BranchesTags
Open more actions menu

Latest commit

 

History

25 Commits
25 Commits

Folders and files

NameName
Last commit message
Last commit date
pictures
pictures
 
 
src
src
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
pom.xml
pom.xml
 
 
versions.md
versions.md
 
 

Repository files navigation

design
License Built with Maven
负载均衡和高可用的池化 Thrift Client

Features

  1. 链式调用API,简洁直观
  2. 完善的默认配置,无需担心调用时配置不全导致抛错
  3. 池化连接对象,高效管理连接的生命周期
  4. 异常服务自动隔离与恢复
  5. 多种可配置的负载均衡策略,支持随机、轮询、权重和哈希
  6. 多种可配置的服务级别,并自动根据服务级别进行服务降级

Architectural Design

design

Usage

1. Maven

<dependency>
    <groupId>com.github.cyfonly</groupId>
    <artifactId>thriftj</artifactId>
    <version>1.0.1</version>
</dependency>
<dependency>
    log4j or logback, and so on...
</dependency>

2. 调用

除 servers 必须配置外,其他配置均为可选(使用默认配置)

//Thrift server 列表
private static final String servers = "127.0.0.1:10001,127.0.0.1:10002";

//TTransport 验证器
ConnectionValidator validator = new ConnectionValidator() {
    @Override
    public boolean isValid(TTransport object) {
        return object.isOpen();
    }
};

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

//failover 策略
FailoverStrategy failoverStrategy = new FailoverStrategy();

//构造 ThriftClient 对象并配置
final ThriftClient thriftClient = new ThriftClient();
thriftClient.servers(servers)
            .loadBalance(Constant.LoadBalance.RANDOM)
            .connectionValidator(validator)
            .poolConfig(poolConfig)
            .failoverStrategy(failoverStrategy)
            .connTimeout(5)
            .backupServers("")
            .serviceLevel(Constant.ServiceLevel.NOT_EMPTY)
            .start();
            
//打印从 ThriftClient 获取到的可用服务列表
List<ThriftServer> servers = thriftClient.getAvailableServers();
for(ThriftServer server : servers){
    System.out.println(server.getHost() + ":" + server.getPort());
}

//服务调用
if(servers.size()>0){
    try{
		    TestThriftJ.Client client = thriftClient.iface(TestThriftJ.Client.class);
		    QryResult result = client.qryTest(1);
		    System.out.println("result[code=" + result.code + " msg=" + result.msg + "]");
	  }catch(Throwable t){
		    logger.error("-------------exception happen", t);
	  }
}

Demo

see https://github.com/cyfonly/ThriftJ/tree/master/src/test

License

基于 Apache License 2.0 发布。有关详细信息,请参阅 LICENSE。

About

负载均衡和高可用的池化 Thrift Client

www.cnblogs.com/cyfonly/p/6284117.html

Resources

Readme
Apache-2.0 license
Activity

Stars

63 stars

Watchers

10 watching

Forks

39 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.