源码地址
[email protected]:chenyaoBOY/hessian.git
https://github.com/chenyaoBOY/hessian.git
1.Hessian使用的介绍
Hessian是一个轻量级的RPC框架,基于HTTP协议
1.1 服务端配置
三部曲
- 通过springMVC的DispatherServlet控制Hessian服务接口的URL访问路径 即web.xml配置
- 新建Hessian服务 例如:DemoService 和实现类 DemoServiceImpl
- 配置Hessian服务的暴露 通过HessianServiceExporter
代码示例
1.1.1pom文件 : spring pom文件 + Hessian pom文件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.1.7.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Hessian的包 -->
<dependency>
<groupId>org.study</groupId>
<artifactId>hessian-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38.1</version>
</dependency>
<!-- 添加Spring支持 -->
<!-- 核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- pring IOC的基础实现,包含访问配置文件、创建和管理bean等 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
1.1.2 新建Hessian服务
//接口
public interface DemoService {
String sayHello(String content);
}
import demo.DemoService;
//实现类
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String content) {
System.out.println("content = " + content);
return content;
}
}
1.1.3 配置web.xml
<servlet>
<servlet-name>service</servlet-name>
<!-- 使用Spring的代理Servlet -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 加载spring配置文件 需要定义该配置文件 -->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>service</servlet-name>
<!-- 配置servlet拦截路径 即Hessian的访问拦截路径 http:.../service/...-->
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
1.1.4 配置applicationContext.xml
<!-- 接口的具体实现类 -->
<bean id="demoService" class="demo.impl.DemoServiceImpl" />
<!-- 使用Spring的HessianServie做代理 -->
<!-- name属性是该服务的访问路径 结合servlet的路径就是 .../service/demo -->
<bean name="/demo" class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- service引用具体的实现实体Bean-->
<property name="service" ref="demoService" />
<!-- 暴露的服务 -->
<property name="serviceInterface" value="demo.DemoService"/>
</bean>
综上 服务端的所有配置都已经完毕,启动Tomcat 端口8080 Hessian的服务路径就是
localhost:8080/service/demo
2 Hessian客户端的调用
2.1比较简单的方式
通过 HessianProxyFactory 工厂 获取具体的服务接口,然后入参 Hessian服务的URL即可
新建的工程 只需导入Hessian的pom文件即可,或者直接在服务端的工程中测试
public void test(){
HessianProxyFactory factory = new HessianProxyFactory();
String url = "http://localhost:8080/service/demo";
try {
//入参两个参数 一个是接口的class 一个是接口的URL路径
DemoService service = (DemoService) factory.create(DemoService.class, url);
String hello = service.sayHello("hello world !");
//输出hello wolrd!
System.out.println(hello);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
这里要特别注意URL路径的问题,如果Tomcat配置了访问的项目名称,则需要在访问的时候加上项目名称
例如IDEA的使用如下
当配置了ApplicationContext的时候,需要在URL路径上配置该项目名称
localhost:8080/hessian-server/service/demo
2.2 通过配置文件配置
- 新建client客户端工程,配置pom文件 需要spring和Hessian的jar ,和服务端配置一样即可,还要依赖Hessian服务接口 DemoService
- 创建spring 的配置文件 ,配置Hessian服务的消费
代码示例
spring配置文件applicationContext.xml
<!-- 客户端Hessian代理工厂Bean -->
<bean id="demoService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!-- 请求代理Servlet路径 -->
<property name="serviceUrl">
<value>http://localhost:8080/service/demo</value>
</property>
<!-- 接口定义 -->
<property name="serviceInterface">
<value>demo.DemoService</value>
</property>
</bean>
启动spring容器 测试接口调用
public class DemoClient {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DemoService service = (DemoService) context.getBean("demoService");
String hello = service.sayHello("say hello");
System.out.println(hello);
}
}
最后贴出源码地址
[email protected]:chenyaoBOY/hessian.git
https://github.com/chenyaoBOY/hessian.git