|
/**
* 远程服务缓冲池。
*
* <p>
* 对于一个既定的服务契约(既接口),可以有很多服务提供者( <b> ServiceProvider </b> )。该接口的提出,是为了解决服务访问者和服务提供者之间 “ 一对多 ” 的关系。
*
* @author Tony
*/
public interface RemoteServicePool {
/**
* 从缓冲池中获取一个 <b> Service </b> 。
*
* <p>
* 此处获得的是一个 <code> Object </code> ,需要调用者自己做类型转换。
*
* <p>
* 参数 <b> serviceId </b> 代表服务缓冲池中的一个实例名称。服务类型采用配置文件中默认的类型。
*
* @param serviceId
* 实例名称
*
* @return 服务对象
*/
Object getService(String serviceId);
}
|
|
<bean id="userServicePool" class="com. tonysoft .common.XFireRemoteServicePool">
<property name="serviceInterface">
<value>com. tonysoft .demo.service.UserService</value>
</property>
<property name="serviceUrls">
<map>
<entry key=" server 1 ">
<value>http://localhost:8080/server1/service/userService?WSDL</value>
</entry>
<entry key="server2">
<value>http://localhost:8080/server2/service/userService?WSDL</value>
</entry>
</map>
</property>
</bean>
|
|
/** 服务工厂 */
public RemoteServicePool userServicePool ;
/**
* 测试新增一个不存在的用户。
*/
public void testAddUser() {
UserService userService = null ;
try {
userService =(UserService) userServicePool .getService("server2");
} catch (Exception e){
throw new RuntimeException( " 获取服务失败,失败原因:" + e);
}
OperateResult result = userService .addUser( new User( "daodao" , " 技术部" ));
assertEquals(result.isSuccess(), true );
}
|
|
< bean id = "helloHttpInvokerService" parent = "abstractServiceProxyFactory" >
< property name = "serviceInterface" >
< value > com.tonysoft.common.service.repository.example.HelloHttpInvoker </ value >
</ property >
</ bean >
|
|
private HelloHttpInvoker helloHttpInvokerService ;
public void testHttpInvoker() {
assertNotNull( "helloHttpInvokerService can't be null !" , helloHttpInvokerService );
assertEquals ( "Hello , HttpInvoker !" , helloHttpInvokerService .sayHello());
}
/**
* @param helloHttpInvokerService
* the helloHttpInvokerService to set
*/
public void setHelloHttpInvokerService(HelloHttpInvoker helloHttpInvokerService) {
this . helloHttpInvokerService = helloHttpInvokerService;
}
|
|
/**
* 服务代理工厂。
*
* <p>
* 该工厂对程序员屏蔽了服务实现的技术细节,对于 XFire 、 Hessian 、 HttpInvoker 等常用远程服务形式进行封装。
*
* <p>
* 程序员只需要提供一个服务接口(契约),该工厂会从服务中心 <code> ServiceRepository </code> 中查找符合该接口的远程服务实例。
*
* <p>
* 查找的规则是由服务 ID 提供者所提供的服务 ID 和服务接口名字共同组成的服务关键字匹配。
*
* @author Tony
*/
public class ServiceProxyFactory implements FactoryBean {
/** 服务中心 */
private ServiceRepository serviceRepository ;
/** 服务 ID 提供者 */
private ServiceIdProvider serviceIdProvider ;
/** 服务接口 */
private Class serviceInterface ;
/*
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
public Object getObject() throws Exception {
return ProxyFactory.getProxy(getObjectType(), new ServiceProxyInterceptor());
// return serviceRepository.getService(serviceInterface, serviceIdProvider.getCurrentServiceId());
}
/*
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
public Class getObjectType() {
return serviceInterface ;
}
/*
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
*/
public boolean isSingleton() {
return true ;
}
/*
* 远程服务代理拦截器。
*/
private class ServiceProxyInterceptor implements MethodInterceptor {
/*
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
Object[] args = invocation.getArguments();
Object client = getClient();
return method.invoke(client, args);
}
private Object getClient() {
try {
return serviceRepository .getService( serviceInterface , serviceIdProvider .getCurrentServiceId());
} catch (ServiceException e) {
// TODO
e.printStackTrace();
return null ;
}
}
}
// ---- 容器自动注入 ----
······
|
|
return serviceRepository .getService( serviceInterface , serviceIdProvider .getCurrentServiceId());
|
|
/**
* 服务代理生成器。
*
* @author Tony
*/
public interface ServiceProxyGenerator {
/**
* 取得服务代理对象。
*
* @param serviceClass
* 服务接口
* @param serviceUrl
* 服务地址
* @param props
* 附加属性
* @return 代理对象
* @throws Exception
*/
Object getService(Class serviceClass, String serviceUrl, Properties props) throws Exception;
}
|
|
public void registService(ServiceModel serviceModel) throws ServiceException {
······
String key = serviceModel.getServiceId() + KEY_SPAR
+ serviceModel.getServiceClass().getName();
if ( serviceNames .contains(key)) {
throw new ServiceRegistException( "service is exist!" );
}
Object proxy = null ;
try {
ServiceProxyGenerator proxyGenerator = (ServiceProxyGenerator) beanFactory
.getBean(serviceModel.getServiceType() + PROXY_GENERATOR_END );
proxy = proxyGenerator.getService(serviceModel.getServiceClass(), serviceModel
.getServiceUrl(), serviceModel.getProps());
} catch (Exception e) {
throw new ServiceRegistException( "can't regist service !" , e);
}
if (proxy != null ) {
serviceNames .add(key);
serviceContainer .put(key, proxy);
} else {
throw new ServiceRegistException( "fail to regist service !" );
}
}
|
|
<!-- XFire 类型服务代理生成器 -->
< bean id = "xfire_generator" class = "com.tonysoft.common.service.repository.generator.XFireServiceProxyGenerator" lazy-init = "true" >
< property name = "serviceFactory" >
< ref bean = "xfire.serviceFactory" />
</ property >
</ bean >
<!-- Hessian 类型服务代理生成器 -->
< bean id = "hessian_generator" class = "com.tonysoft.common.service.repository.generator.HessianServiceProxyGenerator" lazy-init = "true" >
</ bean >
<!-- HttpInvoker 类型服务代理生成器 -->
< bean id = "httpinvoker_generator" class = "com.tonysoft.common.service.repository.generator.HttpInvokeServiceProxyGenerator" lazy-init = "true" >
</ bean >
<!-- 自定义 类型服务代理生成器 -->
< bean id = "custom_generator" class = "com.tonysoft.common.service.repository.generator.CustomServiceProxyGenerator" lazy-init = "true" >
</ bean >
<!-- 服务中心(资源库) -->
< bean id = "serviceRepository" class = "com.tonysoft.common.service.repository.DefaultServiceRepository" >
</ bean >
<!-- 服务 ID 提供者 -->
< bean id = "serviceIdProvider" class = "com.tonysoft.common.service.repository.provider.DefaultServiceIdProvider" >
</ bean >
<!-- 所有远程服务的基础类 -->
< bean id = "abstractServiceProxyFactory" class = "com.tonysoft.common.service.repository.ServiceProxyFactory" abstract = "true" >
</ bean >
|
|
public class HttpInvokeServiceProxyGenerator implements ServiceProxyGenerator {
/** HttpInvoker 服务代理工厂 */
private HttpInvokerProxyFactoryBean httpInvokerFactory = new HttpInvokerProxyFactoryBean();
/*
* @see com.alipay.xfiredemo.common.ServiceProxyGenerator#getService(java.lang.Class, java.lang.String,
* java.util.Properties)
*/
public Object getService(Class serviceClass, String serviceUrl, Properties props) {
// Todo initial httpInvokerFactory with props
httpInvokerFactory .setServiceInterface(serviceClass);
httpInvokerFactory .setServiceUrl(serviceUrl);
// must invoke this method
httpInvokerFactory .afterPropertiesSet();
return httpInvokerFactory .getObject();
}
}
|
|
<!-- 初始化时注册的静态服务 -->
< bean id = "bootupServiceRegister" class = "com.tonysoft.common.service.repository.register.BootupServiceRegister" lazy-init = "false" >
< property name = "services" >
< list >
<!--
<bean class="com.tonysoft.common.service.repository.ServiceModel">
<property name="serviceClass"><value>com.tonysoft.common.service.repository.example.HelloHessian</value></property>
<property name="serviceId"><value>default</value></property>
<property name="serviceType"><value>hessian</value></property>
<property name="serviceUrl"><value>http://localhost:8080/serviceRepositoryApplication/service/hessian/helloHessian.service</value></property>
<property name="props">
<props></props>
</property>
</bean>
-->
< bean class = "com.tonysoft.common.service.repository.ServiceModel" >
< property name = "serviceClass" >< value > com.tonysoft.common.service.repository.example.HelloHttpInvoker </ value ></ property >
< property name = "serviceId" >< value > default </ value ></ property >
< property name = "serviceType" >< value > httpinvoker </ value ></ property >
< property name = "serviceUrl" >< value > http://localhost:8080/serviceRepositoryApplication/service/httpInvoker/helloHttpInvoker.service </ value ></ property >
< property name = "props" >
< props ></ props >
</ property >
</ bean >
< bean class = "com.tonysoft.common.service.repository.ServiceModel" >
< property name = "serviceClass" >< value > com.tonysoft.common.service.repository.example.HelloXFire </ value ></ property >
< property name = "serviceId" >< value > default </ value ></ property >
< property name = "serviceType" >< value > xfire </ value ></ property >
< property name = "serviceUrl" >< value > http://localhost:8080/serviceRepositoryApplication/service/xfire/helloXFire.service?WSDL </ value ></ property >
< property name = "props" >
< props ></ props >
</ property >
</ bean >
</ list >
</ property >
</ bean >
|
1971



被折叠的 条评论
为什么被折叠?



