Armeria 小试牛刀



HelloService.thrift 定义服务接口

namespace java com.example.thrift

service HelloService {
    string hello(1:string name)
}

使用thrift-0.9.3.exe 工具,命令:thrift-0.9.3 -r --gen java HelloService.thrift 在当前文件夹gen-java 下面生成HelloService.java

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;
	}
}

ArmeriaServer 为RPC 端服务类:

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");
	}
}

ArmeriaClient 为RPC客户端类:

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);
	}
}

ArmeriaServer RPC服务端 增加数据分析、日志、文档。

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");
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值