在spring boot application

时间:2017-05-12 10:53:20

标签: spring-boot redis rabbitmq spring-jms pivotal-cloud-foundry

我能够使用关键的REDIS和RABBITMQ服务。虽然绑定服务我能够获取凭据并在我的application.properties中使用该凭据进行spring启动项目。

但是我使用的这个配置在application.Properties中是硬编码的 为了动态地进行这种配置,我发现我们可以使用由pivotal提供的vcap服务。

所以想要使用redis和rabbimq的运行时凭证。

我的代码在下面供参考。

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

我的配置课程:

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);
    }
}

我的MessagesConsumer类,我在其中使用Conguration从rabbitmq jms队列中获取消息并保存到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

}}

任何帮助都会很明显。

0 个答案:

没有答案