Tutorials/howto use cluster config

From CubeiaWiki

Jump to: navigation, search

This is a simple example of how you can use and access the properties from cluster.props. The properties specified there will be propagated by the master node so it is a powerful way of keeping properties consistent over many servers.

The example below outlines how to make a configuration for a remote service URL in String format.

Documentation Links
References for documentation regarding the configuration API.
Configurable Javadoc
Config provider Javadoc
Developer manual

Configuration Placeholder

Create an interface, we will call it RemoteServiceConfig, let it extend Configurable. Add an annotation for the namespace(@Configurated) and a getter for remote URL with a @Property annotation with default value.

@Configurated(namespace="com.example.remoteservice")
public interface RemoteServiceConfig extends Configurable {

    @Property(defaultValue="1") 
    public int getCallerId();

    @Property(defaultValue="http://userservice:8080/user-service") 
    public String getRemoteUrl();
	
}

Note that the "callerId" returns an integer. The configuration service will be able to convert the configuration string value to any type that is either a primitive or have a constructor with a single string argument, and the special cases InetAddress, File and enumerations.

This interface has to be exported to the common classloader in your service deployment descriptor service.xml.

Configuration Accessor

In our example we will read the configuration from a deployed Service. The code snippet below is from the init-method of the Service's lifecycle. We provide null as namespace to the getConfiguration call since we are not interested in overriding the default namespace for the provided configuration class.

public void init(ServiceContext con) throws SystemException {
    try {
        ClusterConfigProviderContract contr = con.getParentRegistry().getServiceInstance(ClusterConfigProviderContract.class);
        RemoteServiceConfig configuration = contr.getConfiguration(RemoteServiceConfig .class, null);
        remoteUrl = configuration.getRemoteUrl();
	callerId = configuration.getCallerId();
    } catch (ConfigurationException e) {
        log.error("Failed to read remote service configuration. Will fall back on default value", e);
    }
}

Configuration

Add the configuration property to $FBHOME$/conf/cluster.props. If you are running a cluster you only need to add this on the master server(s).

com.example.remoteservice.caller-id=1
com.example.remoteservice.remote-url=http://localhost:8080/remote-service

That's it! The configuration should be set and ready to go.

Personal tools