一、前言
基于springframework的spring-web的服务端服务注册的org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter定义类、客户端代理调用org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean代理工厂类,详情定义及其使用过程参见下面代码示例(改代码示例完整项目代码可以到相关资源下载页面下载)。
二、服务端代码示例
1. 定义需注册开放给远程调用的服务接口及其实现类
package com.xwood.springmvc.test.remote;@b@@b@public interface RemoteService {@b@@b@ void invoke();@b@}
package com.xwood.springmvc.test.remote;@b@@b@public class RemoteServiceImpl implements RemoteService {@b@@b@ @Override@b@ public void invoke() {@b@ System.out.println("remote service is invoked!!!");@b@ }@b@@b@}
2. spring的xml配置文件
<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"@b@ xmlns:context="http://www.springframework.org/schema/context"@b@ xmlns:mvc="http://www.springframework.org/schema/mvc"@b@ xsi:schemaLocation="http://www.springframework.org/schema/beans@b@ http://www.springframework.org/schema/beans/spring-beans-4.1.xsd @b@ http://www.springframework.org/schema/context@b@ http://www.springframework.org/schema/context/spring-context-4.1.xsd @b@ http://www.springframework.org/schema/mvc@b@ http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">@b@@b@ <context:component-scan base-package="com.xwood.springmvc" />@b@@b@ <context:annotation-config /> @b@ @b@ ...@b@@b@ <bean name="remote" class="com.xwood.springmvc.test.remote.RemoteServiceImpl" />@b@ <bean name="/remoteservice"@b@ class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">@b@ <property name="service" ref="remote" />@b@ <property name="serviceInterface"@b@ value="com.xwood.springmvc.test.remote.RemoteService" />@b@ </bean>@b@@b@</beans>
3.web.xml配置,部署到tomcat(我这边测试maven项目)
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@ xmlns="http://java.sun.com/xml/ns/javaee"@b@ xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"@b@ id="WebApp_ID" version="3.0">@b@ <display-name>HelloWorldSpring</display-name>@b@@b@ <servlet>@b@ <servlet-name>spring-mvc</servlet-name>@b@ <servlet-class>@b@ org.springframework.web.servlet.DispatcherServlet@b@ </servlet-class>@b@ <load-on-startup>1</load-on-startup>@b@ </servlet>@b@@b@ <servlet-mapping>@b@ <servlet-name>spring-mvc</servlet-name>@b@ <url-pattern>/</url-pattern>@b@ </servlet-mapping>@b@ @b@ <context-param>@b@ <param-name>contextConfigLocation</param-name>@b@ <param-value>/WEB-INF/root-context.xml</param-value>@b@ </context-param>@b@ @b@ <listener>@b@ <listener-class>org.springframework.web.context.ContextLoaderListener@b@ </listener-class>@b@ </listener>@b@</web-app>
tomcat启动日志
[INFO] Scanning for projects...@b@[INFO] @b@[INFO] ------------------------------------------------------------------------@b@[INFO] Building HelloSpringMVC Maven Webapp 0.0.1-SNAPSHOT@b@[INFO] ------------------------------------------------------------------------@b@[INFO] @b@[INFO] --- maven-resources-plugin:2.4.1:resources (default-resources) @ HelloSpringMVC ---@b@[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!@b@[INFO] Copying 1 resource@b@[INFO] @b@[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ HelloSpringMVC ---@b@[INFO] Nothing to compile - all classes are up to date@b@[INFO] @b@[INFO] --- tomcat-maven-plugin:1.1:run (default-cli) @ HelloSpringMVC ---@b@@b@[INFO] --- tomcat-maven-plugin:1.1:run (default-cli) @ HelloSpringMVC ---@b@@b@[INFO] Running war on http://localhost:9999/HelloSpringMVC@b@@b@[INFO] Using existing Tomcat server configuration at C:\xwood_net @b@@b@[INFO] Using existing Tomcat server configuration at ...@b@.....@b@信息: Mapped URL path [/remoteservice] onto handler '/remoteservice'@b@十二月 18, 2018 11:34:29 下午 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler@b@....@b@@b@信息: FrameworkServlet 'spring-mvc': initialization completed in 3629 ms@b@十二月 18, 2018 11:34:29 下午 org.apache.coyote.http11.Http11Protocol init@b@信息: Initializing Coyote HTTP/1.1 on http-9999@b@十二月 18, 2018 11:34:29 下午 org.apache.coyote.http11.Http11Protocol start@b@信息: Starting Coyote HTTP/1.1 on http-9999@b@remote service is invoked!!!
三、客户端代码示例
1. RemoteService接口(同上二中1部分)
2. spring的xml配置(serviceUrl地址 - 根据服务端ip地址端口结合发布服务,如上面二中tomcat启动日志中的"http://localhost:9999/HelloSpringMVC")
<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"@b@ xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"@b@ xsi:schemaLocation="http://www.springframework.org/schema/beans@b@ http://www.springframework.org/schema/beans/spring-beans-4.1.xsd@b@ http://www.springframework.org/schema/context@b@ http://www.springframework.org/schema/context/spring-context-4.1.xsd">@b@@b@ <context:annotation-config />@b@ <context:component-scan base-package="com"></context:component-scan>@b@@b@ <bean id="iRemoteTest"@b@ class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">@b@ <property name="serviceUrl"@b@ value="http://localhost:9999/HelloSpringMVC/remoteservice" />@b@ <property name="serviceInterface"@b@ value="com.xwood.springmvc.test.remote.RemoteService" />@b@ </bean>@b@@b@</beans>
3. 测试RemoteServiceClientTest类
package com.xwood.springmvc.test;@b@@b@import org.junit.Test;@b@import org.springframework.test.context.ContextConfiguration;@b@import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;@b@import com.xwood.springmvc.test.remote.RemoteService;@b@@b@@ContextConfiguration(locations = { "classpath*:junitSpringContext.xml" })@b@public class RemoteServiceClientTest extends AbstractJUnit4SpringContextTests {@b@@b@ @Test@b@ public void testHttpClient() throws Exception { @b@ RemoteService remoteService= (RemoteService)this.applicationContext.getBean("iRemoteTest");@b@ remoteService.invoke();@b@ }@b@ @b@}
运行服务端控制台打印结果
....@b@信息: Initializing Coyote HTTP/1.1 on http-9999@b@十二月 18, 2018 11:34:29 下午 org.apache.coyote.http11.Http11Protocol start@b@信息: Starting Coyote HTTP/1.1 on http-9999@b@@b@@b@remote service is invoked!!!