Skip to content

Latest commit

 

History

History
122 lines (96 loc) · 3.47 KB

README.md

File metadata and controls

122 lines (96 loc) · 3.47 KB

jistol-spring-remote

Jistol-spring-remote v2.0 allows you to create Remote Server / Client with simple configuration. It runs on JDK 1.8 or later, uses Spring 4.3.7.RELEASE version.

Maven Installation

<dependencies>
    <dependency>
        <groupId>io.github.jistol</groupId>
        <artifactId>jistol-spring-remote</artifactId>
        <version>2.0.0</version>
    </dependency>
</dependencies>

Remote Server

You can set up Spring Remote Server(RMI,HttpInvoker) in only two steps.

Add @EnableRemoteServer on configuration file.

@SpringBootApplication
@EnableRemoteServer
public class TestApplication {
  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

Add @RmiServer / @HttpInvokerServer on your service

@Service("rmiService")
@RmiServer(serviceInterface = RmiService.class, port = "${test.rmi.port}")
public class RmiServiceImpl implements RmiService {
    @Value("${test.rmi.port}") private String rmiPort;

    @Override
    public String say() {
        Class clazz = this.getClass();
        return clazz.getName() +  "." + new Object(){}.getClass().getEnclosingMethod().getName() + ", rmiPort :" + rmiPort;
    }
}

@Service("/httpInvokerService")
@HttpInvokerServer(serviceInterface = HttpInvokerService.class)
public class HttpInvokerServiceImpl implements HttpInvokerService {
    @Value("${server.port}") private String testHttpPort;

    @Override public String say() {
        Class clazz = this.getClass();
        return clazz.getName() +  "." + new Object(){}.getClass().getEnclosingMethod().getName() + ", httpPort :" + testHttpPort;
    }
}

Remote Client

The Remote Client can be configured in two ways.

1. Annotation Base (@RmiClient / @HttpInvokerClient)

Add @EnableRemoteClient on configuration file with basePackage.

@SpringBootApplication
@EnableRemoteClient(basePackage = "io.github.jistol.remote.test")
@RestController
public class TestApplication {
  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

Make only Client Interface.

@RmiClient(ip = "${test.rmi.server.ip}", port = "${test.rmi.server.port}")
public interface RmiServiceClient {
    RmiService rmiService();
}

@HttpInvokerClient(ip = "${test.http.server.ip}", port = "${test.http.server.port}")
public interface HttpServiceClient {
    @RemoteContext(context = "httpInvokerService")
    HttpInvokerService httpInvokerService();
}

if you not use @RemoteContext, Automatically make remote context by method name. If you use Annotation, it will be generated by Dynamic Proxy, which may cause some performance degradation. If you are sensitive to performance, please use as follows.

2. Use FactoryBeanUtil

@RequestMapping({"/rmi"})
public String sayProtocol()
{
    Class<DirectRmiService> type = DirectRmiService.class;
    RmiProxyFactoryBean rmiProxyFactoryBean = FactoryBeanUtil.getRmiProxyFactoryBean("rmi://" + rmiIp + ":" + rmiPort + "/directRmiService", type);
    return type.cast(rmiProxyFactoryBean.getObject()).say();
}

@RequestMapping({"/http"})
public String httpSayProtocol()
{
    Class<DirectHttpInvokerService> type = DirectHttpInvokerService.class;
    HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = FactoryBeanUtil.getHttpInvokerProxyFactoryBean("http://" + httpIp + ":" + httpPort + "/directHttpInvokerService", type);
    return type.cast(httpInvokerProxyFactoryBean.getObject()).say();
}