DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Improving Backend Performance Part 1/3: Lazy Loading in Vaadin Apps
  • Best Performance Practices for Hibernate 5 and Spring Boot 2 (Part 4)
  • Best Performance Practices for Hibernate 5 and Spring Boot 2 (Part 1)
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services

Trending

  • Mastering Advanced Aggregations in Spark SQL
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • Memory Leak Due to Time-Taking finalize() Method
  1. DZone
  2. Data Engineering
  3. Databases
  4. Azure SQL With PCF Spring Boot Applications (Part 1 — GeoReplication)

Azure SQL With PCF Spring Boot Applications (Part 1 — GeoReplication)

One of the best features about Azure is it's high avaliability, and this tutorial shows you how to take advantage of that with Spring Boot apps.

By 
Elena Neroslavskaya user avatar
Elena Neroslavskaya
·
Updated Aug. 13, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
9.0K Views

Join the DZone community and get the full member experience.

Join For Free

This is part 1 of a 2-part series demonstrating how to leverage advanced Azure SQL (PaaS) features from Java Spring Boot applications running on PCF (Pivotal CloudFoundry) on Azure. The first article will show you how to use a Spring Boot application with Azure SQL Database auto-failover groups to provide resilience to regional outages, and the next article will show you how to use the Azure SQL AlwaysEncrypted feature in Spring Boot applications to provide encryption for PI data in the cloud.

This article demonstrates how to use Azure SQL auto-failover groups in SpringBoot applications running on PCF, configuring  for the seamless integration of the Java apps with Azure services and using Service Broker to connect PCF applications with Azure SQL service.

Note: On the day of publishing, the new Open Service Broker for Azure (OSBA) does not support features described in this article as they are not yet available.

First, let’s look at the setup of PCF applications for high-availability. To prevent app downtime, PCF applications could in active-active or active-standby mode, where two or more identical PCF deployments are installed in different data centers. If one PCF deployment becomes unavailable, traffic is seamlessly routed to the other deployment. Applications are deployed to both PCF deployments in parallel, to achieve identical deployments. For resiliency on the data layer, PCF Applications connect to the Azure SQL database that is geo-replicated between Azure regions. In the event of region outage, all components will failover together. The diagram below shows the topology:

Image title

PCF applications are configured to access Azure SQL database via auto-failover group read-write listener, using FQDN <failover-group-name>.database.windows.net. In case the region outage traffic is directed to the secondary region and the SQL Database service detects that the primary database is not accessible and triggers failover to the secondary region, FQDN is seamlessly directing data to the secondary region without requiring application changes or restarts.  For more information on auto-failover group setup please refer to this Microsoft webpage.

Azure Service Broker Setup

Let’s review how the Meta Azure Service Broker should be set up to provide integration with Azure SQL in multiple regions.  Service broker could be installed on PCF as a Pivotal Tile in Ops Manager or just pushed as an application in PCF system org. We will show the Tile deployment mode:

  1. Download and install MASB from pivotal network into the Ops Manager and follow documentation on setting up connectivity to Azure Subscription, providing a database to save the broker configuration.Image title
  2. In enterprises, the broker is usually set to use pre-configured SQL servers so that databases created in it will inherit security/audit/management settings. Prepare Azure SQL servers in a primary and secondary region. The broker will create and replicate databases in the specified servers. In our example, the following servers were created in two regions:Image title
  3. Next, configure both the primary and secondary target SQL servers in the service broker — resources groups, server admin credentials and regions of the created servers in the step above. Image title
  4. Save and Apply changes in Ops Manager to install the service broker.

Spring Boot Application

Now that we have the broker installed, we will deploy a simple Spring Boot application – Spring Music — to PCF and create services it will bind to.

The Spring Music application leverages Spring AutoConfiguration and Spring Cloud Connectors to detect the type of database and provide the datasource to the application. To enable SQL Server for the application, the following is included in the SpringMusic:

  • SQL Server driver is included in build.gradle
  •  RelationalCloudDataSourceConfig includes “sqlserver-cloud” profile assigned by CloudConnectors when application is running in PCF and has SQL server service bound to it.
@Configuration
@Profile({"mysql-cloud", "postgres-cloud", "oracle-cloud", "sqlserver-cloud"})
public class RelationalCloudDataSourceConfig extends AbstractCloudConfig {

    private static final Logger logger = LoggerFactory.getLogger(RelationalCloudDataSourceConfig.class);


    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSource dataSource() {
        logger.info("CREATING SQL DS on the Cloud");
        return connectionFactory().dataSource();
    }
}


