Repository navigation
Expand file tree
/
Copy pathclient-thrift.html
More file actions
340 lines (202 loc) · 14.4 KB
/
Copy pathclient-thrift.html
File metadata and controls
340 lines (202 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calling a Thrift service — Armeria 0.82.0 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/overrides.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Calling a gRPC service" href="client-grpc.html" />
<link rel="prev" title="Calling an HTTP service" href="client-http.html" />
<script src="_static/center_page.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/default.min.css">
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html">
<img src="_static/armeria.png" class="logo" alt="Logo"/>
</a>
<div class="version">
0.82
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="setup.html">Setting up a project</a></li>
<li class="toctree-l1"><a class="reference internal" href="server.html">Writing a server</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="client.html">Writing a client</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="client-http.html">Calling an HTTP service</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Calling a Thrift service</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#see-also">See also</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="client-grpc.html">Calling a gRPC service</a></li>
<li class="toctree-l2"><a class="reference internal" href="client-decorator.html">Decorating a client</a></li>
<li class="toctree-l2"><a class="reference internal" href="client-retrofit.html">Retrofit integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="client-custom-http-headers.html">Sending custom HTTP headers</a></li>
<li class="toctree-l2"><a class="reference internal" href="client-timeouts.html">Overriding client timeouts</a></li>
<li class="toctree-l2"><a class="reference internal" href="client-retry.html">Automatic retry</a></li>
<li class="toctree-l2"><a class="reference internal" href="client-circuit-breaker.html">Circuit breaker</a></li>
<li class="toctree-l2"><a class="reference internal" href="client-service-discovery.html">Client-side load balancing and service discovery</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="advanced.html">Advanced topics</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/line/armeria/releases">Release notes</a></li>
<li class="toctree-l1"><a class="reference external" href="apidocs/index.html#://">API documentation</a></li>
<li class="toctree-l1"><a class="reference external" href="xref/index.html#://">Source cross-reference</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/line/armeria/issues?q=label%3Aquestion">Questions and answers</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/line/armeria">Fork me at GitHub</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/line/armeria/blob/master/CONTRIBUTING.md">Contributing</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Armeria</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li><a href="client.html">Writing a client</a> »</li>
<li>Calling a Thrift service</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/line/armeria/blob/master/site/src/sphinx/client-thrift.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="calling-a-thrift-service">
<span id="client-thrift"></span><h1>Calling a Thrift service<a class="headerlink" href="#calling-a-thrift-service" title="Permalink to this headline">¶</a></h1>
<p>Let’s assume we have the following Thrift IDL, served at <tt class="docutils literal">http://127.0.0.1:8080/hello</tt>, just like what we
used in <a class="reference internal" href="server-thrift.html#server-thrift"><span class="std std-ref">Running a Thrift service</span></a>:</p>
<div class="highlight-thrift notranslate"><div class="highlight hljs"><pre class="thrift">namespace java com.example.thrift.hello
service HelloService {
string hello(1:string name)
}</pre></div>
</div>
<p>Making a call starts from creating a client:</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">import com.linecorp.armeria.client.Clients;
HelloService.Iface helloService = Clients.newClient(
"tbinary+http://127.0.0.1:8080/hello",
HelloService.Iface.class); // or AsyncIface.class
String greeting = helloService.hello("Armerian World");
assert greeting.equals("Hello, Armerian World!");</pre></div>
</div>
<p>Note that we added the serialization format of the call using the <tt class="docutils literal">+</tt> operator in the scheme part of the URI.
Because we are calling a Thrift server, we should choose: <tt class="docutils literal">tbinary</tt>, <tt class="docutils literal">tcompact</tt>, <tt class="docutils literal">tjson</tt> or <tt class="docutils literal">ttext</tt>.</p>
<p>Since we specified <tt class="docutils literal">HelloService.Iface</tt> as the client type, <tt class="docutils literal">Clients.newClient()</tt> will return a synchronous
client implementation. If we specified <tt class="docutils literal">HelloService.AsyncIface</tt>, the calling code would have looked like
the following:</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">import com.linecorp.armeria.common.thrift.ThriftCompletableFuture;
import com.linecorp.armeria.common.util.CompletionActions;
import com.linecorp.armeria.client.Clients;
HelloService.AsyncIface helloService = Clients.newClient(
"tbinary+http://127.0.0.1:8080/hello",
HelloService.AsyncIface.class);
ThriftCompletableFuture<String> future = new ThriftCompletableFuture<String>();
helloService.hello("Armerian World", future);
future.thenAccept(response -> assert response.equals("Hello, Armerian World!"))
.exceptionally(cause -> {
cause.printStackTrace();
return null;
});
// You can also wait until the call is finished.
String reply = future.get();</pre></div>
</div>
<p>The example above introduces a new class called <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/common/thrift/ThriftCompletableFuture.html">ThriftCompletableFuture</a></code>. It is a subtype of Java 8
<a class="reference external" href="https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/CompletableFuture.html">CompletableFuture</a> that implements Thrift <a class="reference external" href="https://github.com/apache/thrift/blob/bd964c7f3460c308161cb6eb90583874a7d8d848/lib/java/src/org/apache/thrift/async/AsyncMethodCallback.java#L22">AsyncMethodCallback</a>. Once passed as a callback of an asynchronous
Thrift call, <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/common/thrift/ThriftCompletableFuture.html">ThriftCompletableFuture</a></code> will complete itself when the reply is received or the call
fails. You’ll find it way more convenient to consume the reply than <a class="reference external" href="https://github.com/apache/thrift/blob/bd964c7f3460c308161cb6eb90583874a7d8d848/lib/java/src/org/apache/thrift/async/AsyncMethodCallback.java#L22">AsyncMethodCallback</a> thanks to the rich set
of methods provided by <a class="reference external" href="https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/CompletableFuture.html">CompletableFuture</a>.</p>
<p>You can also use the builder pattern for client construction:</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;
HelloService.Iface helloService = new ClientBuilder("tbinary+http://127.0.0.1:8080/hello")
.defaultResponseTimeoutMillis(10000)
.decorator(LoggingClient.newDecorator())
.build(HelloService.Iface.class); // or AsyncIface.class
String greeting = helloService.hello("Armerian World");
assert greeting.equals("Hello, Armerian World!");</pre></div>
</div>
<p>As you might have noticed already, we decorated the client using <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/client/logging/LoggingClient.html">LoggingClient</a></code>, which logs all
requests and responses. You might be interested in decorating a client using other decorators, for example
to gather metrics. Please also refer to <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/client/ClientBuilder.html">ClientBuilder</a></code> for more configuration options.</p>
<div class="section" id="see-also">
<h2>See also<a class="headerlink" href="#see-also" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference internal" href="server-thrift.html#server-thrift"><span class="std std-ref">Running a Thrift service</span></a></li>
<li><a class="reference internal" href="client-decorator.html#client-decorator"><span class="std std-ref">Decorating a client</span></a></li>
<li><a class="reference internal" href="client-custom-http-headers.html#client-custom-http-headers"><span class="std std-ref">Sending custom HTTP headers</span></a></li>
</ul>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="client-grpc.html" class="btn btn-neutral float-right" title="Calling a gRPC service" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="client-http.html" class="btn btn-neutral" title="Calling an HTTP service" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2015-2019, LINE Corporation
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
<div class="github-fork-ribbon-wrapper right">
<div class="github-fork-ribbon">
<a href="https://github.com/line/armeria">Fork me at GitHub</a>
</div>
</div>
<script type="text/javascript" src="_static/add_badges.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/gradle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/protobuf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/thrift.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/yaml.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.6.0/highlightjs-line-numbers.min.js"></script>
<script>
document.querySelectorAll('div.hljs > pre').forEach(function(block) {
hljs.highlightBlock(block);
hljs.lineNumbersBlock(block);
});
</script>
</body>
</html>