我希望将thrift用于我的Web服务调用。从我迄今为止从节俭文档中读到的内容来看,我将不得不编写一个包含我想要公开的POJO和服务的thrift文件。然后需要使用thrift编译器编译该文件以生成Java类。然后必须使用这些来编写客户端和服务器。
有没有更简单的方法来实现这一点(任何基于注释或Spring框架集成可用)?
答案 0 :(得分:3)
我写了一篇关于Spring Boot和Thrift集成的文章,详细解释了如何将它们组合在一起:)你可以在这里找到我的文章:
Java.DZone: Building Microservices with Spring Boot and Apache Thrift.
通常,您应该像这样创建Servlet bean:
@Bean
public TProtocolFactory tProtocolFactory() {
return new TBinaryProtocol.Factory();
}
@Bean
public Servlet calculator(TProtocolFactory protocolFactory, CalculatorServiceHandler handler) {
return new TServlet(new TCalculatorService.Processor<CalculatorServiceHandler>(handler), protocolFactory);
}
其中TCalculatorService是您的Thrift服务
答案 1 :(得分:2)
不,春天和节俭之间没有自定义绑定。一旦创建了.thrift
文件,您将生成将构成thrift通信层的Java类。
例如,我创建了一个通过hibernate调用SQL的Java服务器(这是一个层)并通过thrift(另一个层)返回数据。不幸的是,必须有一些Java代码将处理从一个层移动到另一个层的抽象数据。
答案 2 :(得分:2)
您可以使用此项目在SpringBoot和Apache Thrift https://github.com/aatarasoff/spring-thrift-starter之间进行集成。
正如在README中所描述的那样,只需连接starter并创建处理程序,就像使用@RestController一样:
MySingleton *singleton = [MySingleton sharedInstance];
NSString *token = singleton.oauth_token;
NSString *secret = singleton.oauth_token_secret;
EnvironmentSingleton *environmentSingleton = [EnvironmentSingleton sharedInstance];
NSString *envURL = [NSString stringWithFormat:@"%@%@", environmentSingleton.environment, environmentSingleton.base_url];
NSString *resourcePath = [NSString stringWithFormat:@"%@/api/user", envURL];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:token forKey:@"token"];
[params setObject:secret forKey:@"secret"];
[params setObject:txtEmail.text forKey:@"email"];
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[UserInfo class]];
[objectMapping addAttributeMappingsFromDictionary:@{@"_appId": @"appId",
@"_fullName": @"fullName",
@"_domainUrn": @"domainUrn"}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping method:RKRequestMethodPOST pathPattern:nil keyPath:nil statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:resourcePath]];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSArray *objects = [[NSArray alloc] initWithArray:[result array]];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Error(%li): %@", (long)error.code,error.localizedDescription);
}];
[operation start];
答案 3 :(得分:0)