RPC框架--missian框架

Missian簡介java

注:(創始者不明,應用於sina下面的公司(愛問))spring

Missian是一個構建於MinaHessian基礎上的異步RPC框架,可以兼容HTTP協議和TCP協議,能和Hessian互通兼容,它提供了:apache

 

一、一個基於mina的高性能服務器,具有mina全部的一切優秀基因。Missian服務器可以在一個端口之上兼容tcp和http協議格式,因 此開啓了Missian服務以後,便可以用Missian客戶端調用(同步/異步,HTTP/TCP),也能夠供Hessian客戶端調用。api

二、一個基於阻塞式的傳統Socket的同步客戶端,一樣支持HTTP和TCP,它可使用HTTP或TCP去訪問Missian服務,同時也能夠 使用HTTP方式去訪問Hessian服務。您能夠選擇使用短鏈接的方式,也可使用長鏈接(這時強制要求使用鏈接池,但Missian提供了一個基於 apache commons-pool的Socket鏈接池實現)。能夠根據具體狀況決定使用方式。服務器

三、一個基於Mina NioSocketConnector的異步客戶端。默認使用一個4個線程的線程池來處理回調,但開發者也能夠指定線程數,或者傳入一個存在的線程池。異 步客戶端只能調用Missian服務,而不可以調用Hessian服務(正在思考如何實現)。一樣,協議能夠是HTTP或者TCP。網絡

 

Mina: 是一個網絡通訊應用框架,也就是說,它主要是對基於TCP/IP、UDP/IP協議棧的通訊框,和netty框架相似;session

Hessian:採用的是二進制協議,因此它很適合於發送二進制數據的RPC框架,基於http的同步框架框架

 

missian主要實現異步

/**
* api: 接口
* url:遠程調用RPC服務器的地址
* loader :對象
**/
public
Object create(Class<?> api, String url, ClassLoader loader) { if (api == null) throw new NullPointerException( "api must not be null for HessianProxyFactory.create()"); InvocationHandler handler = null; TransportURL u = new TransportURL(url); handler = new SyncMissianProxy(u, this);            // 經過 hessian 遠程調度 初始化 hander return Proxy.newProxyInstance(loader, new Class[] { api,    // 經過動態代理實現接口 HessianRemoteObject.class }, handler); }

 

 

missian整合spring搭建使用socket

一、引入jar包

    <dependency>
            <groupId>missian</groupId>
            <artifactId>missian-wenwo</artifactId>  <!--  此包在私服暫時找不到,sina下的框架 -->
            <version>0.4.4</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

 

 

二、搭建服務器 beans-context-missian.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <bean
        class="org.springframework.beans.factory.config.CustomEditorConfigurer ">
        <property name="customEditors">
            <map>
                <entry key="java.net.SocketAddress">
                    <bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
                </entry>
            </map>
        </property>
    </bean>
    <!-- The IoHandler implementation -->
    <bean id="minaHandler" class="com.missian.server.handler.MissianHandler">
        <constructor-arg>
            <bean class="com.missian.common.beanlocate.SpringLocator" />
        </constructor-arg>
    </bean>

    <!-- the IoFilters -->
    <bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter" />
    <bean id="codecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
        <constructor-arg>
            <bean class="com.missian.server.codec.MissianCodecFactory " />
        </constructor-arg>
    </bean>
    <bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter">
        <property name="messageReceivedLogLevel" value="DEBUG" />
        <property name="messageSentLogLevel" value="DEBUG" />
        <property name="sessionCreatedLogLevel" value="DEBUG" />
        <property name="sessionClosedLogLevel" value="DEBUG" />
        <property name="sessionIdleLogLevel" value="DEBUG" />
        <property name="sessionOpenedLogLevel" value="DEBUG" />
    </bean>
    <!-- The non-SSL filter chain. -->
    <bean id="filterChainBuilder"
        class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
        <property name="filters">
            <map>
                <entry key="codecFilter" value-ref="codecFilter" />
                <entry key="executor" value-ref="executorFilter" />
                <entry key="loggingFilter" value-ref="loggingFilter" />
            </map>
        </property>
    </bean>
    <!-- The IoAcceptor which binds to port 1235 server side  對外開放端口 1235 -->
    <bean id="minaAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"
        init-method="bind" destroy-method="unbind">
        <property name="defaultLocalAddress" value=":1235" />
        <property name="handler" ref="minaHandler" />
        <property name="reuseAddress" value="true" />
        <property name="filterChainBuilder" ref="filterChainBuilder" />
    </bean>
    
    <bean id="missianFactory" class="com.missian.client.sync.SyncMissianProxyFactory">
        <constructor-arg index="0">
            <bean class="com.missian.client.NetworkConfig">
                <property name="readTimeout" value="60000"/>
                <property name="connectTimeout" value="60000"/>
                <property name="receiveBufferSize" value="2048"/>
            </bean>
        </constructor-arg>
    </bean>
    
</beans>

 

三、搭建客戶端beans-context-missianclient.xml,能夠訪問其它的missan服務器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="weiboBigNodeFacade" factory-bean="missianFactory" factory-method="create">
        <constructor-arg index="0" value="com.wenwo.weiboproxy.service.WeiboBigNodeFacade" type="java.lang.Class" />   <!-- 要調用的接口 ,此接口必須在missan服務器中實現了,獲取交給了spring管理-->
        <constructor-arg index="1" value="tcp://127.0.0.1:1235/weiboBigNodeFacade" />                     <!--  調用的服務器和接口  -->
    </bean>
    
</beans>

 

四、使用RPC

 

這就是使用rpc的過程

相關文章
相關標籤/搜索