Armeria 小试牛刀

2024-02-18 06:58
文章标签 小试牛刀 armeria

本文主要是介绍Armeria 小试牛刀,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



HelloService.thrift 定义服务接口

namespace java com.example.thriftservice 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 
{@Overridepublic 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");}
}



这篇关于Armeria 小试牛刀的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/720394

相关文章

OrangePi AIpro小试牛刀-目标检测(YoloV5s)

非常高兴参加本次香橙派AI Pro,香橙派联合华为昇腾打造的一款AI推理开发板评测活动,以前使用树莓派Raspberry Pi4B 8G版本,这次有幸使用国产嵌入式开发板。 一窥芳容 这款开发板搭载的芯片是和华为昇腾的Atlas 200I DK A2同款的处理器,功耗低至8W 默认AI CPU和Control CPU 比例1:3, 芯片名称 310B1:表示芯片算

python小试牛刀编写批处理初步

bat越来越觉得不好用,取字符串是非常的麻烦,于是我打算用python开始编写,现在的雏形是这样的. #!/bin/env python#encoding: utf-8#"Non-ASCII character '\xe6' in file"import sysimport osprint("arg length:"+str(len(sys.argv)))# sys.argvfil

Python爬虫小试牛刀

学了几日Python爬虫,做了一个无聊的爬虫。。。 # -*- coding: utf-8 -*-import urllib.requestimport repage = urllib.request.urlopen("https://movie.douban.com/")cnt = page.read().decode("utf-8")#首次过滤name = re.findall(r

Ansible小试牛刀

Ansible 利用ssh通道管理多台主机,以实现批量操作主机的目的,适合运维开发测试使用。 使用步骤如下: 1、安装 yum install python-pip pip install ansible 2、配置ssh免密通道 ssh-keygen 生产公钥、私钥 ssh-copy-id root@192.168.37.122 远程复制公钥 vim ~/.ssh/config Host mk1

SQL的子查询与JOIN的小试牛刀

//学生表CREATE TABLE student(ID INT PRIMARY KEY,s_name VARCHAR(16) NOT NULL,class_id INT NOT NULL);INSERT INTO student VALUES(1, "qf", 3), (2, "lap", 3), (3, "qfa", 8)//班级表CREATE TABLE class(class

doxygen小试牛刀

今天试用了一下传说中的doxygen,用一句话概括就是:很强大,很方便! 在sourceforge下载了最新版本1.7.2(http://sourceforge.net/projects/doxygen/files/),又在网上找了几篇介绍文档,就开工研究了。总的来说,应用其GUI的配置向导很方便,下面把小试牛刀的应用心得小结如下:   (1)将其所有默认的UTF-8编码,替换成GB

Django学习之小试牛刀

六、Django学习之小试牛刀 其他关于Python Web开发笔记:(如果遇到问题可以一起交流~) 一、Flask学习之HTML-CSDN博客 二、Flask学习之CSS-CSDN博客 【接上篇】二、Flask学习之CSS(下篇)-CSDN博客 三、Flask学习之BootSrap-CSDN博客 四、Flask学习之JavaScript-CSDN博客 五、Flask学习之MyS

pywinauto小试牛刀

#encoding = utf-8from pywinauto import Applicationapp=Application().start("Notepad.exe")app['Notepad'].print_control_identifiers()app['Notepad'].menu_select(u'帮助(H)->关于记事本(A)')app[u'关于"记事本"'].print_co

makefile小试牛刀

使用make编译两个.c文件 //a.cvoid fun1(void);int main(){fun1();return 0;}//b.c#include <stdio.h>void fun1(){printf("this is creat by make file.\n");}//makefile内容//第一版a.exe:a.o b.ogcc -o a.exe a.o

GDPU 小试牛刀

自由发挥,尽力就行,答案无标准,你就是唯一! Take it easy! 前端 1. HTML 请问HTML的全称是什么? Hyper Text Markup Language 超文本标记语言 2. 文档流 请谈一谈你对文档流的理解?言简意赅最好 在前端开发中,文档流(Document Flow)指的是HTML文档中元素按照其在DOM(文档对象模型)中的先后顺序依次排列