[email protected]:chenyaoBOY/hessian.git
https://github.com/chenyaoBOY/hessian.git
Hessian是一个轻量级的RPC框架,基于HTTP协议
三部曲
代码示例
1.1.1pom文件 : spring pom文件 + Hessian pom文件
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
<spring.version>4.1.7.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.studygroupId>
<artifactId>hessian-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.cauchogroupId>
<artifactId>hessianartifactId>
<version>4.0.38.1version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<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>serviceservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>serviceservlet-name>
<url-pattern>/service/*url-pattern>
servlet-mapping>
1.1.4 配置applicationContext.xml
<bean id="demoService" class="demo.impl.DemoServiceImpl" />
<bean name="/demo" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="demoService" />
<property name="serviceInterface" value="demo.DemoService"/>
bean>
综上 服务端的所有配置都已经完毕,启动Tomcat 端口8080 Hessian的服务路径就是
localhost:8080/service/demo
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 通过配置文件配置
<bean id="demoService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8080/service/demovalue>
property>
<property name="serviceInterface">
<value>demo.DemoServicevalue>
property>
bean>
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