Cloud Foundry provides a service instance is essentially access information: IP, port, user name, password, etc., the use of the process of course can be written dead in the code or configuration file, but the advanced way of course is to let the program automatically according to the environment to adapt the service instance information, switch database, message middleware, Location of code connections such as cloud storage. This article first describes how the spring program with listener configured in Web. XML automatically obtains the service instance information for CF, and the sample code is managed by MAVEN.
The following listener spring programs are configured in Web. XML, and the service information bound in the PAAs can be automatically obtained by defining the bean in the context configuration file using the specified syntax.
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
For example, to use the Redis service, simply define the following in the context configuration file:
<cloud:redis-connection-factory id="redisConnectionFactory"/>
Then use the following code in your program to get the bundled Redis service information:
@Autowired(required=false) RedisConnectionFactory redisConnectionFactory;Hostname = ((JedisConnectionFactory) redisConnectionFactory).getHostName();Port = ((JedisConnectionFactory) redisConnectionFactory).getPort());
For Redis, this bean can be manipulated directly through the Spring-data-redis.
Due to the compatibility of spring with Cloud foundry and its related services (such as Redis), different versions of the spring library may cause compatibility issues such as spring 3.2.8 and Spring-cloud 1.0.0 put together will cause programs that are bound to the Redis service to not start on the PAAs. Make sure that the compatible version is available in the Pom.xml of the sample program (which is a MAVEN project).
The spring program reads the Cloud Foundry Service information sample code