开发者

关于dubbo的RPC和RESTful性能及对比

开发者 https://www.devze.com 2022-12-20 10:26 出处:网络 作者: fomeiherz
目录先上结论原因分析HTTP请求代码RPC代码总结先上结论 RPC请求的效率是HTTP请求的1.6倍左右,性能明显比HTTP请求要高很多。
目录
  • 先上结论
  • 原因分析
    • HTTP请求代码
    • RPC代码
  • 总结

    先上结论

    RPC请求的效率是HTTP请求的1.6倍左右,性能明显比HTTP请求要高很多。

    原因分析

    RESTful是基于HTTP协议进行交互的,HTTP协议包含大量的请求头、响应头信息。

    而dubbo是基于dubbo自定义的二进制协议进行传输,消息体比较简单,传输数据要小很多。

    关于dubbo的RPC和RESTful性能及对比

    HTTP请求代码

    // 服务端基于spring boot搭建
    // 服务端代码
    @SpringBootApplication
    @RestController
    public class DemoApplication {
    
        public static void main编程(String[] args) {编程客栈
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @RequestMapping("/helloworld")
        public String helloworld() {
            return "hello world";
        }
    
    }
    
    // 客户端代码
    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();
           php         }
                }
            }
        }
    }
    
    

    RPC代码

    // dubbo-demo工程的代码,详情请看:https://github.com/apache/dubbo/tree/master/dubbo-demo
    // 服务端
    public class DemoServiceImpl implements DemoService {
    
        JovRQcTh@Override
        public String sayHello(String name) {
            return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
        }
    }
    
    // 客户端
    public class Consumer {
    
        public static void main(String[] args) {
            //Prevent to get IP开发者_PythonV6 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();
                    }
                }
            }
        }
    }
    

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号