Reliable Source Code you can depend on - vidyasekaran/GCP GitHub Wiki
Flow : File uploaded to cloudstorage bucket is notified to a topic and springboot app consumes the object and publish it to another topic.
You can perform below 2 labs using above..
git source : git clone https://github.com/bbachi/spring-boot-gcp-pubsub.git
Learn -
- Create a topic and set notification on cloudstorage bucket
gsutil notification create -t vidya-practise2-topic -f json gs://vidya-practise2
- listing notifications on the bucket
gsutil notification list gs://my-dummy-bucket
- Write springboot app to consume object from a subscription.
Lab : Create a CloudStorage bucket - set notification on a topic so that object created in cloudstorage publish it to a pubsub topic
Lab : How To Subscribe and Send PubSub Messages to another topic in Spring Boot App
We have cloud storage and there is a subscription created on the Bucket folder. As soon as we put any file in the cloud storage the message is sent to the Subscription and the Spring Boot receives the details and does some processing and sends these details as a message to another topic.
How To Dockerize Java App and Run Java APIs on GCP Cloud Run
https://medium.com/bb-tutorials-and-thoughts/how-to-run-java-apis-on-gcp-cloud-run-af5dd5f14
**You will learn **
Dockerize Java JPA (H2) project - Build an image - Tag and push it to gcr here project name is very important otherwise you will get permission error iaptest-328809- Run it via Cloud Run
docker tag cloud-run-api gcr.io/iaptest-328809/cloud-run-api:v1 docker push gcr.io/iaptest-328809/cloud-run-api:v1
Lab : Create a Kind in Cloud Datastore and do CRUD from Java API
You can learn
-
How to use DatastoreRepository same like Spring Data JPA to perform CRUD operation.
-
Use CustomPromptProvider for your API so you will get options in command line.
git clone : https://github.com/spring-cloud/spring-cloud-gcp cd spring-cloud-gcp/spring-cloud-gcp-samples/spring-cloud-gcp-data-datastore-basic-sample
Open the code in Cloud Shell Editor
- Modify application.properties to include project id like below
spring.cloud.gcp.datastore.project-id=iaptest-328809 #spring.cloud.gcp.datastore.credentials.location= spring.cloud.gcp.datastore.namespace=spring-demo logging.level.root=WARN
-
Create a Kind in Cloud DataStore in 'spring-demo' namespace.
-
Refer Code : https://github.com/spring-cloud/spring-cloud-gcp/tree/main/spring-cloud-gcp-samples
Clone All your spring boot GCP source code from here you cd into spring-cloud-gcp-samples directory and execute via cloud shell and play with shell editor
git clone https://github.com/spring-cloud/spring-cloud-gcp cd spring-cloud-gcp-samples/spring-cloud-gcp-data-datastore-basic-sample
Maven commands to build and run
mvn clean install mvn spring-boot:run mvn -DskipTests=true clean install
git commands
Switch from main branch to feature
git checkout -b feature
fatal: The current branch feature has no upstream branch. To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature
Ref: https://mydeveloperplanet.com/2019/04/10/deploy-spring-boot-app-to-gcp-app-engine/
Deploy Spring Boot App to GCP App Engine
https://mydeveloperplanet.com/2019/04/10/deploy-spring-boot-app-to-gcp-app-engine/
Using Service account to authenticate
Create a new service account, download its private key and point the spring.cloud.gcp.credentials.location property to it. Such as: spring.cloud.gcp.credentials.location=file:/path/to/creds.json
Create bastion host and login into private machine
https://www.youtube.com/watch?v=yMkyd_s3XEk
Theory : Setup Firewall rule, Bastion host,
Lab : Configure FireWall rules to all SSH into a Virtual machine. Lab : Configure Firewall rule to allow just bastion host to SSH into private machine in a subnet. Lab : Create a Custom VPC - 2 Subnets subnet-a, subnet-b, create 2 VMs one private in subnet-b and another bastion host in subnet-a. Setup Firewall rule to allow SSH from bastion host to private machine.
Create cloudsql and connection from local
Source code used is in samples - git clone https://github.com/vidyasekaran/spring-cloud-gcp.git
setup service account
https://cloud.google.com/docs/authentication/getting-started#windows
service account (allow cloudsql access)
You need to create a service account and add cloudsql admin role and generate key and it downloads in local. C:\Users\shant\Downloads\iaptest-328809-ff9c3af42c04
I created user and password in CloudSQL and added in properties file for it to work.
spring.cloud.gcp.sql.database-name= create db and provide name here spring.cloud.gcp.sql.instance-connection-name=connection name you will get from cloudsql overview page
So app starts despite "table already exists" errors.
spring.datasource.continue-on-error=true
Enforces database initialization
spring.datasource.initialization-mode=always
Leave empty for root, uncomment and fill out if you specified a user
spring.datasource.username=guru
Uncomment if root password is specified
spring.datasource.password=guruguru
spring.cloud.gcp.project-id=iaptest-328809 spring.cloud.gcp.credentials.location=file:iaptest-328809-ff9c3af42c04.json
You need to create a service account and add cloudsql admin role and generate key and it downloads in local, you need to copy it in the project and setup like above.
gcloud sql connect userdb --user=root --quiet
Using Secret Manager to store and reteive passwords etc
Refer : https://spring-gcp.saturnism.me/app-dev/cloud-services/secret-management
You can go to Secret Manager in GCP to create a secret or use cmd line below echo -n "qwerty" | gcloud secrets create order-db-password --data-file=- --replication-policy=automatic