Archives for groovy

Vertx with Groovy config files and environments

(CAVEAT. I don’t think I ever tested this, so there might be one or two tweaks that need to be done)

So vertx has ways to write configs for your verticles.  They are in code, in a .json file with -conf on the command line, in a .js file with load() in js. But nothing built in with Groovy.

You can put them in your code and load them programatically. However, with that route, your config is in your code and changes to the config would require changing code. So then you would want to externalize your config. And vertx allows you to tell it what config file to load with the -conf command line argument.

If you use this approach, then where do you put your config files. In this case, they would have to be in the directory that you are running your verticle. This is fine if you are in the same directory as the verticles, which means a simple verticle not in a module. So now you want to package your verticle(s) into a module. This now means that your config files are in the directory that you are running from but not with the verticle in the module itself. This is also fine and what you typically do when you are using a module say from the vertx mods repository.

So lets now say you want to create configs for different environments and store that config in a .groovy file and in the same directory as your module. And you also have more than one verticle in your module. How do you go about that? This is what this blog post is about. How to do just that. This will still require a .json config file in the directory you run from, but that .json can be as simple as stating what environment you want to run with. So very simple

config.json contains

{ "env" :"prod"}

or
{"env" :"dev"}

So when you want to change your environment you are using you just have to change that one simple property in the config.json file.

Now lets look at how we are going to write our actual config for our verticles using a Groovy script.

I have a Config.groovy file that looks like this

environments {
    dev {
        myVerticleOneConfig: {
            numberOfInstances: 10
        }
        myVerticleTwoConfig: {
            numberOfInstances: 10
        }
        dataIntegrationWorkerConfig: {
            uri: "amqp://localhost:5672"
            defaultContentType: "application/json"
            numberOfInstances: 10
        }
        mongoDBPersisterWorkerConfig: {
            host: "localhost"
            port: 27017
            db_name: "addressBook"
            numberOfInstances: 10
        }
    }
    test {
        myVerticleOneConfig: {
            numberOfInstances: 50
        }
        myVerticleTwoConfig: {
            numberOfInstances: 50
        }
        dataIntegrationWorkerConfig: {
            uri: "amqp://192.168.0.10:5672"
            defaultContentType: "application/json"
            numberOfInstances: 50
        }
        mongoDBPersisterWorkerConfig: {
            host: "192.168.0.11"
            port: 27017
            db_name: "addressBook"
            numberOfInstances: 20
        }
    }
    prod {
        myVerticleOneConfig: {
            numberOfInstances: 100
        }
        myVerticleTwoConfig: {
            numberOfInstances: 100
        }
        dataIntegrationWorkerConfig: {
            uri: "amqp:/63.18.0.10:5672"
            defaultContentType: "application/json"
            numberOfInstances: 100
        }
        mongoDBPersisterWorkerConfig: {
            host: "63.18.0.11"
            port: 27017
            db_name: "addressBook"
            numberOfInstances: 30
        }
    }
}

Now I want to use these configs in my vertx module/application. One you have more than one verticle to deploy in your app with other mods and verticles and worker verticles, I highly recommend creating a vertx module for your app and to create a main app that deploys your verticles. like

MyApp.groovy file

// Config variable definition will be going here

container.deployVerticle("VerticleOne.groovy", config.verticleOne.numberOfInstances)
container.deployWorkerVerticle("VerticleTwoWorker.groovy", config.verticleTwo.numberOfInstances)
container.deployModule(
        "amqp-busmod#1.2.0-SNAPSHOT",
        config.dataIntegrationWorkerConfig,
        config.dataIntegrationWorkerConfig.numberOfInstances
)

container.deployModule(
        "vertx.mongo-persistor-v1.1",
        config.mongoDBPersisterWorkerConfig,
        config.mongoDBPersisterWorkerConfig.numberOfInstances
)

So in this we deploy our verticles and worker verticles and repo mods passing in configuration and number of instances using our external configuration which we will assign to a variable called config.

So the next part is to define that variable “config”. With Groovy’s ConfigSlurper we can load in the Config.groovy file with the following line of code

def config = new ConfigSlurper(container.config.env).parse(new File('Config.groovy').toURL())

That line goes to the top of the MyApp.groovy file.

Notice the constructor of ConfigSluper I am passing in container.config.env . This will come from reading in the config.json file being passed to -conf at the command line. If the env property in config.json is equal to “dev” then the dev section in the Config.groovy will fill all the values under the “config” variable and you are all set.

Now when I run my module it will be with the following command line

vertx runmod com.pwp.mymodule-v1.0.0 -conf config.json