Skip to content

Spring Annotation For Easy Remote (RMI/HTTP INVOKER)

Notifications You must be signed in to change notification settings

jistol/jistol-spring-remote

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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();
}

About

Spring Annotation For Easy Remote (RMI/HTTP INVOKER)

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages