Creating a service

As the first step of the tutorial, we'll create a simple service with a dummy method in Thrift and implement the service in Java.

What you need

No preparation is required for this step. Do check that you've prepared the prerequisites.

1. Create a thrift file

Create a thrift file, blog.thrift in the {project_root}/src/main/thrift folder as follows. In the thrift file, let's define the BlogService service with the hello() method.

blog.thrift
namespace java example.armeria.blog.thrift

service BlogService {
  string hello()
}

2. Compile the thrift file

Compile the blog.thrift file to generate Java code. You can refer to the full build.gradle file for generating code with Gradle Thrift Plugin.

./gradlew compileThrift

You'll see the generated Java code in the {project_root}/build/generated-sources/thrift/gen-java/example/armeria/blog/thrift/ folder.

3. Implement the service

Now, let's implement the service in Java.

  1. Create a file, BlogServiceImpl.java.

  2. Declare the BlogServiceImpl class implementing the BlogService service we defined earlier in Thrift.

    BlogServiceImpl.java
    package example.armeria.server.blog.thrift;
    
    import example.armeria.blog.thrift.BlogService;
    
    public class BlogServiceImpl implements BlogService.AsyncIface {}
  3. In the BlogServiceImpl class, override the hello() method, a dummy method for temporary use to test the connection between server and client.

    BlogServiceImpl.java
    import org.apache.thrift.async.AsyncMethodCallback;
    ...
    @Override
    public void hello(AsyncMethodCallback<String> resultHandler) {
      resultHandler.onComplete("Hello, Armeria!");
    }

What's next

In this step, we've created a simple service with a dummy method.

Next, at Step 2. Run a server, we'll create and run a server with the service we've created. Also, we'll run a client to make a call to the service.