關(guān)于dubbo的RPC和RESTful性能及對(duì)比
先上結(jié)論
RPC請(qǐng)求的效率是HTTP請(qǐng)求的1.6倍左右,性能明顯比HTTP請(qǐng)求要高很多。
原因分析
RESTful是基于HTTP協(xié)議進(jìn)行交互的,HTTP協(xié)議包含大量的請(qǐng)求頭、響應(yīng)頭信息。
而dubbo是基于dubbo自定義的二進(jìn)制協(xié)議進(jìn)行傳輸,消息體比較簡(jiǎn)單,傳輸數(shù)據(jù)要小很多。

HTTP請(qǐng)求代碼
// 服務(wù)端基于spring boot搭建
// 服務(wù)端代碼
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/helloworld")
public String helloworld() {
return "hello world";
}
}
// 客戶(hù)端代碼
import org.springframework.util.StopWatch;
import org.springframework.web.client.RestTemplate;
public class HelloworldTest {
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
StopWatch stopWatch = new StopWatch();
stopWatch.start();
for (int j = 0; j < 10; j++) {
System.out.println("------------------");
for (int i = 1; i <= 10000; i++) {
restTemplate.getForObject("http://127.0.0.1/helloworld", String.class);
if (i % 1000 == 0) {
stopWatch.stop();
System.out.println(stopWatch.getTotalTimeSeconds());
stopWatch = new StopWatch();
stopWatch.start();
}
}
}
}
}
RPC代碼
// dubbo-demo工程的代碼,詳情請(qǐng)看:https://github.com/apache/dubbo/tree/master/dubbo-demo
// 服務(wù)端
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
// 客戶(hù)端
public class Consumer {
public static void main(String[] args) {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
StopWatch stopWatch = new StopWatch();
stopWatch.start();
for (int j = 0; j < 10; j++) {
System.out.println("-----------");
for (int i = 1; i <= 10000; i++) {
demoService.sayHello("world"); // call remote method
if (i % 1000 == 0) {
stopWatch.stop();
System.out.println(stopWatch.getTotalTimeSeconds());
stopWatch = new StopWatch();
stopWatch.start();
}
}
}
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Nacos在執(zhí)行startup.cmd的時(shí)候出現(xiàn)閃退的問(wèn)題
因?yàn)樵诠ぷ髦械捻?xiàng)目中需要使用到nacos作為注冊(cè)中心,但是在使用nacos的過(guò)程中運(yùn)行startup.cmd的時(shí)候出現(xiàn)了閃退的情況,運(yùn)行startup.cmd閃一下就沒(méi)有了,我把解決這個(gè)問(wèn)題的全過(guò)程理了一下,希望能幫到您,需要的朋友可以參考下2023-12-12
SpringBoot?緩存預(yù)熱的實(shí)現(xiàn)
本文主要介紹了SpringBoot?緩存預(yù)熱的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2007-11-11
Spring Boot Jar 包部署腳本的實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于Spring Boot Jar 包部署腳本的實(shí)例講解內(nèi)容,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。2021-12-12
Java多線(xiàn)程之深入理解ReentrantLock
這篇文章主要介紹了Java多線(xiàn)程之深入理解ReentrantLock,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
如何解決EasyExcel導(dǎo)出文件LocalDateTime報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了如何解決EasyExcel導(dǎo)出文件LocalDateTime報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
詳解Java使用JDBC連接MySQL數(shù)據(jù)庫(kù)
本文詳細(xì)講解了Java使用JDBC連接MySQL數(shù)據(jù)庫(kù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
Java Swing JButton按鈕的實(shí)現(xiàn)示例
這篇文章主要介紹了Java Swing JButton按鈕的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

