-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation: - Provide a tutorial for beginners who wants to use Apache Thrift with Armeria Modifications: - Add a tutorial for writing an Apache Thrift service with Armeria Result: - Adds the tutorial in the "THRIFT SERVICE" menu in armeria.dev site. Co-authored-by: jrhee17 <[email protected]>
- Loading branch information
Showing
16 changed files
with
1,438 additions
and
48 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
examples/tutorials/thrift/src/main/java/example/armeria/server/blog/thrift/BlogClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package example.armeria.server.blog.thrift; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import org.apache.thrift.TException; | ||
|
||
import com.linecorp.armeria.client.logging.LoggingClient; | ||
import com.linecorp.armeria.client.thrift.ThriftClients; | ||
|
||
import example.armeria.blog.thrift.BlogPost; | ||
import example.armeria.blog.thrift.BlogService; | ||
import example.armeria.blog.thrift.CreateBlogPostRequest; | ||
import example.armeria.blog.thrift.DeleteBlogPostRequest; | ||
import example.armeria.blog.thrift.GetBlogPostRequest; | ||
import example.armeria.blog.thrift.ListBlogPostsRequest; | ||
import example.armeria.blog.thrift.UpdateBlogPostRequest; | ||
|
||
public final class BlogClient { | ||
|
||
private final BlogService.Iface blogService; | ||
|
||
BlogClient(URI uri, String path) { | ||
blogService = ThriftClients.builder(uri) | ||
.path(path) | ||
.decorator(LoggingClient.newDecorator()) | ||
.build(BlogService.Iface.class); | ||
} | ||
|
||
BlogPost createBlogPost(String title, String content) throws TException { | ||
final CreateBlogPostRequest request = | ||
new CreateBlogPostRequest().setTitle(title) | ||
.setContent(content); | ||
return blogService.createBlogPost(request); | ||
} | ||
|
||
BlogPost getBlogPost(int id) throws TException { | ||
final GetBlogPostRequest request = | ||
new GetBlogPostRequest().setId(id); | ||
return blogService.getBlogPost(request); | ||
} | ||
|
||
List<BlogPost> listBlogPosts(boolean descending) throws TException { | ||
return blogService.listBlogPosts(new ListBlogPostsRequest().setDescending(descending)) | ||
.getBlogs(); | ||
} | ||
|
||
BlogPost updateBlogPost(int id, String newTitle, String newContent) throws TException { | ||
final UpdateBlogPostRequest request = new UpdateBlogPostRequest().setId(id) | ||
.setTitle(newTitle) | ||
.setContent(newContent); | ||
return blogService.updateBlogPost(request); | ||
} | ||
|
||
void deleteBlogPost(int id) throws TException { | ||
final DeleteBlogPostRequest request = new DeleteBlogPostRequest().setId(id); | ||
blogService.deleteBlogPost(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--- | ||
menuTitle: "Create a service" | ||
order: 1 | ||
category: thrift | ||
type: step | ||
targetLang: java | ||
--- | ||
|
||
# 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. | ||
|
||
<TutorialSteps current={1} /> | ||
|
||
## What you need | ||
|
||
No preparation is required for this step. Do check that you've prepared the [prerequisites](/tutorials/thrift/blog/#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. | ||
|
||
```cpp filename=blog.thrift | ||
namespace java example.armeria.blog.thrift | ||
|
||
service BlogService { | ||
string hello() | ||
} | ||
``` | ||
<Tip> | ||
See [Sample service structure](/tutorials/thrift/blog#sample-service-structure) for the overall folder structure. | ||
</Tip> | ||
## 2. Compile the thrift file | ||
Compile the `blog.thrift` file to generate Java code. | ||
You can refer to the full [build.gradle](https://github.com/line/armeria-examples/tree/main/tutorials/thrift/build.gradle) file for generating code with [Gradle Thrift Plugin](https://github.com/jruyi/thrift-gradle-plugin). | ||
```bash | ||
./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. | ||
```java filename=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. | ||
```java filename=BlogServiceImpl.java | ||
import org.apache.thrift.async.AsyncMethodCallback; | ||
... | ||
@Override | ||
public void hello(AsyncMethodCallback<String> resultHandler) { | ||
resultHandler.onComplete("Hello, Armeria!"); | ||
} | ||
``` | ||
|
||
<Tip> | ||
|
||
Although here we implement the asynchronous interface `BlogService.AsyncIface`, note that implementing the synchronous interface `BlogService.Iface` isn't very different. | ||
See [Running a Thrift service](/docs/server-thrift) for more information. | ||
|
||
</Tip> | ||
|
||
## What's next | ||
|
||
In this step, we've created a simple service with a dummy method. | ||
|
||
Next, at [Step 2. Run a server](/tutorials/thrift/blog/run-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. | ||
|
||
<TutorialSteps current={1} /> |
Oops, something went wrong.