(iv) Remote service: Hessian Service Implementation remote invocation

Source: Internet
Author: User

A long time ago, we shared the process of RMI providing remote services
https://blog.csdn.net/bestcxx/article/details/79488165
In this article we continue to understand the process of Hessian providing remote services

Compared to RMI, Hessian is a cross language

RMI is a remote invocation process solution provided by the Java language itself that requires both the provider and the caller of the service to be the Java language, and because Hessian is externally similar to an HTTP service, it can be accessed across languages. In extension, because RMI needs to register the port and a randomly allocated data communication port, causing it to encounter firewall problems, and Hessian with the aid of HTTP service, can avoid this problem.

Hessian based on binary message communication

Like RMI, Hessian also communicates through binary messages.

Spring 4 Combined Hessian 4

Maven dependencies introduce Hessian packages-server-side and client

<dependency>
    <groupId>com.caucho</groupId>
    <artifactid>hessian</artifactid >
    <version>4.0.38</version>
</dependency>
Special configuration of the service side

Similar to the RMI Service release, the Hessian service must first declare the local service as the Hessian service

/**
 * Registered as remote service
 * @return
/@Bean public
hessianserviceexporter Hessianexportedapiservice () {
    hessianserviceexporter exporter=new hessianserviceexporter ();
    Exporter.setservice (Getapiservice ());
    Exporter.setserviceinterface (apiservice.class);
    return exporter;
}   

/**
 * Registered as spring Bean
 * @return * * *
@Bean public 
apiservice Getapiservice () {return
    new Apiserviceimpl ();
}

In addition, you need to set a URL map that will be processed by the Hessian service when the Web Access request conforms to the mapping

/**
 * Set URL mapping * *
@Bean public
handlermapping hessianmapping () {
    simpleurlhandlermapping Shm=new simpleurlhandlermapping ();
    Properties Properties=new properties ();
    Properties.put ("/api.service", "Hessianexportedapiservice");
    Shm.setmappings (properties);
    return SHM;
}

The last server is a Web project, so the Web.xml content is included

<servlet-mapping>  
<servlet-name>springservlet</servlet-name>  
<url-pattern>/ </url-pattern> 
<!--  <url-pattern>*.service</url-pattern>   -->
</ Servlet-mapping>
calls from the client

The client's invocation process is much simpler than the service side

/**
     * Set proxy for remote service
     * @return * * *
     @Bean public
    Hessianproxyfactorybean Apiservice () {
        Hessianproxyfactorybean proxy=new Hessianproxyfactorybean ();
        Proxy.setserviceurl ("Http://localhost:8086/hessian-provider/api.service");
        Proxy.setserviceinterface (apiservice.class);
        return proxy;
    

GitHub Complete Example

Https://github.com/Bestcxy/RPC/tree/master/hessian
HESSIAN-API defines an interface
Hessian-provider offers services
Hessian-consumer Invoke Hessian Service

Call result

Start two Web projects, Access http://localhost:8085/hessian-consumer/

{"result": "Success", "Name": "Service End: User entered name =jecket", "model": {"UserName": "Jecket", "Age": 20 }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.