{"id":9,"date":"2021-01-20T22:14:39","date_gmt":"2021-01-20T22:14:39","guid":{"rendered":"http:\/\/sre-blog.ant0ny.net\/wordpress\/?p=9"},"modified":"2021-01-21T09:42:18","modified_gmt":"2021-01-21T09:42:18","slug":"azure-app-configuration-with-spring-boot","status":"publish","type":"post","link":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/2021\/01\/20\/azure-app-configuration-with-spring-boot\/","title":{"rendered":"Azure App Configuration with Spring Boot"},"content":{"rendered":"\n<p>Today I wanted to see if I could get a Java Spring Boot app talking to App Configuration in Azure to consume some dummy configuration as a simple proof of concept.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-app-configuration\/overview\">https:\/\/docs.microsoft.com\/en-us\/azure\/azure-app-configuration\/overview<\/a><\/p>\n\n\n\n<p>Azure App Config is a powerful tool for storing configuration, references to secrets in key vaults and it also has feature flag functionality. Microsoft have been clever here with this service and combined 3 distinct concepts into a &#8216;One Stop Shop&#8217; place in which to retrieve your app&#8217;s config.<\/p>\n\n\n\n<p>I set about creating a simple app Java Spring app, I created a simple template using the helpful Spring Initializer and accepted all the defaults.<\/p>\n\n\n\n<p><a href=\"https:\/\/start.spring.io\/\">https:\/\/start.spring.io\/<\/a><\/p>\n\n\n\n<p>This created a demo.zip folder which was downloaded locally and then added the following files:-<\/p>\n\n\n\n<p>MessageProperties.java file in my package folder in my case src\/main\/java\/com\/example\/demo<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.example.demo;\n\nimport org.springframework.boot.context.properties.ConfigurationProperties;\n\n@ConfigurationProperties(prefix = \"config\")\npublic class MessageProperties {\n    private String message;\n\n    public String getMessage() {\n        return message;\n    }\n\n    public void setMessage(String message) {\n        this.message = message;\n    }\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"421\" src=\"http:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-19.59.04-1024x421.png\" alt=\"\" class=\"wp-image-16\" srcset=\"https:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-19.59.04-1024x421.png 1024w, https:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-19.59.04-300x123.png 300w, https:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-19.59.04-768x315.png 768w, https:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-19.59.04-1536x631.png 1536w, https:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-19.59.04-2048x841.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p> HelloContoller.java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.example.demo;\n\nimport org.springframework.web.bind.annotation.RestController;\nimport org.springframework.web.bind.annotation.RequestMapping;\n\n@RestController\npublic class HelloController {\n    private final MessageProperties properties;\n\n    public HelloController(MessageProperties properties) {\n        this.properties = properties;\n    }\n\n    @RequestMapping(\"\/\")\n    public String index() {\n        return \"Message: \" + properties.getMessage();\n    }\n}<\/pre>\n\n\n\n<p>Application.java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.example.demo;\n\nimport java.util.Arrays;\n\nimport org.springframework.boot.CommandLineRunner;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.annotation.Bean;\n\nimport org.springframework.boot.context.properties.EnableConfigurationProperties;\n\n\n@SpringBootApplication\n@EnableConfigurationProperties(MessageProperties.class)\npublic class Application {\n\n\tpublic static void main(String[] args) {\n\t\tSpringApplication.run(Application.class, args);\n\t};\n\n}<\/pre>\n\n\n\n<p>And finally add an reference to the environment variable to get the connection string to the App Config instance in a bootstrap.properties file in your resources folder<\/p>\n\n\n\n<p>bootstrap.properties<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING}<\/pre>\n\n\n\n<p>There is a pre-req to have an Azure App Configuration instance running and a configuration key:-<br><br> \/application\/config.message  <\/p>\n\n\n\n<p>and a value of:- <\/p>\n\n\n\n<p>Hello from Azure Configuration<\/p>\n\n\n\n<p>Like this&#8230;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"983\" height=\"232\" src=\"http:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-22.54.59.png\" alt=\"\" class=\"wp-image-24\" srcset=\"https:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-22.54.59.png 983w, https:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-22.54.59-300x71.png 300w, https:\/\/sre-blog.ant0ny.net\/wordpress\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-20-at-22.54.59-768x181.png 768w\" sizes=\"(max-width: 983px) 100vw, 983px\" \/><\/figure>\n\n\n\n<p>Also you will need to grab the Connection string from the &#8216;Access Keys&#8217; section of App Configuration and add it to the export to set the environment variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">export APP_CONFIGURATION_CONNECTION_STRING='Endpoint=https:\/\/my-azure-app-config.azconfig.io;Id=xxxxxxxxxxx;Secret=Ixxxxxxxxxxxxxxxxxx'\n\nmvn clean package\n\nmvn spring-boot:run\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Goto http:\/\/localhost:8080 in your browser and observe the config pulled from Azure App Config &#8211; it works!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I wanted to see if I could get a Java Spring Boot app talking to App Configuration in Azure<\/p>\n<p><a href=\"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/2021\/01\/20\/azure-app-configuration-with-spring-boot\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\">Azure App Configuration with Spring Boot<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/9"}],"collection":[{"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":14,"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/9\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sre-blog.ant0ny.net\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}