本篇主要介绍的是Spring与Thrift简单整合,将Thrift Bean的生命周期托管给Spring进行管理,更贴近实际工程使用。仍然是分为服务端与客户端部分。
客户端
工程结构如下
客户端主要分为两个部分
-
注册Thrfit Bean到Spring
-
访问服务端
Bean注册
下面注册了一个自定义的UserThriftService,这个是在Server端定义
import ltd.klein.thrift.server.api.iface.UserThriftService;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ThriftConfig {
@Bean
public UserThriftService.Client userServiceClient () throws Exception {
TTransport transport = new TSocket ( "localhost" , 9001 ) ;
transport.open () ;
TBinaryProtocol protocol = new TBinaryProtocol ( transport ) ;
return new UserThriftService.Client ( protocol ) ;
}
}
服务调用
通过注册的Bean访问远程User服务,注意如果返回为空会抛异常
import ltd.klein.thrift.server.api.iface.UserThriftService;
import ltd.klein.thrift.server.api.param.User;
import org.apache.thrift.TException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping ( "/user" )
public class UserController {
@Resource
private UserThriftService.Client userServiceClient;
@GetMapping ( "/{id}" )
public User findUserById ( @PathVariable int id ) {
try {
return userServiceClient.findUser ( id ) ; // org.apache.thrift.TApplicationException: findUser failed: unknown result
} catch ( TException e ) {
e.printStackTrace () ;
return null;
}
}
}
服务端
服务端的工程结构如下。拆分了api和service两个model,其中api中定义了请求参数、接口等idl,客户端通过maven将其引入。service中将服务注册为Bean,并实现了具体业务逻辑。
IDL
UserThriftService
namespace java ltd.klein.thrift.server.api.iface
include '../param/User.thrift'
service UserThriftService {
User.User findUser ( 1:i32 id ) ;
}
User
namespace java ltd.klein.thrift.server.api.param
struct User {
1: string name;
2: i32 id;
}
Bean注册
import ltd.klein.thrift.server.api.iface.UserThriftService;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* @author Klein
* @date 2024-10-20 20:22
*/
@Configuration
public class ThriftConfig {
@Resource
private UserThriftService.Iface userThriftService;
@Bean ( initMethod = "serve" , destroyMethod = "stop" )
public TServer thriftServer () {
try {
TProcessor processor = new UserThriftService.Processor <>( userThriftService ) ;
TServerTransport serverTransport = new TServerSocket ( 9001 ) ;
TServer server = new TSimpleServer ( new TServer.Args ( serverTransport ) .processor ( processor ) .protocolFactory ( new TBinaryProtocol.Factory ())) ;
return server;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
}
服务端实现
业务实现不是本期重点,这里仅简单介绍
实现类实现UserThriftService.Iface(IDL编译后生成)
@Service
public class UserThriftServiceImpl implements UserThriftService.Iface {
@Override
public User findUser ( int id ) throws TException {
if ( id == 0 ) {
User user = new User () ;
user.setId ( id ) ;
user.setName ( "zhangsan" ) ;
return user;
}
return null;
}
}
本篇整合Thrift就到这里,下一篇会介绍Thrift的IDL编写。如有问题,欢迎留言讨论。