服务端配置
maven依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-remoting</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.60</version>
</dependency>
</dependencies>
服务接口:
package com.llg.service;
public interface HelloService {
public String hello(String name);
}
接口实现类:
package com.llg.service.impl;
import com.llg.service.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return "hello "+name;
}
}
web.xml:
在spring中实现Burlap需要借助springMVC所有须创建web项目
<!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>
<servlet>
<servlet-name>Burlap</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Burlap</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.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 name="helloService" class="com.llg.service.impl.HelloServiceImpl"/>
<bean name="/HelloService" class="org.springframework.remoting.caucho.BurlapServiceExporter">
<property name="service" ref="helloService"/>
<property name="serviceInterface" value="com.llg.service.HelloService"/>
</bean>
</beans>
在这里首先将需要发布为远程服务的bean注入到spring容器中,在这里是com.llg.service.impl.HelloServiceImpl取名为helloService,然后使用org.springframework.remoting.caucho.BurlapServiceExporter将helloService发布为远程服务,service属性指明要发布为远程服务的bean,serviceInterface指明这个bean所实现的接口。name为“/HelloService”指明这个url的请求交给交给远程服务处理而不是DispatcherServlet,由于我的项目名为BurlapServer,所以url为http://ip:port/BurlapServer/HelloService的请求将会交给远程服务处理。
客户端
pom.xml:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.60</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-remoting</artifactId>
<version>2.0.8</version>
</dependency>
</dependencies>
与服务端相同的接口:
package com.llg.service;
public interface HelloService {
public String hello(String name);
}
applicationContext.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 class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/BurlpServer/HelloService"/>
<property name="serviceInterface" value="com.llg.service.HelloService"/>
</bean>
</beans>
在这里使用org.springframework.remoting.caucho.BurlapProxyFactoryBean将远程服务的bean注入到本地Spring容器中,
serviceUrl属性指明远程服务url,也就是刚才说的http://ip:port/BurlapServer/HelloService
serviceInterface属性指明服务实现的接口。
测试
服务端由于是web项目,直接启动服务器即可
客户端测试代码:
package com.llg;
import com.llg.service.HelloService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestBurlapClient {
@Autowired
private HelloService helloService;
@Test
public void test(){
System.out.println(helloService.hello("李四"));
}
}
在这里直接取出注入到本地Spring容器中的HelloServicve服务,并调用hello()方法,根据服务端定义的实现类可推断出应该输出:“hello 李四”。
运行截图: