远程/在线服务

时间:2015-08-17 13:04:05

标签: spring spring-boot spring-integration rmi spring-cloud

我想知道Spring是否有任何解决方案来支持使用进程内服务或远程服务配置的过程调用。

更新1

一个例子,假设我们有以下内容:

共同项目:

public interface ServiceBInterface {
     boolean doSomething();
}

项目A(取决于共同项目):

@Service
public class ServiceA {

     @Autowired
     private ServiceBInterface serviceB;

     public void flowA() {
            // run flow A code
            boolean result = serviceB.doSomething();
           // continue run flow A code with result of service B 
     }
}

项目B(取决于共同项目):

@Service
public class ServiceB implements ServiceBInterface {
    public boolean doSomething() {
        boolean result = false;
        // execute some code
        return result;
    }
}

我希望能够在以下选项上配置ServiceBInterface bean:

  1. ServiceB的一个实例
  2. 某种对象的实例,它将使ServiceB的RPC成为独立于ServiceA的不同进程运行的。
  3. 答案 项目A(取决于共同项目):

        @Service
        public class ServiceA {
    
             @Autowired
             private ServiceBInterface serviceB;
    
    @PostConstruct
        public void init() {
            if (Boolean.getBoolean("remote")) {
                RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
                rmiProxyFactoryBean.setServiceUrl("rmi://localhost:1099/ServiceB");
                rmiProxyFactoryBean.setServiceInterface(ServiceBInterface.class);
                rmiProxyFactoryBean.setRefreshStubOnConnectFailure(true);
                rmiProxyFactoryBean.setLookupStubOnStartup(false);
    
                rmiProxyFactoryBean.afterPropertiesSet();
                serviceB = (ServiceBInterface) rmiProxyFactoryBean.getObject();
            }
        }
             public void flowA() {
                    // run flow A code
                    boolean result = serviceB.doSomething();
                   // continue run flow A code with result of service B 
             }
        }
    

    项目B(取决于共同项目):

       @Service
        public class ServiceB implements ServiceBInterface {
    
          RmiServiceExporter rmiServiceExporte;
    @PostConstruct
    public void init() throws RemoteException {
            if (Boolean.getBoolean("remoteB")) {
                rmiServiceExporter = new RmiServiceExporter();
                rmiServiceExporter.setServiceName("ServiceB");
                rmiServiceExporter.setService(serviceB());
                rmiServiceExporter.setServiceInterface(ServiceBInterface.class);
                rmiServiceExporter.setServicePort(9999);
                rmiServiceExporter.afterPropertiesSet();
            }
        }
            public boolean doSomething() {
                boolean result = false;
                // execute some code
                return result;
            }
        }
    

1 个答案:

答案 0 :(得分:1)

对不起你的问题我不清楚,但我想知道你是否能够为自己阅读Spring Integration Reference Manual找到一些帮助。例如,有一个RMI支持,它表示为一对入站/出站网关,用于与远程过程服务进行通信。

否则,请更具体,特别是您希望获得的解决方案代码示例。