That’s all that is needed in application code to be able to run in PCF and connect to SQL server.

PCF Application and Services Setup

Now we have to prepare services that would be used by application to integrate with Azure SQL and auto-failover groups.

Run the cf marketplace command to see the services available after installing Azure Service Broker.

Image title

Primary Region PCF Application setup

First, we will use Service Broker to create a database in the primary region, set up the auto-failover group, and geo-replicate the database to the secondary region, and push application that will use the failover group connection:

Image title

To create a database in the primary region we could use the “azure-sqldb” service:

 cf create-service azure-sqldb PremiumP2 springdemodb -c ./springmusicdb.json 

Where the JSON file specifies SQL server and the database name in configuration options:

{
  "sqlServerName": "springprimarydemo",
  "sqldbName": "springmusicdb",
  "transparentDataEncryption": false,
  "sqldbParameters": {
    "properties": {
      "collation": "SQL_Latin1_General_CP1_CI_AS"
    }
  }
}

And next step is to create an Azure SQL database auto-failover group, that would provide database FQDN for the JDBC URL application will use. There are two plans for the failover service:

Image title

In a primary region we run cf command that creates the failover group and replicates the database, and we will use a plan called "SecondaryDatabaseWithFailoverGroup”:

 cf create-service azure-sqldb-failover-group SecondaryDatabaseWithFailoverGroup

springfailoverdb2 -c ./failover.json 

The JSON configuration provides details on the Primary and secondary database servers, failovergroup name, and database to be replicated in a failover group:

{
  "primaryServerName": "springprimarydemo",
  "primaryDbName": "springmusicdb",
  "secondaryServerName": "springsecondarydemo",
  "failoverGroupName": "springfailoverdb2"
}

Once the operation succeeds you can review details of the created Failover group:

Image title

Note: if you are getting errors at this point, one of the reasons might be that both primary or secondary servers were not properly defined in the Service Broker/Tile config.

At this point we could "cf push -f manifest.yml" our SpringBoot application to PCF, providing "manifest.yml" that specifies newly created service to be bound to:

---
applications:
- name: spring-music
  memory: 1G
  buildpack: java_buildpack_offline
  path: build/libs/spring-music-1.0.jar
  routes:
    - route: spring-music-eneros.apps.137.135.115.90.cf.pcfazure.com
  services:
   - springfailoverdb2

Once the is application started on PCF we could see that VCAP_SERVICES   information contains jdbcUrl  pointing to SQL failover group FQDN:

Image title

Navigate to the application and you could see active spring profiles, and services bound to the application:

Image title

Secondary Region PCF Application setup

Now we need to install PCF application in secondary region, and application configuration should be identical so that we do not have configuration drift. We could reuse same manifest.yaml and application binary that was deployed in the primary region. The only difference will be in how we create the service for SQL auto-failover group, since the group and the secondary replicated database already exists.

We will leverage service broker plan called “ExistingDatabaseInFailoverGroup”, which will validate that database and failover group exists and will provide their configuration to application:

 cf create-service azure-sqldb-failover-group  ExistingDatabaseInFailoverGroup

springfailoverdb2 -c ./failover.json 

Deploy application using `cf push -f manifest.yml’.

And we could validate that VCAP_SERVICES point jdbcURL to the same FQDN, and provisioned new user credentials for the secondary application to connect.

The resulting interaction and topology diagram:

Image title

Failover test

To test solution initiate failover in the portal, and observe that application seamlessly starts getting the data from the secondary region database. SQL data replication is base on the Always-On feature of SQL server that mirrors the data between the regions asynchronously.

Image title

Conclusion

In this example, we demonstrated connecting PCF application with Azure SQL geo-replicated databases, that provide resiliency and high-availability. We went under the hood work of Azure Service Broker and how easy it is for Java applications to consume advanced Azure features through Broker.  

For non-PCF Java applications, Spring and not Spring-based, a similar setup could be achieved by implementing provisioning and replication of Azure SQL databases using scripts, and copying FQDN and user credentials in Java configuration. Another approach might be to leverage new Azure Spring Cloud integration libraries  https://github.com/Microsoft/spring-cloud-azure


Special thanks to Ning Kuang and team of Azure PCF developers for constantly improving Service Broker to meet our customers demands.


application Spring Framework sql azure Spring Boot Database

Opinions expressed by DZone contributors are their own.

Related

  • Improving Backend Performance Part 1/3: Lazy Loading in Vaadin Apps
  • Best Performance Practices for Hibernate 5 and Spring Boot 2 (Part 4)
  • Best Performance Practices for Hibernate 5 and Spring Boot 2 (Part 1)
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!