mirror of https://github.com/dapr/java-sdk.git
Review changes of 0.2.0 beta (#205)
* Enable Java highlighting for state sample * Fix Why Pub-Sub link * add http ingress sample debug instructions * add invoke grpc sample debug instructions * align pubsub sample readme w code, fix vscode java arg handling * add input binding sample debug instructions * add actors sample debug instructions * change -D exec to -Dexec * add sample debugging instructions for Unix * fix some typos/language
This commit is contained in:
parent
0ed7363a3f
commit
c2d6464ff5
|
@ -0,0 +1,96 @@
|
|||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch) - Current File",
|
||||
"request": "launch",
|
||||
"mainClass": "${file}"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-DemoActorClient<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.actors.http.DemoActorClient",
|
||||
"projectName": "dapr-sdk-examples"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-DemoActorService<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.actors.http.DemoActorService",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=3000"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-InputBindingExample<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.bindings.http.InputBindingExample",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=3000"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-OutputBindingExample<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.bindings.http.OutputBindingExample",
|
||||
"projectName": "dapr-sdk-examples"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-HelloWorldClient<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.invoke.grpc.HelloWorldClient",
|
||||
"projectName": "dapr-sdk-examples"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-HelloWorldService<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.invoke.grpc.HelloWorldService",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=5000"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-DemoService<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.invoke.http.DemoService",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=3000"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-InvokeClient<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.invoke.http.InvokeClient",
|
||||
"projectName": "dapr-sdk-examples"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-Publisher<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.pubsub.http.Publisher",
|
||||
"projectName": "dapr-sdk-examples"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-Subscriber<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.pubsub.http.Subscriber",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=3000"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-StateClient<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.state.StateClient",
|
||||
"projectName": "dapr-sdk-examples"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -55,7 +55,7 @@ public class DemoActorService {
|
|||
This application uses `ActorRuntime.getInstance().registerActor()` in order to register `DemoActorImpl` as an actor in the Dapr Actor runtime. Internally, it is using `DefaultObjectSerializer` for two properties: `objectSerializer` is for Dapr's sent and received objects, and `stateSerializer` is for objects to be persisted.
|
||||
|
||||
|
||||
`DaprApplication.start()` method will run the Spring Boot [DaprApplication](../../../springboot/DaprApplication.java), which registers the Dapr Spring Boot controller [DaprController](../../springboot/DaprController.java). This controller contains all Actor methods implemented as endpoints. The Dapr's sidecar will call into the controller.
|
||||
`DaprApplication.start()` method will run the Spring Boot [DaprApplication](../../../springboot/DaprApplication.java), which registers the Dapr Spring Boot controller [DaprController](../../../springboot/DaprController.java). This controller contains all Actor methods implemented as endpoints. The Dapr's sidecar will call into the controller.
|
||||
|
||||
See [DemoActorImpl](DemoActorImpl.java) for details on the implementation of an actor:
|
||||
```java
|
||||
|
@ -105,7 +105,37 @@ An actor inherits from `AbstractActor` and implements the constructor to pass th
|
|||
Now, execute the following script in order to run DemoActorService:
|
||||
```sh
|
||||
cd to [repo-root]
|
||||
dapr run --app-id demoactorservice --app-port 3000 --port 3005 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.actors.http.DemoActorService -D exec.args="-p 3000"
|
||||
dapr run --app-id demoactorservice --app-port 3000 --port 3005 -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.actors.http.DemoActorService -Dexec.args="-p 3000"
|
||||
```
|
||||
|
||||
### Debugging the Demo actor service
|
||||
|
||||
If you want to debug the actor service, you have to make sure to provide the port as an argument.
|
||||
|
||||
For VSCode you can find a sample launch.json which includes:
|
||||
```json
|
||||
...
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-DemoActorService<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.actors.http.DemoActorService",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=3000"
|
||||
},
|
||||
...
|
||||
```
|
||||
|
||||
Use the following commands to run the Dapr sidecar.
|
||||
|
||||
For Linux and MacOS:
|
||||
```sh
|
||||
dapr run --app-id demoactorservice --app-port 3000 --port 3005 --grpc-port 5001 -- cat
|
||||
```
|
||||
|
||||
For Windows:
|
||||
```sh
|
||||
dapr run --app-id demoactorservice --app-port 3000 --port 3005 --grpc-port 5001 -- waitfor FOREVER
|
||||
```
|
||||
|
||||
### Running the Actor client
|
||||
|
@ -164,7 +194,7 @@ Use the follow command to execute the DemoActorClient:
|
|||
|
||||
```sh
|
||||
cd to [repo-root]
|
||||
dapr run --app-id demoactorclient --port 3006 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.actors.http.DemoActorClient
|
||||
dapr run --app-id demoactorclient --port 3006 -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.actors.http.DemoActorClient
|
||||
```
|
||||
|
||||
Once running, the `DemoActorClient` logs will start displaying the different steps:
|
||||
|
|
|
@ -18,7 +18,7 @@ import org.apache.commons.cli.Options;
|
|||
* 2. cd to [repo-root]/examples
|
||||
* 3. Run :
|
||||
* dapr run --app-id inputbinding --app-port 3000 --port 3005 \
|
||||
* -- mvn exec:java -D exec.mainClass=io.dapr.examples.bindings.http.InputBindingExample -D exec.args="-p 3000"
|
||||
* -- mvn exec:java -Dexec.mainClass=io.dapr.examples.bindings.http.InputBindingExample -Dexec.args="-p 3000"
|
||||
*/
|
||||
public class InputBindingExample {
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ import io.dapr.client.DaprClientBuilder;
|
|||
* 2. cd to [repo-root]/examples
|
||||
* 3. Run the program:
|
||||
* dapr run --app-id outputbinding --port 3006 \
|
||||
* -- mvn exec:java -D exec.mainClass=io.dapr.examples.bindings.http.OutputBindingExample
|
||||
* -- mvn exec:java -Dexec.mainClass=io.dapr.examples.bindings.http.OutputBindingExample
|
||||
*/
|
||||
public class OutputBindingExample {
|
||||
|
||||
|
|
|
@ -89,7 +89,37 @@ public class InputBindingController {
|
|||
Execute the follow script in order to run the Input Binding example:
|
||||
```sh
|
||||
cd to [repo-root]/examples
|
||||
dapr run --app-id inputbinding --app-port 3000 --port 3005 -- mvn exec:java -D exec.mainClass=io.dapr.examples.bindings.http.InputBindingExample -D exec.args="-p 3000"
|
||||
dapr run --app-id inputbinding --app-port 3000 --port 3005 -- mvn exec:java -Dexec.mainClass=io.dapr.examples.bindings.http.InputBindingExample -Dexec.args="-p 3000"
|
||||
```
|
||||
|
||||
### Debugging the Input binding sample
|
||||
|
||||
If you want to debug the `InputBindingExample`, you have to make sure to provide the port as an argument.
|
||||
|
||||
For VSCode you can find a sample launch.json which includes:
|
||||
```json
|
||||
...
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-InputBindingExample<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.bindings.http.InputBindingExample",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=3000"
|
||||
},
|
||||
...
|
||||
```
|
||||
|
||||
Use the following commands to run the Dapr sidecar.
|
||||
|
||||
For Linux and MacOS:
|
||||
```sh
|
||||
dapr run --app-id inputbinding --app-port 3000 --port 3005 --grpc-port 5001 -- cat
|
||||
```
|
||||
|
||||
For Windows:
|
||||
```sh
|
||||
dapr run --app-id inputbinding --app-port 3000 --port 3005 --grpc-port 5001 -- waitfor FOREVER
|
||||
```
|
||||
|
||||
### Running the Output binding sample
|
||||
|
@ -123,7 +153,7 @@ Use the follow command to execute the Output Binding example:
|
|||
|
||||
```sh
|
||||
cd to [repo-root]/examples
|
||||
dapr run --app-id outputbinding --port 3006 -- mvn exec:java -D exec.mainClass=io.dapr.examples.bindings.http.OutputBindingExample
|
||||
dapr run --app-id outputbinding --port 3006 -- mvn exec:java -Dexec.mainClass=io.dapr.examples.bindings.http.OutputBindingExample
|
||||
```
|
||||
|
||||
Once running, the OutputBindingExample should print the output as follows:
|
||||
|
|
|
@ -13,7 +13,7 @@ import io.dapr.client.domain.Verb;
|
|||
* 1. Build and install jars:
|
||||
* mvn clean install
|
||||
* 2. Send messages to the server:
|
||||
* dapr run -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldClient
|
||||
* dapr run -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldClient
|
||||
*/
|
||||
public class HelloWorldClient {
|
||||
|
||||
|
|
|
@ -30,8 +30,8 @@ import static io.dapr.examples.DaprExamplesProtos.SayResponse;
|
|||
* mvn clean install
|
||||
* 2. Run in server mode:
|
||||
* dapr run --app-id hellogrpc --app-port 5000 --protocol grpc \
|
||||
* -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldService \
|
||||
* -D exec.args="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5009"
|
||||
* -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldService \
|
||||
* -Dexec.args="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5009"
|
||||
*/
|
||||
public class HelloWorldService {
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ mvn install
|
|||
|
||||
### Running the example's service
|
||||
|
||||
The first component is the service. It has a simple API with the `Say` method. This method will print out each message received from the client. The proto file below contains the description of the HelloWorld service found in the `./proto/examples/helloworld.proto` file:
|
||||
The first component is the service. It has a simple API with the `say` method. This method will print out each message received from the client. The proto file below contains the description of the HelloWorld service found in the `./examples/proto/helloworld.proto` file:
|
||||
|
||||
```text
|
||||
service HelloWorld {
|
||||
|
@ -48,19 +48,16 @@ private static class GrpcHelloWorldDaprService extends DaprClientGrpc.DaprClient
|
|||
///...
|
||||
@Override
|
||||
public void onInvoke(DaprClientProtos.InvokeEnvelope request, StreamObserver<Any> responseObserver) {
|
||||
try {
|
||||
if ("say".equals(request.getMethod())) {
|
||||
// IMPORTANT: do not use Any.unpack(), use Type.ParseFrom() instead.
|
||||
SayRequest sayRequest = SayRequest.parseFrom(request.getData().getValue());
|
||||
SayResponse sayResponse = this.say(sayRequest);
|
||||
responseObserver.onNext(Any.pack(sayResponse));
|
||||
}
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
e.printStackTrace();
|
||||
responseObserver.onError(e);
|
||||
} finally {
|
||||
responseObserver.onCompleted();
|
||||
try {
|
||||
if ("say".equals(request.getMethod())) {
|
||||
SayRequest sayRequest =
|
||||
SayRequest.newBuilder().setMessage(request.getData().getValue().toStringUtf8()).build();
|
||||
SayResponse sayResponse = this.say(sayRequest);
|
||||
responseObserver.onNext(Any.pack(sayResponse));
|
||||
}
|
||||
} finally {
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
}
|
||||
///...
|
||||
}
|
||||
|
@ -70,11 +67,41 @@ In the `GrpcHelloWorldDaprService` class, the `onInvoke` method is the most impo
|
|||
Now run the service code:
|
||||
|
||||
```sh
|
||||
dapr run --app-id hellogrpc --app-port 5000 --protocol grpc -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldService -D exec.args="-p 5000"
|
||||
dapr run --app-id hellogrpc --app-port 5000 --protocol grpc -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldService -Dexec.args="-p 5000"
|
||||
```
|
||||
|
||||
The `app-id` argument is used to identify this service in Dapr's runtime. The `app-port` determines which port Dapr's runtime should call into this service. The `protocol` argument informs Dapr which protocol it should use to invoke the application: `grpc` or `http`(default).
|
||||
|
||||
### Debugging the example's service
|
||||
|
||||
If you want to debug the `HelloWorldService`, you have to make sure to provide the port as an argument.
|
||||
|
||||
For VSCode you can find a sample launch.json which includes:
|
||||
```json
|
||||
...
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-HelloWorldService<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.invoke.grpc.HelloWorldService",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=5000"
|
||||
},
|
||||
...
|
||||
```
|
||||
|
||||
Use the following commands to run the Dapr sidecar.
|
||||
|
||||
For Linux and MacOS:
|
||||
```sh
|
||||
dapr run --app-id hellogrpc --app-port 5000 --protocol grpc --port 3005 --grpc-port 5001 -- cat
|
||||
```
|
||||
|
||||
For Windows:
|
||||
```sh
|
||||
dapr run --app-id hellogrpc --app-port 5000 --protocol grpc --port 3005 --grpc-port 5001 -- waitfor FOREVER
|
||||
```
|
||||
|
||||
### Running the example's client
|
||||
|
||||
The other component is the client. It will send one message per second to the service via Dapr's invoke API using Dapr's SDK. Open the `HelloWorldClient.java` file, it uses the Dapr's Java SDK to invoke the `say` method on the service above:
|
||||
|
@ -103,14 +130,14 @@ private static class HelloWorldClient {
|
|||
}
|
||||
```
|
||||
|
||||
First, it creates an instance of `DaprClient` via `DaprClientBuilder`. The protocol used by DaprClient is transparent to the application. The HTTP and GRPC ports used by Dapr's sidecar are automatically chosen and exported as environment variables: `DAPR_HTTP_PORT` and `DAPR_GRPC_PORT`. Dapr's Java SDK references these environment variables when communicating to Dapr's sidecar.
|
||||
First, it creates an instance of `DaprClient` via `DaprClientBuilder`. The protocol used by DaprClient can be set with the JVM's system property `dapr.grpc.enabled` or environment variable `DAPR_GRPC_ENABLED`. If both are not set, the default is to use HTTP. The HTTP and GRPC ports used by Dapr's sidecar are automatically chosen and exported as environment variables to the app: `DAPR_HTTP_PORT` and `DAPR_GRPC_PORT`. Dapr's Java SDK references these environment variables when communicating to Dapr's sidecar.
|
||||
|
||||
Finally, it will go through in an infinite loop and invoke the `say` method every second. Notice the use of `block()` on the return from `invokeService` - it is required to actually make the service invocation via a [Mono](https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html) object.
|
||||
|
||||
Finally, open a new command line terminal and run the client code to send some messages.
|
||||
|
||||
```sh
|
||||
dapr run -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldClient
|
||||
dapr run -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldClient
|
||||
```
|
||||
|
||||
Once the messages are sent, use `CTRL+C` to exit Dapr.
|
||||
|
|
|
@ -16,7 +16,7 @@ import org.apache.commons.cli.Options;
|
|||
* mvn clean install
|
||||
* 2. Run in server mode:
|
||||
* dapr run --app-id invokedemo --app-port 3000 --port 3005 \
|
||||
* -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.http.DemoService -D exec.args="-p 3000"
|
||||
* -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.invoke.http.DemoService -Dexec.args="-p 3000"
|
||||
*/
|
||||
public class DemoService {
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ mvn install
|
|||
|
||||
The Demo service application is meant to expose a method that can be remotely invoked. In this example, the service code has two parts:
|
||||
|
||||
In the `DemoService.java` file, you will find the `DemoService` class, containing the main method. The main method uses the Spring Boot´s DaprApplication class for initializing the `ExposerServiceController`. See the code snippet below:
|
||||
In the `DemoService.java` file, you will find the `DemoService` class, containing the main method. The main method uses the Spring Boot´s DaprApplication class for initializing the `DemoServiceController`. See the code snippet below:
|
||||
|
||||
```java
|
||||
public class DemoService {
|
||||
|
@ -89,11 +89,40 @@ public class DemoServiceController {
|
|||
Use the follow command to execute the demo service example:
|
||||
|
||||
```sh
|
||||
dapr run --app-id invokedemo --app-port 3000 --port 3005 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.http.DemoService -D exec.args="-p 3000"
|
||||
dapr run --app-id invokedemo --app-port 3000 --port 3005 -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.invoke.http.DemoService -Dexec.args="-p 3000"
|
||||
```
|
||||
|
||||
Once running, the ExposerService is now ready to be invoked by Dapr.
|
||||
Once running, the DemoService is now ready to be invoked by Dapr.
|
||||
|
||||
### Debugging the Demo service sample
|
||||
|
||||
If you want to debug the `DemoService`, you have to make sure to provide the port as an argument.
|
||||
|
||||
For VSCode you can find a sample launch.json which includes:
|
||||
```json
|
||||
...
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-DemoService<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.invoke.http.DemoService",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=3000"
|
||||
},
|
||||
...
|
||||
```
|
||||
|
||||
Use the following commands to run the Dapr sidecar.
|
||||
|
||||
For Linux and MacOS:
|
||||
```sh
|
||||
dapr run --app-id invokedemo --app-port 3000 --port 3005 --grpc-port 5001 -- cat
|
||||
```
|
||||
|
||||
For Windows:
|
||||
```sh
|
||||
dapr run --app-id invokedemo --app-port 3000 --port 3005 --grpc-port 5001 -- waitfor FOREVER
|
||||
```
|
||||
|
||||
### Running the InvokeClient sample
|
||||
|
||||
|
@ -118,7 +147,7 @@ The class knows the app id for the remote application. It uses the the static `D
|
|||
|
||||
Execute the follow script in order to run the InvokeClient example, passing two messages for the remote method:
|
||||
```sh
|
||||
dapr run --port 3006 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.http.InvokeClient -D exec.args="'message one' 'message two'"
|
||||
dapr run --port 3006 -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.invoke.http.InvokeClient -Dexec.args="'message one' 'message two'"
|
||||
```
|
||||
Once running, the output should display the messages sent from invoker in the demo service output as follows:
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ import java.util.Collections;
|
|||
* mvn clean install
|
||||
* 2. Run the program:
|
||||
* dapr run --app-id publisher --port 3006 -- \
|
||||
* mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.pubsub.http.Publisher
|
||||
* mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.pubsub.http.Publisher
|
||||
*/
|
||||
public class Publisher {
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# Dapr Pub-Sub Sample
|
||||
|
||||
In this sample, we'll create a publisher and a subscriber java applications using Dapr, based on the publish-subcribe pattern. The publisher will generate messages of a specific topic, while subscriber will listen for messages of specific topic. See [Why Pub-Sub](#why-pub-sub) to understand when this pattern might be a good choice for your software architecture.
|
||||
In this sample, we'll create a publisher and a subscriber java applications using Dapr, based on the publish-subcribe pattern. The publisher will generate messages of a specific topic, while subscriber will listen for messages of specific topic. See [Why Pub-Sub](https://github.com/dapr/samples/tree/master/4.pub-sub#why-pub-sub) to understand when this pattern might be a good choice for your software architecture.
|
||||
|
||||
Visit [this](https://github.com/dapr/docs/tree/master/concepts/publish-subscribe-messaging) link for more information about Dapr and Pub-Sub.
|
||||
|
||||
|
@ -52,16 +52,16 @@ public class SubscriberController {
|
|||
///...
|
||||
@GetMapping("/dapr/subscribe")
|
||||
public byte[] daprConfig() throws Exception {
|
||||
return SERIALIZER.serialize(new String[] { "message" });
|
||||
return SERIALIZER.serialize(new String[] { "testingtopic" });
|
||||
}
|
||||
|
||||
@PostMapping(path = "/message")
|
||||
@PostMapping(path = "/testingtopic")
|
||||
public Mono<Void> handleMessage(@RequestBody(required = false) byte[] body,
|
||||
@RequestHeader Map<String, String> headers) {
|
||||
return Mono.fromRunnable(() -> {
|
||||
try {
|
||||
// Dapr's event is compliant to CloudEvent.
|
||||
CloudEventEnvelope envelope = SERIALIZER.deserialize(body, CloudEventEnvelope.class);
|
||||
CloudEvent envelope = CloudEvent.deserialize(body);
|
||||
|
||||
String message = envelope.getData() == null ? "" : new String(envelope.getData());
|
||||
System.out.println("Subscriber got message: " + message);
|
||||
|
@ -74,7 +74,37 @@ public class SubscriberController {
|
|||
```
|
||||
Execute the follow script in order to run the Subscriber example:
|
||||
```sh
|
||||
dapr run --app-id subscriber --app-port 3000 --port 3005 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.pubsub.http.Subscriber -D exec.args="-p 3000"
|
||||
dapr run --app-id subscriber --app-port 3000 --port 3005 -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.pubsub.http.Subscriber -Dexec.args="-p 3000"
|
||||
```
|
||||
|
||||
### Debugging the subscriber
|
||||
|
||||
If you want to debug the `Subscriber`, you have to make sure to provide the port as an argument.
|
||||
|
||||
For VSCode you can find a sample launch.json which includes:
|
||||
```json
|
||||
...
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Debug (Launch)-Subscriber<dapr-sdk-examples>",
|
||||
"request": "launch",
|
||||
"mainClass": "io.dapr.examples.pubsub.http.Subscriber",
|
||||
"projectName": "dapr-sdk-examples",
|
||||
"args": "-port=3000"
|
||||
},
|
||||
...
|
||||
```
|
||||
|
||||
Use the following commands to run the Dapr sidecar.
|
||||
|
||||
For Linux and MacOS
|
||||
```sh
|
||||
dapr run --app-id subscriber --app-port 3000 --port 3005 --grpc-port 5001 -- cat
|
||||
```
|
||||
|
||||
For Windows:
|
||||
```sh
|
||||
dapr run --app-id subscriber --app-port 3000 --port 3005 --grpc-port 5001 -- waitfor FOREVER
|
||||
```
|
||||
|
||||
### Running the publisher
|
||||
|
@ -122,7 +152,7 @@ public class Publisher {
|
|||
Use the follow command to execute the Publisher example:
|
||||
|
||||
```sh
|
||||
dapr run --app-id publisher --port 3006 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.pubsub.http.Publisher
|
||||
dapr run --app-id publisher --port 3006 -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.pubsub.http.Publisher
|
||||
```
|
||||
|
||||
Once running, the Publisher should print the output as follows:
|
||||
|
@ -137,4 +167,4 @@ Once running, the Subscriber should print the output as follows:
|
|||
|
||||
Messages have been retrieved from the topic.
|
||||
|
||||
For more details on Dapr Spring Boot integration, please refer to [Dapr Spring Boot](../../../springboot/DaprApplication.java) Application implementation.
|
||||
For more details on Dapr Spring Boot integration, please refer to [Dapr Spring Boot](../../../springboot/DaprApplication.java) Application implementation.
|
||||
|
|
|
@ -27,7 +27,7 @@ mvn install
|
|||
### Running the StateClient
|
||||
This example uses the Java SDK Dapr client in order to save, retrieve and delete a state, in this case, an instance of a class. See the code snippet bellow:
|
||||
|
||||
```
|
||||
```java
|
||||
public class StateClient {
|
||||
///...
|
||||
private static final String KEY_NAME = "mykey";
|
||||
|
@ -60,7 +60,7 @@ The code uses the `DaprClient` created by the `DaprClientBuilder`. Notice that t
|
|||
|
||||
Run this example with the following command:
|
||||
```sh
|
||||
dapr run --port 3006 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.state.StateClient -D exec.args="'my message'"
|
||||
dapr run --port 3006 -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.state.StateClient -Dexec.args="'my message'"
|
||||
```
|
||||
Once running, the OutputBindingExample should print the output as follows:
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ import reactor.core.publisher.Mono;
|
|||
* mvn clean install
|
||||
* 2. send a message to be saved as state:
|
||||
* dapr run --port 3006 -- \
|
||||
* mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.state.StateClient -D exec.args="'my message'"
|
||||
* mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.state.StateClient -Dexec.args="'my message'"
|
||||
*/
|
||||
public class StateClient {
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ public class DaprRun {
|
|||
|
||||
// the arg in -Dexec.args is the app's port
|
||||
private static final String DAPR_COMMAND =
|
||||
" -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=test -D exec.args=\"%s\"";
|
||||
" -- mvn exec:java -Dexec.mainClass=%s -Dexec.classpathScope=test -Dexec.args=\"%s\"";
|
||||
|
||||
private final DaprPorts ports;
|
||||
|
||||
|
|
Loading…
Reference in New Issue