RPC 调用框架 springboot-MQRPC

码农软件 · 软件分类 · RPC/XMLRPC项目 · 2019-04-11 10:28:57

软件介绍

一个简单便捷的基于springboot+RabbitMQ中间件实现的RPC调用框架

远程调用过程如下

首先:消费者和生产者spring容器初始化的时候,会根据配置的的api在RabbitMQ上建立相应的队列,消费者会监听相关队列

1)生产者(client)调用以本地调用方式调用服务;

2)client 接收到调用后通过Hessian将方法、参数等组装成能够进行网络传输的消息体;

3)client 通过代理类,执行invoke方法,统一将消息发送到MQ监听的服务端;

4)server 收到消息后通过Hessian进行解码;

5)server 根据解码结果调用本地的服务;

6)本地服务执行并将结果返回给server ;

7)server 将返回结果通过Hessian打包发送至消费方;

8)client 接收到消息,并进行解码;

9)生产者得到最终结果。

客户端配置:


    <!-- RabbitMQ连接池 -->
    <task:executor id="amqpConnectionTaskExecutor" pool-size="5"/>

    <!-- RabbitMQ连接器 -->
    <rabbit:connection-factory id="connectionFactory" executor="amqpConnectionTaskExecutor" host="127.0.0.1" port="5672" username="admin" password="admin"
                               virtual-host="/kl"/>

    <bean id="myService" class="com.kl.client.MQClientProxyFactoryBean">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="serviceInterface" value="com.kl.api.Service"/>
    </bean>
服务端配置:



   <!-- RabbitMQ连接池 -->
    <task:executor id="amqpConnectionTaskExecutor" pool-size="5"/>

    <!-- RabbitMQ连接器 -->
    <rabbit:connection-factory id="connectionFactory" executor="amqpConnectionTaskExecutor" host="127.0.0.1" port="5672" username="admin" password="admin"
                               virtual-host="/kl"/>
    <bean id="ServiceImpl" class="com.kl.apiImpl.ServiceImpl"/>

    <bean id="MyServiceEndpoint" class="com.kl.server.MQServerEndpoint">
        <constructor-arg index="0" ref="ServiceImpl"/>
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>


测试代码及图例

    /**
     * 暴力测试
     * @param args
     */
    public static void main(String[] args) {
        GenericXmlApplicationContext context = new GenericXmlApplicationContext(
                "classpath:/applicationContext-client.xml");
        Service service = (Service) context.getBean("myService");
        new ClientTest().exec(service);
    }
    public void exec(Service service){
        ExecutorService executorService= Executors.newFixedThreadPool(30);
        for(int i=0;i<=30;i++){
            executorService.submit(new Task(service));
        }
    }
    private class Task implements Callable {
        private Service service;
        public Task(Service service){
            this.service=service;
        }
        @Override
        public Object call() throws Exception {
            for(int i=0;i<=100000;i++){
                System.out.println("servicEcho当前线程:"+Thread.currentThread().getName()+"| 线程任务数"+i+"| 输出:"+service.echo("Hello AMQP!"));
                System.out.println("serviceStudent当前线程:"+Thread.currentThread().getName()+"| 线程任务数"+i+"| 输出:"+service.getStudent(null).getName());

            }
            return null;
        }
    }



以下为测试图例
输入图片说明
输入图片说明
输入图片说明



本文地址:https://codercto.com/soft/d/3334.html

程序员健康指南

程序员健康指南

Joe Kutner / 陈少芸 / 人民邮电出版社 / 2014-9-20 / 31.60元

本书是为程序员量身制作的健康指南,针对头痛、眼部疲劳、背部疼痛和手腕疼痛等常见的问题,简要介绍了其成因、测试方法,并列出了每天的行动计划,从运动、饮食等方面给出详细指导,帮助程序员在不改变工作方式的情况下轻松拥有健康。 本书适合程序员、长期伏案工作的其他人群以及所有关心健康的人士阅读。一起来看看 《程序员健康指南》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具