TwitterServer¶
TwitterServer defines a template from which servers at Twitter are built. It provides common application components such as an administrative HTTP server, tracing, stats, etc. These features are wired in correctly for use in production at Twitter.
Getting Started¶
TwitterServer is published to Maven central:
<dependency> <groupId>com.twitter</groupId> <artifactId>twitter-server_2.12</artifactId> <version>24.2.0</version> </dependency>
or, with sbt:
libraryDependencies += "com.twitter" %% "twitter-server" % "24.2.0"
First we’ll need to import a few things into our namespace.
import com.twitter.finagle.{Http, Service}
import com.twitter.finagle.http.{Request, Response, Status}
import com.twitter.server.TwitterServer
import com.twitter.util.{Await, Future}
TwitterServer defines its own version of the standard main
. To use
it, create an object extended with com.twitter.server.TwitterServer
,
and define the main()
method (no arguments).
In this example, we use Finagle to start an HTTP server on port 8888. The service bound to this port is a simple hello service.
object BasicServer extends TwitterServer {
val service = new Service[Request, Response] {
def apply(request: Request) = {
val response = Response(request.version, Status.Ok)
response.contentString = "hello"
Future.value(response)
}
}
def main(): Unit = {
val server = Http.serve(":8888", service)
onExit {
server.close()
}
Await.ready(server)
}
}
onExit
is used to register code to be run when the process shutdown
is requested.
After compiling, we can start the server like any other java or scala process.
$ java -jar target/myserver-1.0.0-SNAPSHOT.jar &
[1] 74159
I 1210 21:32:39.326 THREAD1: /admin => com.twitter.server.handler.SummaryHandler
...
I 1210 21:32:39.332 THREAD1: /favicon.ico => com.twitter.server.handler.ResourceHandler
I 1210 21:32:39.340 THREAD1: Serving admin http on 0.0.0.0/0.0.0.0:9990
I 1210 21:32:39.361 THREAD1: Finagle version 6.31.0 (rev=50d3bb0eea5ad3ed332111d707184c80fed6a506) built at 20151203-164135
I 1210 21:32:39.910 THREAD1: Tracer: com.twitter.finagle.zipkin.thrift.SamplingTracer
$ curl localhost:8888
hello
This server is fully configured to run in Twitter’s production environment. See features for more details.
User’s guide¶
- Features
- HTTP Admin interface
- Using TwitterServer from Java
- Migration from Ostrich
- FAQ
- Changelog
- Unreleased
- 24.2.0
- 23.11.0
- 22.12.0
- 22.7.0
- 22.4.0
- 22.3.0
- 22.2.0
- 22.1.0
- 21.12.0
- 21.11.0
- 21.10.0
- 21.9.0
- 21.8.0 (No 21.7.0 Release)
- 21.6.0
- 21.5.0
- 21.4.0
- 21.3.0
- 21.2.0
- 21.1.0
- 20.12.0
- 20.10.0
- 20.9.0
- 20.8.1
- 20.8.0 (DO NOT USE)
- 20.7.0
- 20.6.0
- 20.5.0
- 20.4.1
- 20.4.0 (DO NOT USE)
- 20.3.0
- 20.2.1
- 20.1.0
- 19.12.0
- 19.11.0
- 19.10.0
- 19.9.0
- 19.8.0
- 19.7.0
- 19.6.0
- 19.5.1
- 19.5.0
- 19.4.0
- 19.3.0
- 19.2.0
- 19.1.0
- 18.12.0
- 18.11.0
- 18.10.0
- 18.9.1
- 18.9.0
- 18.8.0
- 18.7.0
- 18.6.0
- 18.5.0
- 18.4.0
- 18.3.0
- 18.2.0
- 18.1.0
- 17.12.0
- 17.11.0
- 17.10.0
- 1.32.0
- 1.31.0
- 1.30.0
- 1.29.0
- 1.28.0
- 1.27.0
- 1.26.0
- 1.25.0
- 1.24.0
- 1.23.0
- 1.22.0
- 1.21.0
- 1.20.0
- 1.19.0
- 1.18.0
- 1.17.0
- 1.16.0
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.0
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.0
- 1.7.6
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1