class MyService { String hello(String name) { return "Hello, " + name + "!"; } } public class Server { public static void main(String[] args) { MyService myService = new MyService(); Service<HttpRequest, HttpResponse> service = new Service<HttpRequest, HttpResponse>() { @Override public Future<HttpResponse> apply(HttpRequest request) { String name = request.getParam("name"); String response = myService.hello(name); return Future.value(new HttpResponse(response)); } }; ServerBuilder.safeBuild(service, ServerBuilder.get().name("MyServer").bindTo(new InetSocketAddress(8080))); } } public class Client { public static void main(String[] args) { Service<HttpRequest, HttpResponse> client = ClientBuilder.safeBuild(ClientBuilder.get().codec(Http.get()) .hosts("localhost:8080").hostConnectionLimit(1).name("MyClient")); HttpRequest request = new HttpRequest("/hello?name=John"); Future<HttpResponse> responseFuture = client.apply(request); responseFuture.onSuccess(response -> { System.out.println("Received response: " + response.getBody()); }); responseFuture.onFailure(throwable -> { System.out.println("Request failed: " + throwable.getMessage()); }); } }


上一篇:
下一篇:
切换中文