Set of cool annotations that helps you building Thrift applications with Spring Boot.
- Spring Boot 3.2.x support (upgraded from Spring Boot 2.x)
- Java 17+ required (upgraded from Java 11)
- Spring Cloud 2023.0.x support
- Micrometer Tracing replaces Spring Cloud Sleuth for distributed tracing
- Custom header propagation via
ThriftClientHeaderCustomizerSPI - Standard
java-library+maven-publishGradle plugins (replaced Nebula plugins) - Constructor injection throughout (no more
@Autowiredfield injection)
repositories {
...
maven {
url = uri("https://maven.pkg.github.com/aatarasoff/spring-thrift-starter")
credentials {
username = System.getenv("USERNAME")
password = System.getenv("TOKEN")
}
}
}
implementation 'info.developerblog.spring.thrift:spring-thrift-starter:4.0.0'For more information, please look at the official GitHub Packages documentation.
Annotation @ThriftController("servlet_path") helps you building server controller for request processing
@ThriftController("/api")
public class TGreetingServiceController implements TGreetingService.Iface {
@Override
public String greet(TName name) throws TException {
// your logic
}
}@ThriftClient(serviceId = "registered_service", (path) = "server_handler_path") helps you with multithreaded client with full Spring Cloud support.
@ThriftClient(serviceId = "greeting-service", path = "/api")
TGreetingService.Client client;Thrift clients can also be used as regular beans
(which can be configured through app properties)
class Service {
@Autowired
private TGreetingService.Client client;
}class Service {
private final TGreetingService.Client client;
@Autowired
public Service(TGreetingService.Client client) {
this.client = client;
}
}@ThriftClientsMap(mapperClass) annotation helps to create a string-keyed map of clients for a set of services having the same interface, allowing to define the concrete callee instance at runtime:
@ThriftClientsMap(mapperClass = SampleMapper.class)
Map<String, TGreetingService.Client> clientsMap;Mapper class requirements:
- must extend AbstractThriftClientKeyMapper
- must be registered as a bean in the application context
greeting-service: #service name
endpoint: http://localhost:8080/api #direct endpoint
ribbon: #manually ribbon
listOfServers: localhost:8080
path: /service #general path
connectTimeout: 1000 #default=1000
readTimeout: 10000 #default=30000
thrift.client.max.threads: 10 #default=8If you use service discovery backend (as Eureka or Consul) only path maybe needed.
See tests for better understanding.
Distributed tracing is supported via Micrometer Tracing with Brave bridge (replaces Spring Cloud Sleuth which was removed in Spring Boot 3).
Implement ThriftClientHeaderCustomizer and register it as a Spring bean to inject custom headers into outgoing Thrift calls:
@Component
public class AuthHeaderCustomizer implements ThriftClientHeaderCustomizer {
@Override
public Map<String, String> headers() {
return Map.of("Authorization", "Bearer " + tokenProvider.getToken());
}
}- @bsideup who inspired me with his project
- @lavcraft who was helping me when I've been stucked
- @driver733 for implementing the bean registration support