hessian是一个采用二进制格式传输的服务框架,相对传统的SOAP web service,更轻捷。官网地址:http://hessian.caucho.com/,该公司还有一个比较有意思的web服务框架:http://baratine.io/,感兴趣的可以尝试玩下见入门教程http://doc.baratine.io/v1.0/tutorials/hello-eclipse-maven/。
Dubbo提供了RPC服务、服务治理,而Spring Cloud则涵盖了微服务的方方面面,服务治理只是其中的一个方面,关于二者的比较,可以参看程序猿DD的一篇博客–微服务架构的基础框架选择:Spring Cloud还是Dubbo?。尽管国内大部分互初创联网公司几乎是用Zookeeper+Dubbo搭建的架构,而Spring Cloud商用成熟产品少之又少,但是笔者个人觉得未来短时间之内Spring Cloud必火,各位不要错过了。转入正题,我们已经有了Dubbo、Spring Cloud为什么还要了解Hessian呢?因为
那么接下来就好理解了,Hessian是一个使用Http协议进行远程方法调用的框架。 使用Http协议,必须有一个Web服务(Hessian服务端或者叫提供者–dubbo的叫法); 应用端亦即消费端通过方法调用的形式获得代理对象,调用服务端的接口实现方法; 应用端和服务端都需要持有公共服务的接口信息;
使用hessian需要引入依赖:
<!-- 引入hessian依赖 -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
我们以一个简单的例子入门:获取一个用户的信息。
该接口定义了两个简单方法:
HelloService.java
package org.byron4j.hessian.service;
import org.byron4j.hessian.entity.User;
/**
* @author Byron.Y.Y
* @optDate 2016年12月6日
* This class is for ...
*/
public interface HelloService {
public String helloWorld(String message);
public User getMyInfo(User user);
}
用户类是一个简单的POJO,需要实现Serializable接口以序列化 User.java
package org.byron4j.hessian.entity;
import java.io.Serializable;
import java.util.Map;
/**
* @author Byron.Y.Y
* @optDate 2016年12月6日
* This class is for ...
*/
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = -1352128884522619903L;
private String userName;
private int age;
private Map<String, Object> detailData;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Map<String, Object> getDetailData() {
return detailData;
}
public void setDetailData(Map<String, Object> detailData) {
this.detailData = detailData;
}
}
公共服务需要打包成jar包,hessian服务端、client端都需要依赖该jar;或者使用maven构建的话将公共服务需要打包成jardeploy到私服中,hessian服务端、client端都通过maven坐标引入依赖(本系列文章采取的方式)。
服务端必须暴露服务,我们将其发构建成一个web服务,在web.xml中配置指定hessian服务的接口以及具体的实现服务、servlet;该工程只有一个实现HelloService接口的类HelloServiceImpl并且继承自HessianServlet。
HelloServiceImpl.java
package org.byron4j.hessian.service.impl;
import org.byron4j.hessian.entity.User;
import org.byron4j.hessian.service.HelloService;
import com.caucho.hessian.server.HessianServlet;
/**
* @author Byron.Y.Y
* @optDate 2016年12月6日
*
*/
public class HelloServiceImpl extends HessianServlet implements HelloService{
/**
*
*/
private static final long serialVersionUID = -3537274030227675984L;
@Override
public String helloWorld(String message) {
return "Hello, " + message;
}
@Override
public User getMyInfo(User user) {
if(null == user){ return new User(); }
user.setAge(99);
return user;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>hessian-showcase</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hessian-service</servlet-name>
<servlet-class>
com.caucho.hessian.server.HessianServlet
</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>
<!-- 服务实现类 -->
org.byron4j.hessian.service.impl.HelloServiceImpl
</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<!-- 服务接口 -->
<param-value>
org.byron4j.hessian.service.HelloService
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessian-service</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.byron4j</groupId>
<artifactId>hessian</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>hessian</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 引入hessian依赖 -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
<!-- 引入接口服务定义(公共服务接口jar包) -->
<dependency>
<groupId>org.byron4j</groupId>
<artifactId>hessianMiddleJar</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 引入servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
</dependencies>
<build>
<finalName>hessionDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
client端同样需要依赖公共服务jar,该项目只有一个单元测试类HessianClient。使用HessianProxyFactory创建接口服务
HessianProxyFactory factory = new HessianProxyFactory();
HelloService helloService = (HelloService) factory.create(HelloService.class, url);
helloService.helloWorld("kitty!")
HessianClient.java
package org.byron4j.hessionClient;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.byron4j.hessian.entity.User;
import org.byron4j.hessian.service.HelloService;
import org.junit.Test;
import com.caucho.hessian.client.HessianProxyFactory;
/**
* Hello world!
*
*/
public class HessianClient
{
static ExecutorService es = Executors.newFixedThreadPool(10);
/**
* 测试hessian服务是否可用,并要求在2秒内获得响应
* @throws MalformedURLException
*/
@Test//(timeout = 2000)
public void testService4Success() throws MalformedURLException {
String url = "http://localhost:8080/hessionDemo/hessian";
System.out.println("请求的服务端地址:" + url);
HessianProxyFactory factory = new HessianProxyFactory();
HelloService helloService = (HelloService) factory.create(HelloService.class, url);
System.out.println("服务端返回结果为:" + helloService.helloWorld("kitty!"));
HashMap<String, Object> detailData = new HashMap<String, Object>();
detailData.put("isMarried", "N");
detailData.put("gender", "F");
User user = new User();
user.setAge(18);
user.setUserName("OYY");
user.setDetailData(detailData);
int time = 100000;
long startTime = System.currentTimeMillis();
for(int i = 0; i< time; i++){
es.execute(new Runnable() {
@Override
public void run() {
helloService.getMyInfo(user);
}
});
}
System.out.println(time + "次调用耗时:" + (System.currentTimeMillis() - startTime));
System.out.println("+--获得复杂对象:" );
System.out.println(" +--新年龄:" + helloService.getMyInfo(user).getAge());
System.out.println(" +--隐私信息:" + helloService.getMyInfo(user).getDetailData());
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.byron4j</groupId>
<artifactId>hessionClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hessionClient</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 引入hessian依赖 -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
<!-- 引入接口服务定义(公共服务接口jar包) -->
<dependency>
<groupId>org.byron4j</groupId>
<artifactId>hessianMiddleJar</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>hessionClientDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
运行client单元测试结果如下,本实例还增加了一个线程实例用于测试单线程高访问量、多线程多访问量使用hessian RMI调用的时间。
请求的服务端地址:http://localhost:8080/hessionDemo/hessian
服务端返回结果为:Hello, kitty!
100000次调用耗时:33
+--获得复杂对象:
+--新年龄:99
+--隐私信息:{isMarried=N, gender=F}
Hessian远程调用框架学习一,第一个JAVA使用hessian的入门demo,简单实用易理解 http://download.csdn.net/detail/zixiao217/9709949