1 为什么集成Hessian到Spring中?
2 开发步骤:
(1) 下载spring(包含hessian)
下载地址:http://sourceforge.net/projects/springframework/files/springframework-2/2.5.5/
或者从我的云盘上下载: http://pan.baidu.com/s/1tfBQS
(2) 创建动态web工程
注意选择tomcat服务器,以及选择生成web.xml
这是最终的工程视图:
(3) 引入jar
右击工程,选择configure build path,add external jars,将所需的jar文件都加进来。
在我提供的源码下载中有需要的所有jar文件。
(4) 创建接口和实现类
参考我这篇博文: http://blog.csdn.net/jiyiqinlovexx/article/details/17284559
(5) 配置
主要是配置文件,服务的话随便写接口方法即可。由于是基于http,必须起web服务,通过spring的mvc适配器把请求转发给hessian。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HessianProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
</web-app>
hello-servlet.xml(命名格式: ServletName-servlet.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean id="helloService" class="com.jiq.hessian.HelloService">
<!-- IOC,any additional properties, maybe a DAO? -->
</bean>
<bean name="/helloService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="helloService"/>
<property name="serviceInterface" value="com.jiq.hessian.IHelloService"/>
</bean>
</beans>
(6) 创建server,部署项目
先写一个jsp来测试服务是否正确(hello.jsp):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="com.caucho.hessian.client.HessianProxyFactory" %>
<%@ page import="com.jiq.hessian.IHelloService" %>
<%
HessianProxyFactory factory = new HessianProxyFactory();
String url = "http://localhost:80/HessianProject/hello/helloService";
IHelloService proxy = (IHelloService)factory.create(IHelloService.class, url);
out.print(proxy.SayHello());
%>
在view中打开servers试图,空白处右击,创建server,选择你的tomcat,启动服务器
浏览器中输出入: http://localhost/HessianProject/hello.jsp 访问。
(7) 启动,classNotFound(DispatcherServlet等)错误,修改部署路径
右击工程,properties,选择deployment Assembly,点击add,选择java build path entries,将所需的spring的jar文件都加进去。
然后再启动server就不会有这个问题了。
(8) 开发客户端
配置文件:hello-client.xml(名字任取)。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:80/HessianProject/hello/helloService"/>
<property name="serviceInterface" value="com.jiq.hessian.IHelloService"/>
</bean>
</beans>
测试程序:
package com.jiq.clientTest;
import java.net.MalformedURLException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.caucho.hessian.client.HessianProxyFactory;
import com.jiq.hessian.IHelloService;
public class HessianClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
//调用方式1
// String url = "http://localhost:80/HessianProject/hello/helloService";
// HessianProxyFactory factory = new HessianProxyFactory();
//
// try
// {
// IHelloService proxy =(IHelloService)factory.create(IHelloService.class, url);
// System.out.println(proxy.SayHello());
// }
// catch (MalformedURLException e) {
// e.printStackTrace();
// }
//调用方式2
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("./com/jiq/clientTest/hello-client.xml"));
IHelloService helloService = (IHelloService)factory.getBean("helloService");
System.out.print(helloService.SayHello());
//调用方式3
// ApplicationContext context=new ClassPathXmlApplicationContext("hello-client.xml");
// IHelloService service1=(IHelloService)context.getBean("helloService");
// System.out.print(service1.SayHello());
}
}
3 工程源码下载:
其中包含了工程zip文件,以及所需的所有jar文件。
====================================== 问题自我总结 =================================================
把我的问题理一下,下一步解决:
1、 为什么集成到Spring?为了方便利用Spring的IoC容器进行管理么?
2、Hessian的服务端能够脱离Tomcat等Web容器?安装为Windows服务可能么?
不过好像没必要,因为我的服务端是要跨平台的,既要在windows上部署,也要在Linux上部署。
脱离Web容器也没必要。
3、如何与Mina结合,利用Mina的快速,长连接等特点?
4、那个什么Invoker是怎么回事?
5、离我的高性能分布式服务框架还有多远?
梁飞说过一句话: 在分布式服务框架中,RPC只是基石,当应用全面服务化后,服务治理才是关键,这也是Dubbo的一个工作重心,但这一次开源的不包含治理模块。
顿时让我觉得走向高性能分布式服务框架的路,还有好长,好长。。。 希望环境对我有利!!!
我今天突然想到,对服务做负载均衡了,数据库呢?所有这些不同机器上的服务访问同一个数据库也是问题吧。据说mysql能同时处理几十万的请求,不大清楚,如果要更好的性能是不是需要针对数据库进行分表,分库呢? 需要继续研究。。。。。。。 果然路很长