Use redis and rabbitmq key cloud cloud solution using vcap service in spring boot application

I can use the services REDIS and RABBITMQ, which are on the main page. While the bindings service I can get the credentials and use those credentials in my application.properties for the spring boot project.

But this config I am using is hardcoded in application.Properties In order to dynamically tweak this config I found out that we can use vcap services provided by pivot.

Therefore you want to use runtime credentials for redis and rabbimq.

See my code below for reference.

application.propeties

rabbitmq.host=hostname
rabbitmq.virtual-host=vhostanme
rabbitmq.username=username
rabbitmq.password=password
rabbit.mainqueue=abhi
rabbit.errorqueue=abc
redis.host=redishostname
redis.port=port
redis.password=password

      

My Config class:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Config {


    static String rabbitMqHost;
    static String rabbitMqVHost;
    static String rabbitMqUsername;
    static String rabbitMqPassword;
    static String rabbitMqMainQueue;
    static String rabbitMqErrorQueue;
    static String redisHost;
    static int redisPort;
    static String redisPassword;


    Config() {
    }


    public static String getRedisHost() {
        return redisHost;
    }

    public static void setRedisHost(String redisHost) {
        Config.redisHost = redisHost;
    }

    public static int getRedisPort() {
        return redisPort;
    }

    public static void setRedisPort(int redisPort) {
        Config.redisPort = redisPort;
    }

    public static String getRedisPassword() {
        return redisPassword;
    }

    public static void setRedisPassword(String redisPassword) {
        Config.redisPassword = redisPassword;
    }

    public static String getRabbitMqMainQueue() {
        return rabbitMqMainQueue;
    }

    public static void setRabbitMqMainQueue(String rabbitMqMainQueue) {
        Config.rabbitMqMainQueue = rabbitMqMainQueue;
    }

    public static String getRabbitMqErrorQueue() {
        return rabbitMqErrorQueue;
    }

    public static void setRabbitMqErrorQueue(String rabbitMqErrorQueue) {
        Config.rabbitMqErrorQueue = rabbitMqErrorQueue;
    }

    public static String getRabbitMqHost() {
        return rabbitMqHost;
    }

    public static void setRabbitMqHost(String rabbitMqHost) {
        Config.rabbitMqHost = rabbitMqHost;
    }

    public static String getRabbitMqVHost() {
        return rabbitMqVHost;
    }

    public static void setRabbitMqVHost(String rabbitMqVHost) {
        Config.rabbitMqVHost = rabbitMqVHost;
    }

    public static String getRabbitMqUsername() {
        return rabbitMqUsername;
    }

    public static void setRabbitMqUsername(String rabbitMqUsername) {
        Config.rabbitMqUsername = rabbitMqUsername;
    }

    public static String getRabbitMqPassword() {
        return rabbitMqPassword;
    }

    public static void setRabbitMqPassword(String rabbitMqPassword) {
        Config.rabbitMqPassword = rabbitMqPassword;
    }

    @Value("${rabbitmq.host}")
    public void setRabbitMqHosts(String url) {
        setRabbitMqHost(url);
    }

    @Value("${rabbitmq.virtual-host}")
    public void setRabbitMqVHosts(String url) {
        setRabbitMqVHost(url);
    }

    @Value("${rabbitmq.username}")
    public void setRabbitUsernames(String url) {
        setRabbitMqUsername(url);
    }

    @Value("${rabbitmq.password}")
    public void setRabbitPasswords(String url) {
        setRabbitMqPassword(url);
    }

    @Value("${rabbit.mainqueue}")
    public void setRabbitMainQueues(String url) {
        setRabbitMqMainQueue(url);
    }

    @Value("${rabbit.errorqueue}")
    public void setRabbitErrorQueues(String url) {
        setRabbitMqErrorQueue(url);
    }

    @Value("${redis.host}")
    public void setRedisHosts(String url) {
        setRedisHost(url);
    }

    @Value("${redis.port}")
    public void setRedisPorts(int url) {
        setRedisPort(url);
    }
    @Value("${redis.password}")
    public void setRedisPasswords(String url) {
        setRedisPassword(url);
    }
}

      

My MessageConsumer class, in which I use this Conguration to get a message from a jumbbit jq queue and save for redis:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.es.consumer.config.Config;
import com.rabbitmq.jms.admin.RMQConnectionFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;

@Component
public class MessagesConsumer {
    @Autowired
    JmsTemplate jmsTemplate;
    final Logger logger = LoggerFactory.getLogger(MessagesConsumer.class);

    Jedis jedis;

    JedisShardInfo shardInfo;

    @PostConstruct
    public void init() {

        shardInfo = new JedisShardInfo(Config.getRedisHost(), Config.getRedisPort());
        shardInfo.setPassword(Config.getRedisPassword());
        jedis = new Jedis(shardInfo);
        jedis.connect();
        jedis.select(2);

    }

    @Bean
    ConnectionFactory connectionFactory() {

        RMQConnectionFactory connectionFactory = new RMQConnectionFactory();
        connectionFactory.setUsername(Config.getRabbitMqUsername());
        connectionFactory.setPassword(Config.getRabbitMqPassword());
        connectionFactory.setVirtualHost(Config.getRabbitMqVHost());
        connectionFactory.setHost(Config.getRabbitMqHost());
        return connectionFactory;

    }

    @SuppressWarnings("rawtypes")
    @Scheduled(fixedRate = 1)
    public void readQueueAndSaveData() {
// take message process it and save to redis as hmset

}}

      

Any help would be appreciated.

+3


source to share


1 answer


There are two ways to do this. 1) Remove all properties from application.properties properties and write a bean config that will create RedisTemplate and RabbitTemplate beans for you. The required factory properties must be obtained from VCAP_SERVICES. In CF, the VCAP_SERVICES env variable will display the service information associated with the application. When you click the app, with redis, the rabbit service is bound to your space, then their properties are available in VCAP_SERVICES. So just do System.getEnv("VCAP_SERVICES")

in your code and then parse the json to get the service details to create templates.

2) Remove properties from application.properties and use spring cloud connector. spring cloud has a subproject called spring cloud connectors that provide utilities for connecting to various cloud services.

http://cloud.spring.io/spring-cloud-connectors/spring-cloud-spring-service-connector.html#_rabbitmq

      



Just you need to define a class that extends AbstractCloudConfig below

class CloudConfig extends AbstractCloudConfig {
    @Bean
    public RabbitConnectionFactory rabbitFactory() {
        return connectionFactory().rabbitConnectionFactory("rabbit-servicename");
    }

    @Bean
    public RedisConnectionFactory redisFactory() {
        return connectionFactory().redisConnectionFactory("redis-servicename");
    }
}

      

The second approach is preferred if you are using Spring, as it requires very little coding and can be switched to different cloud providers with little effort.

+1


source







All Articles