Skip to content

Using Spring Cloud Azure with Azure Emulators

zhihaoguo edited this page Jun 21, 2022 · 1 revision

This article describes how to use Cosmos and Storage emulators with Spring Cloud Azure.
The demos used in this article are spring-cloud-azure-data-cosmos-sample and storage-blob-sample.

Azure Cosmos Emulator

Step01: Run the emulator on Docker for Linux

  1. Follow the instruction on this page to run the cosmos emulator on Docker.

Step02: Download the certificate

  1. After the emulator is running, using a different terminal, load the IP address of your local machine into a variable.
    ipaddr="`ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1`"
    
  2. Next, download the certificate for the emulator.
    curl -k https://$ipaddr:8081/_explorer/emulator.pem > emulatorcert.crt
    

Step03: Use the certificate with Java apps

Run the following bash script to import the certificate:

#!/bin/bash

# If emulator was started with /AllowNetworkAccess, replace the below with the actual IP address of it:
EMULATOR_HOST=localhost
EMULATOR_PORT=8081
EMULATOR_CERT_PATH=/Users/plato1233/Desktop/emulatorcert.crt
openssl s_client -connect ${EMULATOR_HOST}:${EMULATOR_PORT} </dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > $EMULATOR_CERT_PATH
# delete the cert if already exists
# you may need to input the keystore password:changeit
sudo $JAVA_HOME/bin/keytool -cacerts -delete -alias cosmos_emulator
# import the cert
# you may need to input the keystore password:changeit
sudo $JAVA_HOME/bin/keytool -cacerts -importcert -alias cosmos_emulator -file $EMULATOR_CERT_PATH

Step04: Configure spring-cloud-azure-data-cosmos-sample

Update application.yml in spring-cloud-azure-data-cosmos-sample with following configuration:

spring:
  cloud:
    azure:
      cosmos:
        endpoint: https://localhost:8081
        database: test-emulator
        key: C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

Step05: Run the sample with Maven

In your terminal, run mvn clean spring-boot:run.

mvn clean spring-boot:run

Azure Storage Blob Emulator

Step01: Run Azurite with Docker

docker run -p 10000:10000 mcr.microsoft.com/azure-storage/azurite azurite-blob --blobHost 0.0.0.0

Please refer to this page for more information about Azurite.

Step02: Configure storage-blob-sample

Update application.yml in storage-blob-sample with following configuration:

spring:
  cloud:
    azure:
      storage:
        blob:
          account-name: devstoreaccount1
          container-name: testcantainer
          account-key: Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==
          endpoint: http://127.0.0.1:10000/devstoreaccount1

Step03: Run the sample with Maven

In your terminal, run mvn clean spring-boot:run.

mvn clean spring-boot:run

References

Clone this wiki locally