/** * Starts Dapr's callback in a given port. * * @param port Port to listen to. */ publicstaticvoidstart(int port){ SpringApplication app = new SpringApplication(DaprApplication.class); app.run(String.format("--server.port=%d", port)); }
/** * @author Zhang_Xiang * @since 2020/11/7 17:30:26 */ publicclassClientA{ /** * Identifier in Dapr for the service this client will invoke. */ privatestaticfinal String SERVICE_APP_ID = "java-service-b";
/** * Format to output date and time. */ privatestaticfinal DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
/** * Starts the invoke client. * * @param args Messages to be sent as request for the invoke API. */ publicstaticvoidmain(String[] args)throws IOException { try (DaprClient client = (new DaprClientBuilder()).build()) { while (true) { Calendar utcNow = Calendar.getInstance(TimeZone.getTimeZone("GMT")); String utcNowAsString = DATE_FORMAT.format(utcNow.getTime()); String msg = String.format("%s:this this java client A", utcNowAsString); byte[] response = client.invokeService(SERVICE_APP_ID, "say", msg.getBytes(), HttpExtension.POST, null, byte[].class).block(); if (response != null) { String responseResultStr = new String(response); ResponseResult responseResult = JSON.parseObject(responseResultStr, ResponseResult.class); System.out.println(responseResult.getMessage()); } try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
== APP == This is java-service-b,receive the message:2020-11-0814:21:14.336:this this java client A,and request java-service-c get the response:{"message":"This is java-service-c,receive the message:\"2020-11-0814:21:14.336:this this java client A\""}
java-service-b 打印:
1
== APP == This is java-service-b,receive the message:2020-11-0814:21:44.454:this this java client A
java-service-c 打印:
1
== APP == This is java-service-c,receive the message:"2020-11-0814:22:19.571:this this java client A"