-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PubSub subscriber examples over gPRC (#833)
* add grpc subscriber Signed-off-by: MregXN <[email protected]> * modify README.md Signed-off-by: MregXN <[email protected]> * modify README.md in examples Signed-off-by: MregXN <[email protected]> * Modify DaprApplication to support examples where protocol is not specified. Signed-off-by: MregXN <[email protected]> * modify formatter to pass checkstyle Signed-off-by: MregXN <[email protected]> * Update springboot to latest minor.patch version. (#826) Signed-off-by: MregXN <[email protected]> * Use runtime 1.10.0-rc.X and CLI 1.10.0-rc.X (#827) Signed-off-by: Artur Souza <[email protected]> Signed-off-by: MregXN <[email protected]> * Upgrade the version to 1.9.0-SNAPSHOT (#829) Signed-off-by: Artur Souza <[email protected]> Signed-off-by: MregXN <[email protected]> * Generate updated javadocs for 1.8.0 (#836) Signed-off-by: Artur Souza <[email protected]> Signed-off-by: MregXN <[email protected]> * Update Dapr runtime and CLI to 1.10. (#837) Signed-off-by: Artur Souza <[email protected]> Signed-off-by: MregXN <[email protected]> * Inject autoconfiguration in the Spring Boot 3 style (#831) * Bump from spring boot 2.3.5.RELEASE to 2.7.8 Signed-off-by: Sergio <[email protected]> (cherry picked from commit 9152c91) * Ensure old versions of spring boot are still compatible Signed-off-by: Sergio <[email protected]> --------- Signed-off-by: champel <[email protected]> Signed-off-by: Sergio <[email protected]> Signed-off-by: MregXN <[email protected]> * Bump from reactor 2.3.5.RELEASE to 2.7.8 (#830) * Bump from reactor 2.3.5.RELEASE to 2.7.8 Signed-off-by: Sergio <[email protected]> * Simplification Signed-off-by: Sergio <[email protected]> --------- Signed-off-by: Sergio <[email protected]> Signed-off-by: MregXN <[email protected]> * rerun checks Signed-off-by: MregXN <[email protected]> * modify the way of grpc server starts Signed-off-by: MregXN <[email protected]> * modify README Signed-off-by: MregXN <[email protected]> * Update pom.xml Signed-off-by: MregXN <[email protected]> --------- Signed-off-by: MregXN <[email protected]> Signed-off-by: Artur Souza <[email protected]> Signed-off-by: champel <[email protected]> Signed-off-by: Sergio <[email protected]> Signed-off-by: MregXN <[email protected]> Co-authored-by: Artur Souza <[email protected]> Co-authored-by: champel <[email protected]> Co-authored-by: Mukundan Sundararajan <[email protected]>
- Loading branch information
1 parent
aaa5f7b
commit 375e867
Showing
7 changed files
with
234 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
examples/src/main/java/io/dapr/examples/pubsub/grpc/Subscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2021 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.pubsub.grpc; | ||
|
||
import io.dapr.examples.DaprApplication; | ||
import io.grpc.Server; | ||
import io.grpc.ServerBuilder; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.CommandLineParser; | ||
import org.apache.commons.cli.DefaultParser; | ||
import org.apache.commons.cli.Options; | ||
|
||
/** | ||
* Service for subscriber. | ||
* 1. Build and install jars: | ||
* mvn clean install | ||
* 2. cd [repo root]/examples | ||
* 3. Run the server: | ||
* dapr run --components-path ./components/pubsub --app-id subscriber --app-port 3000 -- \ | ||
* java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.grpc.Subscriber -p 3000 | ||
*/ | ||
public class Subscriber { | ||
|
||
/** | ||
* This is the entry point for this example app, which subscribes to a topic. | ||
* @param args The port this app will listen on. | ||
* @throws Exception An Exception on startup. | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
Options options = new Options(); | ||
options.addRequiredOption("p", "port", true, "The port this app will listen on"); | ||
|
||
CommandLineParser parser = new DefaultParser(); | ||
CommandLine cmd = parser.parse(options, args); | ||
|
||
// If port string is not valid, it will throw an exception. | ||
int port = Integer.parseInt(cmd.getOptionValue("port")); | ||
|
||
//start a grpc server | ||
Server server = ServerBuilder.forPort(port) | ||
.addService(new SubscriberGrpcService()) | ||
.build(); | ||
server.start(); | ||
server.awaitTermination(); | ||
|
||
// Start Dapr's callback endpoint. | ||
DaprApplication.start("grpc",port); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
examples/src/main/java/io/dapr/examples/pubsub/grpc/SubscriberGrpcService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2021 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.pubsub.grpc; | ||
|
||
import com.google.protobuf.Empty; | ||
import io.dapr.v1.AppCallbackGrpc; | ||
import io.dapr.v1.DaprAppCallbackProtos; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Class that encapsulates all client-side logic for Grpc. | ||
*/ | ||
public class SubscriberGrpcService extends AppCallbackGrpc.AppCallbackImplBase { | ||
private final List<DaprAppCallbackProtos.TopicSubscription> topicSubscriptionList = new ArrayList<>(); | ||
|
||
@Override | ||
public void listTopicSubscriptions(Empty request, | ||
StreamObserver<DaprAppCallbackProtos.ListTopicSubscriptionsResponse> responseObserver) { | ||
registerConsumer("messagebus","testingtopic"); | ||
try { | ||
DaprAppCallbackProtos.ListTopicSubscriptionsResponse.Builder builder = DaprAppCallbackProtos | ||
.ListTopicSubscriptionsResponse.newBuilder(); | ||
topicSubscriptionList.forEach(builder::addSubscriptions); | ||
DaprAppCallbackProtos.ListTopicSubscriptionsResponse response = builder.build(); | ||
responseObserver.onNext(response); | ||
} catch (Throwable e) { | ||
responseObserver.onError(e); | ||
} finally { | ||
responseObserver.onCompleted(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onTopicEvent(DaprAppCallbackProtos.TopicEventRequest request, | ||
StreamObserver<DaprAppCallbackProtos.TopicEventResponse> responseObserver) { | ||
try { | ||
String data = request.getData().toStringUtf8().replace("\"", ""); | ||
System.out.println("Subscriber got: " + data); | ||
DaprAppCallbackProtos.TopicEventResponse response = DaprAppCallbackProtos.TopicEventResponse.newBuilder() | ||
.setStatus(DaprAppCallbackProtos.TopicEventResponse.TopicEventResponseStatus.SUCCESS) | ||
.build(); | ||
responseObserver.onNext(response); | ||
responseObserver.onCompleted(); | ||
} catch (Throwable e) { | ||
responseObserver.onError(e); | ||
} | ||
} | ||
|
||
/** | ||
* Add pubsub name and topic to topicSubscriptionList. | ||
* | ||
* @param topic the topic | ||
* @param pubsubName the pubsub name | ||
*/ | ||
public void registerConsumer(String pubsubName, String topic) { | ||
topicSubscriptionList.add(DaprAppCallbackProtos.TopicSubscription | ||
.newBuilder() | ||
.setPubsubName(pubsubName) | ||
.setTopic(topic) | ||
.build()); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters