
{"id":142,"date":"2014-07-04T22:36:49","date_gmt":"2014-07-04T12:36:49","guid":{"rendered":"http:\/\/mnm.at\/markus\/?p=142"},"modified":"2015-03-11T20:12:18","modified_gmt":"2015-03-11T09:12:18","slug":"spring-boot-openshift-auto-reconfiguration","status":"publish","type":"post","link":"https:\/\/mnm.at\/markus\/2014\/07\/04\/spring-boot-openshift-auto-reconfiguration\/","title":{"rendered":"Spring Boot + OpenShift + Auto-Reconfiguration"},"content":{"rendered":"<p>I wanted to know how easy it would be to get Spring-Boot auto-reconfiguration to work in OpenShift. It did not really work for me in Cloud Foundry without some massaging ( <a href=\"\/markus\/2014\/07\/02\/spring-boot-cloud-foundry\/\">auto re-config blog<\/a> ).<\/p>\n<p>The proper way would be to extend https:\/\/github.com\/spring-projects\/spring-cloud\/tree\/master\/spring-cloud-core for OpenShift Which is probably dead simple, too, but I wanted to do a quick 30 minute hack which become about a 90 minutes dirty hack.<\/p>\n<p>In simple terms, the cartridge always sets -Dspring.profiles.active=cloud (unless this is overwritten by the app developer). And when it finds a &#8216;OPENSHIFT_MYSQL_DB_URL&#8217; environment variable (which means the user added that service (cartridge) to the application, then the cartridge will add a the MariaDB (MySQL) information to &#8216;VCAP_SERVICES&#8217; In addition I als set &#8216;VCAP_APPLICATION&#8217; to some partially correct values, as well as VCAP_APP_HOST and VCAP_APP_PORT.<\/p>\n<p>Then I deployed the jar generated from the <a href=\"https:\/\/github.com\/markus-mnm\/spring-rest-backbone\/tree\/cloud-foundry-autoreconfig-tests\/Unfamous-Quotes\">unfamous-quotes cloud-foundry-autoreconfig-tests branch<\/a> and it correctly picked up the MySQL DB and <strong>SUCCESS<\/strong>.<\/p>\n<p>It was ridiculously easy to get that working in OpenShift. If I hadn&#8217;t put in a typo in one of my first attempts it would have been even easier.<\/p>\n<p>What I did not mentioned in the last blog was that the version of the <a href=\"https:\/\/github.com\/markus-mnm\/spring-rest-backbone\/tree\/cloud-foundry-autoreconfig-tests\/Unfamous-Quotes\">&#8220;unfamous quotes&#8221;<\/a> code only works on Cloud Foundry if there is a data source supplied by Cloud Foundry. Otherwise you get:<\/p>\n<pre><code>org.springframework.cloud.CloudException: No unique service matching interface javax.sql.DataSource found. Expected 1, found 0\n<\/code><\/pre>\n<p>This happens due to the this code in <code>DataSourceConfiguration<\/code>:<\/p>\n<pre><code>return connectionFactory().dataSource();    \n<\/code><\/pre>\n<p>With the <a href=\"https:\/\/bitbucket.org\/markus_ebenhoeh\/openshift-cartridge-spring-boot-jar-runner\/commits\/ff227aeb8c73a353b0486e90aafe68e8fa5a7602\">current version<\/a> of the spring runner cartridge the same happens: You have to add a MariaDB or it wont work.<\/p>\n<p>Since this was just a quick and dirty test, I did not add any other DB services. But I also would rather check out the spring-core-cloud and create an OpenShift Cloud Connector.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Played with my spring-boot cartridge (http:\/\/mnm.at\/markus\/2014\/06\/15\/building-an-openshift-cartridge) and added some environment variables to make it seem to spring-cloud as if it is running in Cloud Foundry. <a href=\"https:\/\/mnm.at\/markus\/2014\/07\/04\/spring-boot-openshift-auto-reconfiguration\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[20,15,17,18,19,23,24],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-tech","tag-auto-reconfiguration","tag-cloud-foundry","tag-java","tag-openshift","tag-openshift-cartridge","tag-spring-framework","tag-spring-boot"],"_links":{"self":[{"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":5,"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":198,"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/posts\/142\/revisions\/198"}],"wp:attachment":[{"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mnm.at\/markus\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}