Spring+Hessian搭建遠程方法調用


1、hessian調用分為服務端和客戶端

2、總體圖

3、服務端配置

(1)web.xml

<!-- hessian 配置  默認會加載hessian-servlet.xml-->
    <servlet>
        <servlet-name>hessian</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hessian</servlet-name>
        <url-pattern>/hessian/*</url-pattern>
    </servlet-mapping>
    <!-- hessian 配置 -->

(2)Interface

public interface WebHessianService {
    void test();
}

(3)Implement

@Service("webHessianService")
public class WebHessianServiceImpl implements WebHessianService {

    public void test(){
          system.out.println();
    }
}

(4)HessianServerProxyExporter:

Servlet-API-based HTTP request handler that exports the specified service bean as Hessian service endpoint, accessible via a Hessian proxy.
public class HessianServerProxyExporter extends HessianServiceExporter {
    private static Logger logger = LoggerFactory.getLogger(HessianServerProxyExporter.class);
    PropertyUtil propertyUtil = PropertyUtil.getInstance("project");
    public String hessianAuth;

    public HessianServerProxyExporter() {
        this.hessianAuth = this.propertyUtil.getProperty("hessianAuth");
    }

    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("++++ hessian request clientIp:" + request.getRemoteAddr() + "++++requestData:" + request.getRequestURL());
        String auth = request.getHeader("hessianAuth");
        if(auth != null && auth.equalsIgnoreCase(this.hessianAuth)) {
            super.handleRequest(request, response);
        } else {
            logger.info("+++++hessianAuth->fail :" + request.getRemoteAddr() + "," + request.getRequestURL());
        }
    }
}
(5)ServiceBean
 <!--  Hessian訪問路徑 含權限認證 -->
    <bean name="/webHessian" class="com.qysxy.hessian.server.HessianServerProxyExporter">
        <property name="service" ref="webHessianService"/>
        <property name="serviceInterface">
            <value>
                com.qysxy.framework.hessian.WebHessianService
            </value>
        </property>
    </bean>

 4、客戶端配置

(1)HessianClientProxyFactory   :hessian客戶端代理

public class HessianClientProxyFactory extends HessianProxyFactory {
    PropertyUtil propertyUtil = PropertyUtil.getInstance("project");
    private final String hessianAuth;

    public HessianClientProxyFactory() {
        this.hessianAuth = this.propertyUtil.getProperty("hessianAuth");
    }

    protected URLConnection openConnection(URL url) throws IOException {
        URLConnection conn = super.openConnection(url);
        conn.setRequestProperty("hessianAuth", this.hessianAuth);
        return conn;
    }
}

(2)Interface

public interface WebHessianService {
    void test();
}

(3)clientBean:

    <bean id="webHessianService"
        class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceInterface"
            value="com.qysxy.framework.hessian.service.WebHessianService"></property>
        <property name="serviceUrl">
            <value>${hessianPath}/hessian/webHessian</value>
        </property>
        <property name="readTimeout">
            <value>100000</value>
        </property>
        <property name="proxyFactory">
            <bean
                class="com.*.hessian.client.HessianClientProxyFactory" />
        </property>
        <property name="chunkedPost" value="false" />
    </bean>

5、測試

 @Test
    public void hessian() {
        String url = "http://127.0.0.1/hessian/webHessian";
        HessianClientProxyFactory  factory = new HessianClientProxyFactory();
        try {
            WebHessianService service = (WebHessianService) factory.create(WebHessianService.class, url);
           service.test();
            System.err.println("map=" + map.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

 6、總結:

服務端是需要定義方法接口並且實現方法,利用servlet和spring的攔截器控制訪問;

客戶端可以用bean組件定義好調用方法接口、訪問URL、hessian代理工廠


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM