Accessing CloudFoundry user-provided services using Spring Cloud connectors

cloud-foundry, java, spring, spring-cloud, spring-mvc

Solution

Spring Cloud Connectors won't know what to do with this service, as each supported service must be of a known type (MySQL, Postgres, Redis, MongoDB, RabbitMQ, etc). Setting the `connector-type` to your Controller class won't do what you want.

What you will need to do is to create a custom Connectors extension. Here's an example of a project that does that: https://github.com/cf-platform-eng/spring-boot-cities.

Problem

I'm trying to use Spring Cloud to consume a generic REST service from a Cloud Foundry app. This service is created using Spring Boot, as follows: ``` package com.something; @RestController public class DemoServiceController { @RequestMapping("/sayHi") public String sayHi() { return "Hello!"; } } ``` This works fine - I can access `http://www.example.com/srv/demo/sayHi` and get "Hello!" back. Next, I created a user-provided service instance using the CF-CLI and bound it to my app. I can now see the bound service in `VCAP_SERVICES`. ``` cf cups my-demo-service -p '{"url":"http://www.example.com/srv/demo/"}' cf bs my-demo-app my-demo-service ``` Next, as described here, I added this bean to my app's Spring config, with the `connector-type` set to my original controller (I have a reference to it as well). ``` <cloud:service id="myDemoService" service-name="my-demo-service" connector-type="com.something.DemoServiceController" /> ``` Now when I auto-wire `"myDemoService"` into my app, ``` @Autowired private DemoController myDemoService; ``` I get an error: No services of the specified type could be found. I've made sure to include all required dependencies, including `spring-cloud-spring-service-connector` and `spring-cloud-cloudfoundry-connector`. What's going wrong here? Am I giving the wrong bean parameters? Any help is much appreciated.

Original source