quick_start - dinstone/focus GitHub Wiki

快速开始

快速开始给出一个基础的例子,大家感受一下,同一台机器上的客户端和服务端的RPC编程是多么的简单。更多的例子可以参考例子工程 focus-example

运行快速启动的最低要求是:

  • JDK 1.8 及以上
  • Maven 或 Gradle 工程管理工具

同步调用

1.创建maven工程 focus-quickstart 并添加依赖到pom文件。

<dependency>
  <groupId>com.dinstone.focus</groupId>
  <artifactId>focus-server</artifactId>
  <version>1.0.0</version>
</dependency>
<dependency>
  <groupId>com.dinstone.focus</groupId>
  <artifactId>focus-client</artifactId>
  <version>1.0.0</version>
</dependency>
<dependency>
  <groupId>com.dinstone.focus</groupId>
  <artifactId>focus-serialize-json</artifactId>
  <version>1.0.0</version>
</dependency>

2.创建 FooService 接口类。

package focus.quickstart;

public interface FooService {
    public String hello(String name);
}

3.创建 FooService 实现。

package focus.quickstart.server;

import focus.quickstart.FooService;

public class FooServiceImpl implements FooService {

    public String hello(String name) {
        return "hello " + name;
    }
}

4.创建 Focus server 并导出服务。

package focus.quickstart.server;

import java.io.IOException;

import com.dinstone.focus.server.FocusServer;
import com.dinstone.focus.server.ServerOptions;
import com.dinstone.loghub.Logger;
import com.dinstone.loghub.LoggerFactory;

import focus.quickstart.FooService;

public class FocusServerBootstrap {

    private static final Logger LOG = LoggerFactory.getLogger(FocusServerBootstrap.class);

    public static void main(String[] args) {
        ServerOptions serverOptions = new ServerOptions().listen("localhost", 3333)
                .setEndpoint("focus.quickstart.server");
        FocusServer server = new FocusServer(serverOptions);

        // exporting service
        server.exporting(FooService.class, new FooServiceImpl());

        // server.start();
        LOG.info("server start");
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        server.destroy();
        LOG.info("server stop");
    }
}

5.创建 Focus client 导入 FooService 并进行RPC调用。

package focus.quickstart.client;

import com.dinstone.focus.client.ClientOptions;
import com.dinstone.focus.client.FocusClient;

import focus.quickstart.FooService;

public class FocusClientBootstrap {

    public static void main(String[] args) {
        ClientOptions option = new ClientOptions().setEndpoint("focus.quickstart.client").connect("localhost", 3333);
        FocusClient client = new FocusClient(option);
        try {
            FooService fooService = client.importing(FooService.class);
            String reply = fooService.hello("dinstone");
            System.out.println(reply);
        } finally {
            client.destroy();
        }
    }
}

异步调用

1.创建异步调用服务接口 FooAsyncService。

package focus.quickstart.client;

import java.util.concurrent.CompletableFuture;

public interface FooAsyncService {
    public CompletableFuture<String> hello(String name);
}

2.创建 Focus client 导入 FooAsyncService 并进行异步RPC调用。

package focus.quickstart.client;

import java.util.concurrent.CompletableFuture;

import com.dinstone.focus.client.ClientOptions;
import com.dinstone.focus.client.FocusClient;
import com.dinstone.focus.client.ImportOptions;

public class FocusClientAsyncCallBootstrap {

    public static void main(String[] args) throws Exception {
        ClientOptions option = new ClientOptions().setEndpoint("focus.quickstart.client").connect("localhost", 3333);
        FocusClient client = new FocusClient(option);
        try {
            ImportOptions importOptions = new ImportOptions("focus.quickstart.FooService");
            FooAsyncService fooService = client.importing(FooAsyncService.class, importOptions);
            CompletableFuture<String> replyFuture = fooService.hello("dinstone");
            System.out.println(replyFuture.get());
        } finally {
            client.destroy();
        }
    }
}

泛化调用

1.泛化调用不需要创建客户端接口类。简单了不是吗?!

2.创建 Focus client 导入 GenericService 并进行 同步/异步 RPC调用。

package focus.quickstart.client;

import java.util.concurrent.CompletableFuture;

import com.dinstone.focus.client.ClientOptions;
import com.dinstone.focus.client.FocusClient;
import com.dinstone.focus.client.GenericService;

public class FocusClientGenericCallBootstrap {

    public static void main(String[] args) throws Exception {
        ClientOptions option = new ClientOptions().setEndpoint("focus.quickstart.client").connect("localhost", 3333);
        FocusClient client = new FocusClient(option);
        try {
            GenericService genericService = client.generic("focus.quickstart.FooService", null, 3000);

            String reply = genericService.sync(String.class, "hello", "dinstone");
            System.out.println("sync call reply : " + reply);

            CompletableFuture<String> replyFuture = genericService.async(String.class, "hello", "dinstone");
            System.out.println("async call reply : " + replyFuture.get());
        } finally {
            client.destroy();
        }
    }
}
⚠️ **GitHub.com Fallback** ⚠️