github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/docs/content/pubsubs/googlecloud.md (about) 1 +++ 2 title = "Google Cloud Pub/Sub" 3 description = "The fully-managed real-time messaging service from Google" 4 date = 2019-07-06T22:30:00+02:00 5 bref = "The fully-managed real-time messaging service from Google" 6 weight = -60 7 type = "docs" 8 toc = false 9 +++ 10 11 ### Google Cloud Pub/Sub 12 13 Cloud Pub/Sub brings the flexibility and reliability of enterprise message-oriented middleware to 14 the cloud. 15 16 At the same time, Cloud Pub/Sub is a scalable, durable event ingestion and delivery 17 system that serves as a foundation for modern stream analytics pipelines. 18 By providing many-to-many, asynchronous messaging that decouples senders and receivers, 19 it allows for secure and highly available communication among independently written applications. 20 21 Cloud Pub/Sub delivers low-latency, durable messaging that helps developers quickly integrate 22 systems hosted on the Google Cloud Platform and externally. 23 24 Documentation: [https://cloud.google.com/pubsub/docs/](https://cloud.google.com/pubsub/docs/overview) 25 26 ### Installation 27 28 go get github.com/ThreeDotsLabs/watermill-googlecloud 29 30 #### Characteristics 31 32 | Feature | Implements | Note | 33 | ------- | ---------- | ---- | 34 | ConsumerGroups | yes | multiple subscribers within the same Subscription name | 35 | ExactlyOnceDelivery | no | | 36 | GuaranteedOrder | no | | 37 | Persistent | yes* | maximum retention time is 7 days | 38 39 #### Configuration 40 41 {{% render-md %}} 42 {{% load-snippet-partial file="src-link/watermill-googlecloud/pkg/googlecloud/publisher.go" first_line_contains="type PublisherConfig struct " last_line_contains="func NewPublisher" %}} 43 {{% /render-md %}} 44 45 {{% render-md %}} 46 {{% load-snippet-partial file="src-link/watermill-googlecloud/pkg/googlecloud/subscriber.go" first_line_contains="type SubscriberConfig struct {" last_line_contains="func NewSubscriber(" %}} 47 {{% /render-md %}} 48 49 ##### Subscription name 50 51 To receive messages published to a topic, you must create a subscription to that topic. 52 Only messages published to the topic after the subscription is created are available to subscriber 53 applications. 54 55 The subscription connects the topic to a subscriber application that receives and processes 56 messages published to the topic. 57 58 A topic can have multiple subscriptions, but a given subscription belongs to a single topic. 59 60 In Watermill, the subscription is created automatically during calling `Subscribe()`. 61 Subscription name is generated by function passed to `SubscriberConfig.GenerateSubscriptionName`. 62 By default, it is just the topic name (`TopicSubscriptionName`). 63 64 When you want to consume messages from a topic with multiple subscribers, you should use 65 `TopicSubscriptionNameWithSuffix` or your custom function to generate the subscription name. 66 67 #### Connecting 68 69 Watermill will connect to the instance of Google Cloud Pub/Sub indicated by the environment variables. For production setup, set the `GOOGLE_APPLICATION_CREDENTIALS` env, as described in [the official Google Cloud Pub/Sub docs](https://cloud.google.com/pubsub/docs/quickstart-client-libraries#pubsub-client-libraries-go). Note that you won't need to install the Cloud SDK, as Watermill will take care of the administrative tasks (creating topics/subscriptions) with the default settings and proper permissions. 70 71 For development, you can use a Docker image with the emulator and the `PUBSUB_EMULATOR_HOST` env ([check out the Getting Started guide]({{< ref "getting-started#subscribing_gcloud" >}})). 72 73 {{% render-md %}} 74 {{% load-snippet-partial file="src-link/_examples/pubsubs/googlecloud/main.go" first_line_contains="publisher, err :=" last_line_contains="panic(err)" padding_after="1" %}} 75 {{% /render-md %}} 76 77 {{% render-md %}} 78 {{% load-snippet-partial file="src-link/_examples/pubsubs/googlecloud/main.go" first_line_contains="subscriber, err :=" last_line_contains="panic(err)" padding_after="1" %}} 79 {{% /render-md %}} 80 81 #### Publishing 82 83 {{% render-md %}} 84 {{% load-snippet-partial file="src-link/watermill-googlecloud/pkg/googlecloud/publisher.go" first_line_contains="// Publish" last_line_contains="func (p *Publisher) Publish" %}} 85 {{% /render-md %}} 86 87 #### Subscribing 88 89 {{% render-md %}} 90 {{% load-snippet-partial file="src-link/watermill-googlecloud/pkg/googlecloud/subscriber.go" first_line_contains="// Subscribe " last_line_contains="func (s *Subscriber) Subscribe" %}} 91 {{% /render-md %}} 92 93 #### Marshaler 94 95 Watermill's messages cannot be directly sent to Google Cloud Pub/Sub - they need to be marshaled. You can implement your marshaler or use the default implementation. 96 97 {{% render-md %}} 98 {{% load-snippet-partial file="src-link/watermill-googlecloud/pkg/googlecloud/marshaler.go" first_line_contains="// Marshaler" last_line_contains="type DefaultMarshalerUnmarshaler " padding_after="0" %}} 99 {{% /render-md %}} 100 101