The spring program reads the Cloud Foundry Service information sample code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.