一、什么是RPC
RPC全稱Remote Procedure Call,中文名叫遠程過程調用。RPC是一種遠程調用技術,用於不同系統之間的遠程相互調用。其在分布式系統中應用十分廣泛。
二、什么是Hessian
Hessian是一個輕量級的RPC框架。 相比WebService,Hessian更簡單、快捷。采用的是二進制RPC協議,因為采用的是二進制協議,所以它很適合於發送二進制數據。
三、Hessian的使用
1、引入jar包
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency>
2.編寫業務代碼(和普通的業務代碼一樣)
public interface UserService { String getUserInfoById (Integer id); } @Component("uservice") public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; public String getUserInfoById(Integer id) { User user = userMapper.selectByPrimaryKey(id); return user.toString(); } }
3.創建並加載hessian-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 使用Spring的HessianServie做代理 --> <bean name="/userServiceImpl" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- service引用具體的實現實體Bean--> <property name="service" ref="uservice" /> <property name="serviceInterface" value="com.myproject.hessian.UserService" /> </bean> </beans>
public class HessianServiceProxyExporter extends HessianServiceExporter { private static final Base64 base64 = new Base64(); @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String authorization = request.getHeader("Authorization"); if(authorization != null && authorization.length() > 0){ String userAndPwd = new String(base64.decode(authorization.split(" ")[1])); String user = userAndPwd.split(":")[0]; String password = userAndPwd.split(":")[1]; if("user".equalsIgnoreCase(user) && "123456".equalsIgnoreCase(password)) { super.handleRequest(request, response); } else { System.out.println("您無權調用"); } } } }
<beans> <!-- 自己定義代理類來繼承org.springframework.remoting.caucho.HessianServiceExporter類,然后進行權限等一系列操作--> <bean name="/userServiceImpl" class="com.myproject.hessian.exporter.HessianServiceProxyExporter"> <!-- service引用具體的實現實體Bean--> <property name="service" ref="uservice" /> <property name="serviceInterface" value="com.myproject.hessian.UserService" /> </bean> </beans>
<!-- web.xml中進行攔截,並加載配置文件hessian --> <servlet> <servlet-name>hessian</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:hessian-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hessian</servlet-name> <url-pattern>/hessian/*</url-pattern> </servlet-mapping>
5.客戶端編寫
①普通方式調用。同樣需要引入hessian的jar包
//先將服務端的服務接口搬過來,包名和類名方法名最好是要一模一樣 public interface UserService { String getUserInfoById (Integer id); } public class Test { public static void main(String[] args) throws MalformedURLException { String url = "http://localhost:8080/zmyproject/hessian/userServiceImpl"; HessianProxyFactory factory = new HessianProxyFactory();
factory.setUser("user");
factory.setPassword("123456");
UserService userService = (UserService)factory.create(UserService.class, url);
System.out.println(userService.getUserInfoById(2)); } }
②spring框架調用
Ⅰ、先引入jar包,注意jar包的版本我使用的hession-3.1.5.jar,啟動會找不到一個factory類,所以用了4.0.38版本的。
Ⅱ、配置hession-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" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <!--客戶端Hessian代理工廠Bean--> <bean id="userService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <!--這是因為接口中出現方法重載,在調用時,服務器端會跑出異常 。在整合spring中,在客戶端的配置里面加上如下代碼可以解決:--> <property name="overloadEnabled" value="true" /> <!--請求代理Servlet路徑:--> <property name="serviceUrl" value="http://localhost:8080/zmyproject/hessian/userServiceImpl" /> <!--接口定義:--> <property name="serviceInterface" value="com.myproject.hessian.UserService" /> <property name="username" value="user" /> <property name="password" value="123456" /> </bean> </beans>
<!-- web.xml中配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:hession-servlet.xml</param-value> </context-param>
//和hessian服務端一樣的接口
public interface UserService {
String getUserInfoById (Integer id);
}
@Controller//將該類標注為處理器,並且加入spring容器中
@RequestMapping("/hessian")
public class HessianController{
@Autowired
private UserService userService;
@RequestMapping(value="/getInfo",method=RequestMethod.GET)
@ResponseBody
public String getInfo(HttpServletRequest request,HttpServletResponse response){
String userInfo = userService.getUserInfoById(1);
return userInfo;
}
}