Giter Club home page Giter Club logo

aws-publisher-subscriber-pattern-java's Introduction

Integrating microservices by using AWS serverless services

Publisher/Subscriber pattern - Asynchronous Communication

This is a Java implementation of the Publisher/Subscriber Pattern as described in the official AWS documentation https://docs.aws.amazon.com/prescriptive-guidance/latest/modernization-integrating-microservices/pub-sub.html

Topology topology

Description

The communication style in this pattern is asynchronous. As you can see from the topology, the Lambda function is publishing messages to an SNS topic. There are three subscriptions, two services and an email subscription, but you can also create other subscriptions using services like SQS or Kinesis Data Firehose. This pattern is also known as Fanout pattern. The Lambda functions will process the messages asynchronously and the first Lambda will not expect any response from the downstream processors.

The SAM template contains all the information to deploy AWS resources and also the permission required by these service to communicate.

You will be able to create and delete the CloudFormation stack using AWS SAM.

This is fully functional example implemented in Java 11.

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred.

Language:

This is a Maven project which uses Java 11 and AWS SDK

Framework

The framework used to deploy the infrastructure is SAM

Services used

The AWS services used in this pattern are

API Gateway - AWS Lambda - DynamoDB - Amazon SNS

Deployment commands

mvn clean package

# create an S3 bucket where the source code will be stored:
aws s3 mb s3://qsmklpcoiw3i20d92idnkqd

# upload the source code to S3:
aws s3 cp target/sourceCode.zip s3://qsmklpcoiw3i20d92idnkqd

# SAM will deploy the CloudFormation stack described in the template.yml file:
sam deploy --s3-bucket qsmklpcoiw3i20d92idnkqd --stack-name pub-sub-pattern --capabilities CAPABILITY_IAM

Testing

# Subscribe to the SNS Topic to receive the confirmation email

# use aws cli to show the SNS Topics
aws sns list-topics

# subscribe to the SNS topic with your email address and confirm your subscription
aws sns subscribe \
    --topic-arn arn:aws:sns:eu-central-1:YOUR_AWS_ACCOUNT:Communication \
    --protocol email \
    --notification-endpoint [email protected]
    
# Copy the API Gateway URL for the Payments endpoint from the sam deploy outputs
#  i.e. 
# Outputs                                                                                                                                                                                                                             
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Key                 PaymentsEndpoint                                                                                                                                                                                                      3
# Description         API Gateway Payments Endpoint                                                                                                                                                 
# Value               https://______________/prod/payments


# Call the API Gateway 

curl -X POST https://API_GATEWAY_URL -H "Content-Type: application/json" -d '{"customerId": "111-222","customerFullName":"John Smith","customerEmail": "[email protected]","amount": "99.99","product": "car insurance"}' 

# you should receive an email with the information
# and also see in the console the correlationId
# i.e. "040ca0f3-0cdb-4c0d-9377-6bb3edd9308f"

# Scan the DynamoDB CustomerPayments 
aws dynamodb scan --table-name CustomerPayments

# Scan the DynamoDB Sales 
aws dynamodb scan --table-name Sales    

Cleanup

Run the given command to delete the resources that were created. It might take some time for the CloudFormation stack to get deleted.

aws cloudformation delete-stack --stack-name pub-sub-pattern

aws s3 rm s3://qsmklpcoiw3i20d92idnkqd --recursive

aws s3 rb s3://qsmklpcoiw3i20d92idnkqd

Requirements

Author

Razvan Minciuna https://www.linkedin.com/in/razvanminciuna/

aws-publisher-subscriber-pattern-java's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.