在最近給客戶作的兩個系統之間,有一些須要相互調用的地方,由於兩個系統的架構基本相同,都是採用的spring mvc + mybatis,因此嘗試使用了一下spring httpinvoker。目前系統訪問平穩,用戶量也不是很大,沒有出現什麼問題。 java
如今常見的遠程調用,主要有RMI、 spring httpinvoker、 hessian、 burlap、 web services、 protobuf等。各類方式都有本身的優缺點,網上有不少相關的對比,這裏就再也不贅述。可是對於兩個都是java開發的系統而言,spring httpinvoker應該是綜合了傳輸效率和開發效率的比較好的方式。 web
使用了maven開發,三個工程: spring
hello-remoting-interface apache
hello-remoting-server spring-mvc
hello-remoting-client mybatis
定義接口和使用到的實體 架構
pom.xml文件 mvc
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.riwcwt</groupId> <artifactId>hello-remoting-interface</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>hello-httpinvoker-interface</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.2.10.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.5</version> </dependency> </dependencies> </project>
UserService.java app
package com.riwcwt.remoting.service; import java.util.List; import com.riwcwt.remoting.entity.Role; import com.riwcwt.remoting.entity.User; public interface UserService { public List<User> getAllUsers(); public List<Role> getRolesByUser(User user); }
服務提供方 jsp
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/*-context.xml</param-value> </context-param> <!-- 防止發生java.beans.Introspector內存泄露,應將它配置在ContextLoaderListener的前面 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 設置WEB應用字符集,也是經過過濾器完成的 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring MVC 的Servlet,它將加載WEB-INF/mvc-dispatcher.xml 的 配置文件, 以啓動Spring MVC模塊 --> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/mvc-dispatcher.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> </web-app>
UserServiceImpl.java
package com.riwcwt.remoting.service; import java.util.LinkedList; import java.util.List; import org.apache.log4j.Logger; import org.springframework.stereotype.Service; import com.riwcwt.remoting.entity.Role; import com.riwcwt.remoting.entity.User; @Service(value = "userService") public class UserServiceImpl implements UserService { private static Logger logger = Logger.getLogger(UserServiceImpl.class); @Override public List<User> getAllUsers() { logger.info("invoke the first one"); List<User> users = new LinkedList<User>(); for (int i = 0; i < 2; i++) { User user = new User(); user.setUsername("username " + i); user.setDescription("description " + i); List<Role> roles = new LinkedList<Role>(); Role admin = new Role(); admin.setRoleName("amin"); Role manager = new Role(); manager.setRoleName("manager"); roles.add(admin); roles.add(manager); user.setRoles(roles); users.add(user); } return users; } @Override public List<Role> getRolesByUser(User user) { logger.info("invoke the second one"); List<Role> roles = new LinkedList<Role>(); Role admin = new Role(); admin.setRoleName("amin"); Role manager = new Role(); manager.setRoleName("manager"); roles.add(admin); roles.add(manager); return roles; } }
mvc-dispatcher.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:aop="http://www.springframework.org/schema/aop" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 設置使用註解的類所在的jar包 --> <context:component-scan base-package="com.riwcwt.remoting.service"></context:component-scan> <bean name="/user-service" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="userService" /> <property name="serviceInterface" value="com.riwcwt.remoting.service.UserService" /> </bean> </beans>
客戶端調用服務
application-context.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <!-- 導入屬性配置文件 --> <context:property-placeholder location="classpath*:remoting.properties" /> <bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://${remoting.host}:${remoting.port}/${remoting.context}/user-service"> </property> <property name="serviceInterface" value="com.riwcwt.remoting.service.UserService"> </property> <!-- 修改默認的HTTP請求連接方式,提高鏈接效率 --> <property name="httpInvokerRequestExecutor"> <bean class="org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor"> </bean> </property> </bean> </beans>
測試用例代碼
package com.riwcwt.remoting.service; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.riwcwt.remoting.entity.Role; import com.riwcwt.remoting.entity.User; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/spring/application-context.xml") public class UserServiceTest { @Resource(name = "userService") private UserService userService = null; @Test public void testGetAllUsersByHttpInvoker() throws InterruptedException { List<User> users = this.userService.getAllUsers(); for (User user : users) { System.out.println(user.getUsername() + " " + user.getDescription()); for (Role role : user.getRoles()) { System.out.println(role.getRoleName()); } } } @Test public void anotherTest() { for (int i = 0; i < 5; i++) { User user = new User(); user.setUsername("test"); List<Role> roles = this.userService.getRolesByUser(user); for (Role role : roles) { System.out.println(role.getRoleName()); } } } }
server和client工程都依賴於interface工程,這樣就方便於開發。經過上面的例子,能夠看出spring httpinvoker是很方便於作遠程調用的,其實spring對於rmi、hessian、burlap也提供了幾乎和上面配置同樣的調用,只是serviceexpoter和factoorybean不一樣,這裏就再也不一一作示例。