Spring 針對遠程訪問服務提供的一個remote包其的的是提供一套統一的遠程服務發布功能
先來看一下Spring支持那些遠程服務功能 RMI服務 Hessian或者Burlap通過HTTP遠程調用服務 HTTP調用器暴露服務
下面用一個例子來看一下Spring 是怎樣對這些服務進行統一的封裝和管理
先看一下服務器端的源代碼
public interface IBookService {
Book getById(String id);
}
public class Book {
public String name;
public String id;
public String author;
}
public class BookService implements IBookService {
public Book getById(String id) {
return BookStore
getById(id);
}
}
客戶端源代碼
public class BookQueryService {
private IBookService bookService;
public void setAccountService(IBookService bookService) {
this
bookService = bookService;
}
public Book getBookById(String id) {
return bookService
getById(id);
}
}
//客戶端調用示例
public static void main(String[] args) {
ClassPathXmlApplicationContext context;
context = new ClassPathXmlApplicationContext(
applicationContext
xml
);
BookQueryService bookQueryService = (BookQueryService) context
getBean(
bookQueryService
);
Book book = bookQueryService
getBookById(
);
}
使用Spring 發布 RMI服務示例
服務器端配置:
<bean id=
bookService
class=
com
xmatthew
spring
remote
BookService
>
</bean>
<bean class=
org
springframework
remoting
rmi
RmiServiceExporter
>
<!
does not necessarily have to be the same name as the bean to be exported
>
<property name=
serviceName
value=
bookService
/>
<property name=
service
ref=
bookService
/>
<property name=
serviceInterface
value=
com
xmatthew
spring
remote
IBookService
/>
<property name=
registryPort
value=
/>
</bean>
客戶端配置:
<bean class=
com
xmatthew
spring
remote
client
BookQueryService
>
<property name=
bookService
ref=
bookService
/>
</bean>
<bean id=
bookService
class=
org
springframework
remoting
rmi
RmiProxyFactoryBean
>
<property name=
serviceUrl
value=
rmi://localhost:
/bookService
/>
<property name=
serviceInterface
value=
com
xmatthew
spring
remote
IBookService
/>
</bean>
使用Spring 發布 基於Http的Hessian服務示例
注 Hessian提供一種基於HTTP的二進制遠程協議它是由Caucho創建的可以在 找到更多有關Hessian的信息
首為使用Hessian需要為其配置Spring 的 DispatcherServlet把下面的配置加入到webxml中
<servlet>
<servlet
name>remoting</servlet
name>
<servlet
class>org
springframework
web
servlet
DispatcherServlet</servlet
class>
<load
on
startup>
</load
on
startup>
</servlet>
<servlet
mapping>
<servlet
name>remoting</servlet
name>
<url
pattern>/remoting/*</url
pattern>
</servlet
mapping>
服務器端配置:
<bean id=
bookService
class=
com
xmatthew
spring
remote
BookService
>
</bean>
<bean name=
/bookService
class=
org
springframework
remoting
caucho
HessianServiceExporter
>
<property name=
service
ref=
bookService
/>
<property name=
serviceInterface
value=
com
xmatthew
spring
remote
IBookService
/>
</bean>
客戶端配置:
<bean class=
com
xmatthew
spring
remote
client
BookQueryService
>
<property name=
bookService
ref=
bookService
/>
</bean>
<bean id=
bookService
class=
org
springframework
remoting
caucho
HessianProxyFactoryBean
>
<property name=
serviceUrl
value=//localhost:
/bookService
/>
<property name=
serviceInterface
value=
com
xmatthew
spring
remote
IBookService
/>
</bean>
使用Spring 發布 基於Http的Burlap服務示例
Burlap它是一個基於XML的Hessian替代方案它的配置方法和上述Hessian的一樣只要把 Hessian 換成 Burlap 就行了
服務器端使用orgspringframeworkremotingcauchoBurlapServiceExporter 發布服務客戶端使用orgspringframeworkremotingcauchoBurlapProxyFactoryBean
使用Spring 發布 基於HTTP調用器暴露服務
和使用自身序列化機制的輕量級協議Burlap和Hessian相反Spring HTTP調用器使用標准Java序列化機制來通過HTTP暴露業務
但其配置與Burlap和Hessian很相近
服務器端配置:
<bean id=
bookService
class=
com
xmatthew
spring
remote
BookService
>
</bean>
<bean name=
/bookService
class=
org
springframework
remoting
>
<property name=
service
ref=
bookService
/>
<property name=
serviceInterface
value=
com
xmatthew
spring
remote
IBookService
/>
</bean>
客戶端配置:
<bean class=
com
xmatthew
spring
remote
client
BookQueryService
>
<property name=
bookService
ref=
bookService
/>
</bean>
<bean id=
bookService
class=
org
springframework
remoting
>
<property name=
serviceUrl
value=//localhost:
/bookService
/>
<property name=
serviceInterface
value=
com
xmatthew
spring
remote
IBookService
/>
</bean>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28488.html