架构优化之SERVICE 层分离----服务端
来源:互联网 发布:mysql可视化工具使用 编辑:程序博客网 时间:2024/06/08 14:07
由于公司产品一开始开发任务紧张,一直在开足马力在开发,而架构这块儿因为人力不足,公司产品的几个应用有相似的业务,但是在service 层却是各写各的,一是对于代码没有很好的进行复用,而且对于后期的维护而留下了巨大隐患,现在人手空出来,所以就赶紧来做构架优化,也顺便做代码重构的工作了,一方面是为了减少后期业务更改所带来的高成本维护,另一方面也是为了后期产品架构扩展及调优
在网上找了很多资料,最终对比之下,决定用 java 原生的 RMI,将各个应用的 SERVICE 层 和 DAO 层独立出来一个 类似RPC的一个应用,独立 出来之后产品的**pc端**和**微信端**及**后台管理**三个应用的相似业务的SERVICE 层的代码就可以复用了 ,那为什么要采用 RMI方式来实现呢,lz 主要是觉得采用RMI 是面向对象的通信,既可以减少在架构重构的工作量,而且也不会减少代码的执行效率,最后lz 将 RMI 和spring 进行了整合,并且进行了简单的封装,以下为代码:(原先框架SSM)
1、 context.xml 为 RMI 的主要配置文件,和其他配置文件一样 也是 放在 src 目录中的
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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="com.vshop.interceptor.RmiServiceExporterNew"> <property name="serviceName" value="MessageService" /> <property name="service" ref="mobileAccountServiceImpl" /> <property name="serviceInterface" value="com.vshop.busi.rmi.interfaces.AccountService" /> </bean></beans>
2、将 context.xml文件在应用启动时候也要加载进来,所以要更改一下 web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>dps</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/applicationContext.xml,classpath*:cxf-server.xml,classpath*:context.xml </param-value> </context-param>
3、服务IP 及端口配置文件
其中registryHost 项为 本机的IP,registryPort 及 port 固定为3015就可以,不需要修改,
clientIp为限制客户端访问的白名单,用逗号分隔就可以
4、接下来就来说一下 context.xml 文件里面的依赖类了
本来在RMI 和 SPRING 整合的时候 context.xml 文件中的依赖类为 RmiServiceExporter,而且 registryHost 配置项 及 registryPort 、port 都是配置在 context.xml 文件中的,但是考虑到
怕那帮小弟们 在开发中 显得烦锁也避免出现不必要的麻烦,所以 lz 重写了RmiServiceExporter类为RmiServiceExporterNew ,RmiRegistryFactoryBean 重写为 RmiRegistryFactoryBeanNew在重写的类中 也修改了 读取以上三个配置项的文件为prop.properties
5、RmiServiceExporterNew 类的实现
package com.vshop.interceptor;import java.io.IOException;import java.rmi.AlreadyBoundException;import java.rmi.NoSuchObjectException;import java.rmi.NotBoundException;import java.rmi.Remote;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;import java.rmi.server.RMIClientSocketFactory;import java.rmi.server.RMIServerSocketFactory;import java.rmi.server.UnicastRemoteObject;import java.util.HashSet;import java.util.Properties;import java.util.Set;import javax.annotation.Resource;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.remoting.rmi.RmiBasedExporter;import org.springframework.remoting.rmi.RmiRegistryFactoryBean;import com.vshop.utils.PropUtils;public class RmiServiceExporterNew extends RmiBasedExporter implements InitializingBean, DisposableBean { private String serviceName; private int servicePort = 0; // anonymous port private RMIClientSocketFactory clientSocketFactory; private RMIServerSocketFactory serverSocketFactory; private Registry registry ; Properties props; { try { props = PropUtils.getProps(); } catch (IOException e) { e.printStackTrace(); } } private String registryHost=props.getProperty("rmi.registryHost"); private int registryPort =Integer.parseInt(props.getProperty("rmi.registryPort")); // Registry.REGISTRY_PORT; private RMIClientSocketFactory registryClientSocketFactory; private RMIServerSocketFactory registryServerSocketFactory; private boolean alwaysCreateRegistry = false; private boolean replaceExistingBinding = true; private Remote exportedObject; private boolean createdRegistry = false; /** * Set the name of the exported RMI service, * i.e. <code>rmi://host:port/NAME</code> */ public void setServiceName(String serviceName) { this.serviceName = serviceName; } /** * Set the port that the exported RMI service will use. * <p>Default is 0 (anonymous port). */ public void setServicePort(int servicePort) { this.servicePort = servicePort; } /** * Set a custom RMI client socket factory to use for exporting the service. * <p>If the given object also implements <code>java.rmi.server.RMIServerSocketFactory</code>, * it will automatically be registered as server socket factory too. * @see #setServerSocketFactory * @see java.rmi.server.RMIClientSocketFactory * @see java.rmi.server.RMIServerSocketFactory * @see UnicastRemoteObject#exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory) */ public void setClientSocketFactory(RMIClientSocketFactory clientSocketFactory) { this.clientSocketFactory = clientSocketFactory; } /** * Set a custom RMI server socket factory to use for exporting the service. * <p>Only needs to be specified when the client socket factory does not * implement <code>java.rmi.server.RMIServerSocketFactory</code> already. * @see #setClientSocketFactory * @see java.rmi.server.RMIClientSocketFactory * @see java.rmi.server.RMIServerSocketFactory * @see UnicastRemoteObject#exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory) */ public void setServerSocketFactory(RMIServerSocketFactory serverSocketFactory) { this.serverSocketFactory = serverSocketFactory; } /** * Specify the RMI registry to register the exported service with. * Typically used in combination with RmiRegistryFactoryBean. * <p>Alternatively, you can specify all registry properties locally. * This exporter will then try to locate the specified registry, * automatically creating a new local one if appropriate. * <p>Default is a local registry at the default port (1099), * created on the fly if necessary. * @see RmiRegistryFactoryBean * @see #setRegistryHost * @see #setRegistryPort * @see #setRegistryClientSocketFactory * @see #setRegistryServerSocketFactory */ public void setRegistry(Registry registry) { this.registry = registry; } /** * Set the host of the registry for the exported RMI service, * i.e. <code>rmi://HOST:port/name</code> * <p>Default is localhost. */ public void setRegistryHost(String registryHost) { this.registryHost = registryHost; } /** * Set the port of the registry for the exported RMI service, * i.e. <code>rmi://host:PORT/name</code> * <p>Default is <code>Registry.REGISTRY_PORT</code> (1099). * @see java.rmi.registry.Registry#REGISTRY_PORT */ public void setRegistryPort(int registryPort) { this.registryPort = registryPort; } /** * Set a custom RMI client socket factory to use for the RMI registry. * <p>If the given object also implements <code>java.rmi.server.RMIServerSocketFactory</code>, * it will automatically be registered as server socket factory too. * @see #setRegistryServerSocketFactory * @see java.rmi.server.RMIClientSocketFactory * @see java.rmi.server.RMIServerSocketFactory * @see LocateRegistry#getRegistry(String, int, RMIClientSocketFactory) */ public void setRegistryClientSocketFactory(RMIClientSocketFactory registryClientSocketFactory) { this.registryClientSocketFactory = registryClientSocketFactory; } /** * Set a custom RMI server socket factory to use for the RMI registry. * <p>Only needs to be specified when the client socket factory does not * implement <code>java.rmi.server.RMIServerSocketFactory</code> already. * @see #setRegistryClientSocketFactory * @see java.rmi.server.RMIClientSocketFactory * @see java.rmi.server.RMIServerSocketFactory * @see LocateRegistry#createRegistry(int, RMIClientSocketFactory, RMIServerSocketFactory) */ public void setRegistryServerSocketFactory(RMIServerSocketFactory registryServerSocketFactory) { this.registryServerSocketFactory = registryServerSocketFactory; } /** * Set whether to always create the registry in-process, * not attempting to locate an existing registry at the specified port. * <p>Default is "false". Switch this flag to "true" in order to avoid * the overhead of locating an existing registry when you always * intend to create a new registry in any case. */ public void setAlwaysCreateRegistry(boolean alwaysCreateRegistry) { this.alwaysCreateRegistry = alwaysCreateRegistry; } /** * Set whether to replace an existing binding in the RMI registry, * that is, whether to simply override an existing binding with the * specified service in case of a naming conflict in the registry. * <p>Default is "true", assuming that an existing binding for this * exporter's service name is an accidental leftover from a previous * execution. Switch this to "false" to make the exporter fail in such * a scenario, indicating that there was already an RMI object bound. */ public void setReplaceExistingBinding(boolean replaceExistingBinding) { this.replaceExistingBinding = replaceExistingBinding; } public void afterPropertiesSet() throws RemoteException { setInterceptorsByClientIp(); setRegistryByPort(); prepare(); } private void setRegistryByPort() { RmiRegistryFactoryBeanNew rmiRegistryFactoryBeanNew = new RmiRegistryFactoryBeanNew(); rmiRegistryFactoryBeanNew.setPort(Integer.parseInt(props.getProperty("rmi.port"))); Registry object = null; try { object = rmiRegistryFactoryBeanNew.getRegistry(registryPort); } catch (Exception e) { e.printStackTrace(); } this.registry=object; } private void setInterceptorsByClientIp() { Set set = new HashSet(); String clientIpStr = props.getProperty("rmi.clientIp"); String[] clientIp = clientIpStr.split(","); for(String ip:clientIp){ set.add(ip); } SecurityInterceptor securityInterceptor = new SecurityInterceptor(); securityInterceptor.setAllowed(set); SecurityInterceptor[] array = new SecurityInterceptor[]{securityInterceptor}; super.setInterceptors(array); } /** * Initialize this service exporter, registering the service as RMI object. * <p>Creates an RMI registry on the specified port if none exists. * @throws RemoteException if service registration failed */ public void prepare() throws RemoteException { checkService(); if (this.serviceName == null) { throw new IllegalArgumentException("Property 'serviceName' is required"); } // Check socket factories for exported object. if (this.clientSocketFactory instanceof RMIServerSocketFactory) { this.serverSocketFactory = (RMIServerSocketFactory) this.clientSocketFactory; } if ((this.clientSocketFactory != null && this.serverSocketFactory == null) || (this.clientSocketFactory == null && this.serverSocketFactory != null)) { throw new IllegalArgumentException( "Both RMIClientSocketFactory and RMIServerSocketFactory or none required"); } // Check socket factories for RMI registry. if (this.registryClientSocketFactory instanceof RMIServerSocketFactory) { this.registryServerSocketFactory = (RMIServerSocketFactory) this.registryClientSocketFactory; } if (this.registryClientSocketFactory == null && this.registryServerSocketFactory != null) { throw new IllegalArgumentException( "RMIServerSocketFactory without RMIClientSocketFactory for registry not supported"); } this.createdRegistry = false; // Determine RMI registry to use. if (this.registry == null) { this.registry = getRegistry(this.registryHost, this.registryPort, this.registryClientSocketFactory, this.registryServerSocketFactory); this.createdRegistry = true; } // Initialize and cache exported object. this.exportedObject = getObjectToExport(); if (logger.isInfoEnabled()) { logger.info("Binding service '" + this.serviceName + "' to RMI registry: " + this.registry); } // Export RMI object. if (this.clientSocketFactory != null) { UnicastRemoteObject.exportObject( this.exportedObject, this.servicePort, this.clientSocketFactory, this.serverSocketFactory); } else { UnicastRemoteObject.exportObject(this.exportedObject, this.servicePort); } // Bind RMI object to registry. try { if (this.replaceExistingBinding) { this.registry.rebind(this.serviceName, this.exportedObject); } else { this.registry.bind(this.serviceName, this.exportedObject); } } catch (AlreadyBoundException ex) { // Already an RMI object bound for the specified service name... unexportObjectSilently(); throw new IllegalStateException( "Already an RMI object bound for name '" + this.serviceName + "': " + ex.toString()); } catch (RemoteException ex) { // Registry binding failed: let's unexport the RMI object as well. unexportObjectSilently(); throw ex; } } /** * Locate or create the RMI registry for this exporter. * @param registryHost the registry host to use (if this is specified, * no implicit creation of a RMI registry will happen) * @param registryPort the registry port to use * @param clientSocketFactory the RMI client socket factory for the registry (if any) * @param serverSocketFactory the RMI server socket factory for the registry (if any) * @return the RMI registry * @throws RemoteException if the registry couldn't be located or created */ protected Registry getRegistry(String registryHost, int registryPort, RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory) throws RemoteException { if (registryHost != null) { // Host explicitly specified: only lookup possible. if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "' of host [" + registryHost + "]"); } Registry reg = LocateRegistry.getRegistry(registryHost, registryPort, clientSocketFactory); testRegistry(reg); return reg; } else { return getRegistry(registryPort, clientSocketFactory, serverSocketFactory); } } /** * Locate or create the RMI registry for this exporter. * @param registryPort the registry port to use * @param clientSocketFactory the RMI client socket factory for the registry (if any) * @param serverSocketFactory the RMI server socket factory for the registry (if any) * @return the RMI registry * @throws RemoteException if the registry couldn't be located or created */ protected Registry getRegistry( int registryPort, RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory) throws RemoteException { if (clientSocketFactory != null) { if (this.alwaysCreateRegistry) { logger.info("Creating new RMI registry"); return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); } if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "', using custom socket factory"); } synchronized (LocateRegistry.class) { try { // Retrieve existing registry. Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory); testRegistry(reg); return reg; } catch (RemoteException ex) { logger.debug("RMI registry access threw exception", ex); logger.info("Could not detect RMI registry - creating new one"); // Assume no registry found -> create new one. return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); } } } else { return getRegistry(registryPort); } } /** * Locate or create the RMI registry for this exporter. * @param registryPort the registry port to use * @return the RMI registry * @throws RemoteException if the registry couldn't be located or created */ protected Registry getRegistry(int registryPort) throws RemoteException { if (this.alwaysCreateRegistry) { logger.info("Creating new RMI registry"); return LocateRegistry.createRegistry(registryPort); } if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "'"); } synchronized (LocateRegistry.class) { try { // Retrieve existing registry. Registry reg = LocateRegistry.getRegistry(registryPort); testRegistry(reg); return reg; } catch (RemoteException ex) { logger.debug("RMI registry access threw exception", ex); logger.info("Could not detect RMI registry - creating new one"); // Assume no registry found -> create new one. return LocateRegistry.createRegistry(registryPort); } } } /** * Test the given RMI registry, calling some operation on it to * check whether it is still active. * <p>Default implementation calls <code>Registry.list()</code>. * @param registry the RMI registry to test * @throws RemoteException if thrown by registry methods * @see java.rmi.registry.Registry#list() */ protected void testRegistry(Registry registry) throws RemoteException { registry.list(); } /** * Unbind the RMI service from the registry on bean factory shutdown. */ public void destroy() throws RemoteException { if (logger.isInfoEnabled()) { logger.info("Unbinding RMI service '" + this.serviceName + "' from registry" + (this.createdRegistry ? (" at port '" + this.registryPort + "'") : "")); } try { this.registry.unbind(this.serviceName); } catch (NotBoundException ex) { if (logger.isWarnEnabled()) { logger.warn("RMI service '" + this.serviceName + "' is not bound to registry" + (this.createdRegistry ? (" at port '" + this.registryPort + "' anymore") : ""), ex); } } finally { unexportObjectSilently(); } } /** * Unexport the registered RMI object, logging any exception that arises. */ private void unexportObjectSilently() { try { UnicastRemoteObject.unexportObject(this.exportedObject, true); } catch (NoSuchObjectException ex) { if (logger.isWarnEnabled()) { logger.warn("RMI object for service '" + this.serviceName + "' isn't exported anymore", ex); } } }}
6、RmiRegistryFactoryBeanNew的实现
package com.vshop.interceptor;import java.io.IOException;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;import java.rmi.server.RMIClientSocketFactory;import java.rmi.server.RMIServerSocketFactory;import java.rmi.server.UnicastRemoteObject;import java.util.Properties;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.FactoryBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.stereotype.Component;import com.vshop.utils.PropUtils;@Component(value="registry")public class RmiRegistryFactoryBeanNew implements FactoryBean<Registry>, InitializingBean, DisposableBean { protected final Log logger = LogFactory.getLog(getClass()); private String host; Properties props; { try { props = PropUtils.getProps(); } catch (IOException e) { e.printStackTrace(); } } private int port ;//Registry.REGISTRY_PORT; private RMIClientSocketFactory clientSocketFactory; private RMIServerSocketFactory serverSocketFactory; private Registry registry; private boolean alwaysCreate = false; private boolean created = false; /** * Set the host of the registry for the exported RMI service, * i.e. <code>rmi://HOST:port/name</code> * <p>Default is localhost. */ public void setHost(String host) { this.host = host; } /** * Return the host of the registry for the exported RMI service. */ public String getHost() { return this.host; } /** * Set the port of the registry for the exported RMI service, * i.e. <code>rmi://host:PORT/name</code> * <p>Default is <code>Registry.REGISTRY_PORT</code> (1099). */ public void setPort(int port) { this.port = port; } /** * Return the port of the registry for the exported RMI service. */ public int getPort() { return this.port; } /** * Set a custom RMI client socket factory to use for the RMI registry. * <p>If the given object also implements <code>java.rmi.server.RMIServerSocketFactory</code>, * it will automatically be registered as server socket factory too. * @see #setServerSocketFactory * @see java.rmi.server.RMIClientSocketFactory * @see java.rmi.server.RMIServerSocketFactory * @see java.rmi.registry.LocateRegistry#getRegistry(String, int, java.rmi.server.RMIClientSocketFactory) */ public void setClientSocketFactory(RMIClientSocketFactory clientSocketFactory) { this.clientSocketFactory = clientSocketFactory; } /** * Set a custom RMI server socket factory to use for the RMI registry. * <p>Only needs to be specified when the client socket factory does not * implement <code>java.rmi.server.RMIServerSocketFactory</code> already. * @see #setClientSocketFactory * @see java.rmi.server.RMIClientSocketFactory * @see java.rmi.server.RMIServerSocketFactory * @see java.rmi.registry.LocateRegistry#createRegistry(int, RMIClientSocketFactory, java.rmi.server.RMIServerSocketFactory) */ public void setServerSocketFactory(RMIServerSocketFactory serverSocketFactory) { this.serverSocketFactory = serverSocketFactory; } /** * Set whether to always create the registry in-process, * not attempting to locate an existing registry at the specified port. * <p>Default is "false". Switch this flag to "true" in order to avoid * the overhead of locating an existing registry when you always * intend to create a new registry in any case. */ public void setAlwaysCreate(boolean alwaysCreate) { this.alwaysCreate = alwaysCreate; } public void afterPropertiesSet() throws Exception { // Check socket factories for registry. if (this.clientSocketFactory instanceof RMIServerSocketFactory) { this.serverSocketFactory = (RMIServerSocketFactory) this.clientSocketFactory; } if ((this.clientSocketFactory != null && this.serverSocketFactory == null) || (this.clientSocketFactory == null && this.serverSocketFactory != null)) { throw new IllegalArgumentException( "Both RMIClientSocketFactory and RMIServerSocketFactory or none required"); } // Fetch RMI registry to expose. this.registry = getRegistry(this.host, this.port, this.clientSocketFactory, this.serverSocketFactory); } /** * Locate or create the RMI registry. * @param registryHost the registry host to use (if this is specified, * no implicit creation of a RMI registry will happen) * @param registryPort the registry port to use * @param clientSocketFactory the RMI client socket factory for the registry (if any) * @param serverSocketFactory the RMI server socket factory for the registry (if any) * @return the RMI registry * @throws java.rmi.RemoteException if the registry couldn't be located or created */ protected Registry getRegistry(String registryHost, int registryPort, RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory) throws RemoteException { if (registryHost != null) { // Host explictly specified: only lookup possible. if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "' of host [" + registryHost + "]"); } Registry reg = LocateRegistry.getRegistry(registryHost, registryPort, clientSocketFactory); testRegistry(reg); return reg; } else { return getRegistry(registryPort, clientSocketFactory, serverSocketFactory); } } /** * Locate or create the RMI registry. * @param registryPort the registry port to use * @param clientSocketFactory the RMI client socket factory for the registry (if any) * @param serverSocketFactory the RMI server socket factory for the registry (if any) * @return the RMI registry * @throws RemoteException if the registry couldn't be located or created */ protected Registry getRegistry( int registryPort, RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory) throws RemoteException { if (clientSocketFactory != null) { if (this.alwaysCreate) { logger.info("Creating new RMI registry"); this.created = true; return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); } if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "', using custom socket factory"); } synchronized (LocateRegistry.class) { try { // Retrieve existing registry. Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory); testRegistry(reg); return reg; } catch (RemoteException ex) { logger.debug("RMI registry access threw exception", ex); logger.info("Could not detect RMI registry - creating new one"); // Assume no registry found -> create new one. this.created = true; return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); } } } else { return getRegistry(registryPort); } } /** * Locate or create the RMI registry. * @param registryPort the registry port to use * @return the RMI registry * @throws RemoteException if the registry couldn't be located or created */ public Registry getRegistry(int registryPort) throws RemoteException { if (this.alwaysCreate) { logger.info("Creating new RMI registry"); this.created = true; return LocateRegistry.createRegistry(registryPort); } if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "'"); } synchronized (LocateRegistry.class) { try { // Retrieve existing registry. Registry reg = LocateRegistry.getRegistry(registryPort); testRegistry(reg); return reg; } catch (RemoteException ex) { logger.debug("RMI registry access threw exception", ex); logger.info("Could not detect RMI registry - creating new one"); // Assume no registry found -> create new one. this.created = true; return LocateRegistry.createRegistry(registryPort); } } } /** * Test the given RMI registry, calling some operation on it to * check whether it is still active. * <p>Default implementation calls <code>Registry.list()</code>. * @param registry the RMI registry to test * @throws RemoteException if thrown by registry methods * @see java.rmi.registry.Registry#list() */ protected void testRegistry(Registry registry) throws RemoteException { registry.list(); } public Registry getObject() throws Exception { return this.registry; } public Class<? extends Registry> getObjectType() { return (this.registry != null ? this.registry.getClass() : Registry.class); } public boolean isSingleton() { return true; } /** * Unexport the RMI registry on bean factory shutdown, * provided that this bean actually created a registry. */ public void destroy() throws RemoteException { if (this.created) { logger.info("Unexporting RMI registry"); UnicastRemoteObject.unexportObject(this.registry, true); } }}
7、然后就是一个白名单的一个简单的 权限控制了
其主要思想是 实现了MethodInterceptor 接口
这个权限也是在 RmiServiceExporterNew 类中调用的
package com.vshop.interceptor;import java.rmi.server.RemoteServer;import java.util.Iterator;import java.util.Set;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.apache.catalina.valves.RemoteAddrValve;public class SecurityInterceptor implements MethodInterceptor { private Set allowed; public Object invoke(MethodInvocation methodInvocation) throws Throwable { String clientHost = RemoteServer.getClientHost();// RemoteServer.toStub(); methodInvocation.getArguments(); if (allowed != null && allowed.contains(clientHost)) { return methodInvocation.proceed(); } else { throw new SecurityException("非法访问。"); } } public void setAllowed(Set allowed) { this.allowed = allowed; }}
8、最后就是最重要的,在context.xml 文件中提到的 mobileAccountServiceImpl 实现类及AccountService接口了
package com.vshop.busi.rmi.interfaces;public interface AccountService { public int queryBalance(String mobileNo); public String shoopingPayment(String mobileNo, byte protocol); }
package com.vshop.busi.rmi.interfaces.impl;import java.io.Serializable;import org.apache.log4j.Logger;import org.springframework.stereotype.Component;import com.vshop.busi.rmi.interfaces.AccountService;@Component(value="mobileAccountServiceImpl")public class MobileAccountServiceImpl implements AccountService, Serializable { private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class); public int queryBalance(String mobileNo) { if (mobileNo != null) return 100; return 0; } public String shoopingPayment(String mobileNo, byte protocol) { StringBuffer sb = new StringBuffer().append("Your mobile number is \"").append( mobileNo).append("\", protocol type is \"").append(protocol) .append("\"."); // LOG.info("Message is: " + sb.toString()); return sb.toString(); } }
最后还有一点特别重要的,就是 在 各个端 和 这个RPC 传输对象的时候,对象所属的类及依赖类都必须要 实现 序列化接口,类似以下
- 架构优化之SERVICE 层分离----服务端
- 架构优化之SERVICE 层分离----客户端
- 三层架构之UI层完全分离
- 传智播客--进销存之项目架构,DAO层和Service层的泛型抽取和实现
- Service层的性能优化
- 数据库优化之读写分离
- iOS4层结构之Core Service层
- .net分层架构思想(彻底分离每个层)
- 【架构】为什么在服务层设计读写分离
- TYPESDK手游聚合SDK服务端设计思路与架构之三:流程优化之订单保存与通知
- TYPESDK手游聚合SDK服务端设计思路与架构之四:流程优化之信息安全与订单校验
- TYPESDK手游聚合SDK服务端设计思路与架构之五:流程优化之特殊流程处理
- 数据库(Mysql)读写分离之服务器配置和服务端实现
- 前后端分离之图片上传服务端处理方法
- 前端架构之表现与数据分离
- MySQL 主从架构之读写分离
- JAVA SSH开发架构中Action层,Service层,modle层,Dao层的功能区分
- 三层的优化(七层架构)
- smack4.2 用户登录代码
- Photos 框架实践以及坑
- Ubuntu WindowsXP 双系统,开机进不了系统,提示找不到system32\hal.dll
- Android系统镜像文件的打包过程
- 承载IT运维的成功之梦:精准运维
- 架构优化之SERVICE 层分离----服务端
- 倒计时、计算时间
- ganglia学习2之Spark编译带有ganglia的版本
- bzoj1016: [JSOI2008]最小生成树计数
- C语言再学习 -- EOF、feof函数、ferror函数
- can’t convert between vector values of different size return (__m64) __builtin_ia32_vec_init_v2si
- Elasticsearch、Fluentd与Kibana:开源的日志搜索与可视化方案
- AC3/EAC3格式总结
- Linux 守护进程的启动方法