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 }}