HelloService.thrift 定义服务接口
namespace java com.example.thrift
service HelloService {
string hello(1:string name)
}
public class HelloService {
public interface Iface {
public String hello(String name) throws org.apache.thrift.TException;
}
public interface AsyncIface {
public void hello(String name, org.apache.thrift.async.AsyncMethodCallback resultHandler) throws org.apache.thrift.TException;
}
}
在这里使用thrift 规范,生成同步Iface 和异步AsyncIface 接口。
HelloServiceIfaceImpl 实现同步接口。
public class HelloServiceIfaceImpl implements HelloService.Iface
{
@Override
public String hello(String name) throws TException
{
System.out.println("ArmeriaClient ----->"+name);
return "hello to "+name;
}
}
public class ArmeriaServer
{
public static void main(String[] args)
{
HelloService.Iface helloHandler = new HelloServiceIfaceImpl();
//we created a new ServerBuilder and added a new ThriftService to it.
ServerBuilder sb = new ServerBuilder();
sb.port(8080, SessionProtocol.HTTP);
//The ThriftService is bound at the path /hello and will use the TBinary format.
//We also decorated the ThriftService using LoggingService, which logs all Thrift calls and replies.
sb.serviceAt(
"/hello",
ThriftService.of(helloHandler, SerializationFormat.THRIFT_BINARY)
.decorate(LoggingService::new)//使用日志输出请求数据和响应数据
)
.serviceUnder("/docs/", new DocService());
//Armeria provides a service called DocService, which discovers all ThriftService in your Armeria server and lets you browse the available service operations and structs:
Server server= sb.build();
server.start();
System.out.println("ArmeriaServer start");
}
}
public class ArmeriaClient
{
public static void main(String[] args) throws TException
{
HelloService.Iface helloService = new ClientBuilder("tbinary+http://127.0.0.1:8080/hello")
.responseTimeoutMillis(10000)
.decorator(LoggingClient::new)
.build(HelloService.Iface.class);
String greeting = helloService.hello("Armerian World");
System.out.println("ArmeriaClient:"+greeting);
}
}
public class ArmeriaServer
{
public static void main(String[] args)
{
MetricRegistry metricRegistry = new MetricRegistry();
ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(1, TimeUnit.SECONDS);
HelloService.Iface helloHandler = new HelloServiceIfaceImpl();
//we created a new ServerBuilder and added a new ThriftService to it.
ServerBuilder sb = new ServerBuilder();
sb.port(8080, SessionProtocol.HTTP);
//The ThriftService is bound at the path /hello and will use the TBinary format.
//We also decorated the ThriftService using LoggingService, which logs all Thrift calls and replies.
sb.serviceAt(
"/hello",
ThriftService.of(helloHandler, SerializationFormat.THRIFT_BINARY)
.decorate(LoggingService::new).decorate(MetricCollectingService.newDropwizardDecorator(
metricRegistry, MetricRegistry.name("client", "HelloService")))//使用日志输出请求数据和响应数据
)
.serviceUnder("/docs/", new DocService())
;
//Armeria provides a service called DocService, which discovers all ThriftService in your Armeria server and lets you browse the available service operations and structs:
Server server= sb.build();
server.start();
System.out.println("ArmeriaServer start");
}
}