Skip to content

Getting started with Redis client

Shailesh Birari edited this page Dec 5, 2017 · 18 revisions

Dyno wraps Jedis client when talking to a dynomite-redis setup.

Dyno client setup

Here is how you can initialize the dyno client, DynoJedisClient

  1. First pull the dyno client into your build
dependencies {
        compile "com.netflix.dyno:dyno-jedis:{LATEST VERSION}"
}
  1. Build and initialize the client
DynoJedisClient jClient = new DynoJedisClient.Builder()
		.withApplicationName(YOUR APP NAME)
		.withDynomiteClusterName(YOUR DYNOMITE CLUSTER NAME)
        .withDiscoveryClient(discoveryClient) 
		.build();

No DiscoveryClient? Add in your own host supplier

Note that Dyno needs a host supplier so that it knows how to connect to your dynomite cluster. This tells it where the dynomite hosts are so that it can bootstrap its connection pool. At Netflix, we use the Eureka based HostSupplier The EurekaHostsSupplier needs the DiscoveryClient to be inited.

If you don't use any of this, no problem! Just add in your own host supplier. Here is a simple example on how to do that. Note the rack being supplied to the hosts. That is important for dyno so that it can do rack aware load balancing for you. Also the port should be the Dynomite listen port

		
final HostSupplier customHostSupplier = new HostSupplier() {

final List<Host> hosts = new ArrayList<Host>();
				
   @Override
   public Collection<Host> getHosts() {
				
             
	hosts.add(new Host("hostname1", 9080, Status.Up).setRack("us-east-1c"));
	hosts.add(new Host("hostname2", 9080, Status.Up).setRack("us-east-1c"));
	hosts.add(new Host("hostname3", 9080, Status.Up).setRack("us-east-1c"));
	hosts.add(new Host("hostname4", 9080, Status.Up).setRack("us-east-1c"));
             
	return hosts;
   }
};
			
DynoJedisClient dynoClient = new DynoJedisClient.Builder()
            .withApplicationName("MY APP")
            .withDynomiteClusterName("MY CLUSTER")
            .withHostSupplier(customHostSupplier)
            .build();
             
            dynoClient.set("foo", "puneetTest");
            System.out.println("Value: " + dynoClient.get("foo"));
            
            dynoClient.stopClient();

Adding in your own TokenMapSupplier

For now, Dyno client needs the topology map from Dynomite so that it can do effective load balancing, such as TokenAware. Dynomite currently does not expose an endpoint that can be used by Dyno to get the topology map (fix coming soon). You can unblock yourself manually for now by providing your own impl of TokenMapSupplier.

I've uploaded an abstract impl for this AbstractTokenMapSupplier

When you extend this, all you have to do is provide the default json payload that it expects. See the unit test there as an example

final String json = "[{\"token\":\"3051939411\",\"hostname\":\"hostname1\",\"zone\":\"us-east-1d\"}\"," +
				"\"{\"token\":\"188627880\",\"hostname\":\"hostname2\",\"zone\":\"us-east-1d\"},\"" +
				"\"{\"token\":\"2019187467\",\"hostname\":\"hostname3\",\"zone\":\"us-east-1c\"},\"" +
				"\"{\"token\":\"3450843231\",\"hostname\":\"hostname4\",\"zone\":\"us-east-1c\"},\""+
				"\"{\"token\":\"587531700\",\"hostname\":\"hostname5\",\"zone\":\"us-east-1c\"},\"" +
				"\"{\"token\":\"3101134286\",\"hostname\":\"hostname6\",\"zone\":\"us-east-1e\"},\"" +
				"\"{\"token\":\"237822755\",\"hostname\":\"hostname7","zone\":\"us-east-1e\"},\"" +
				"\"{\"token\":\"1669478519\",\"hostname\":\"hostname8\",\"zone\":\"us-east-1e\"}]\"";

private TokenMapSupplier testTokenMapSupplier = new AbstractTokenMapSupplier() {

	@Override
	public String getTopologyJsonPayload() {
		return json;
	}

	@Override
	public String getTopologyJsonPayload(String hostname) {
		return json;
	}
};

Once you do this, you can set your token map supplier impl on the Dyno configuration, when building the Dyno client.

public ConnectionPoolConfigurationImpl withTokenSupplier(TokenMapSupplier tSupplier) {
	tokenSupplier = tSupplier;
	return this;
}

Default Configuration

See the configuration wiki page for all configuration options as well as defaults. The implementation class is ConnectionPoolConfigurationImpl

Overriding the default config

There are two ways to do this. One using an instance of ConnectionPoolConfigurationImpl. The other is via Archaius properties.

Note that in both mechanisms, if you don't override anything, then the default is used.

Using the connection pool builder pattern (java object)

The DynoJedisClient Builder will construct a default ConnectionPoolConfigurationImpl object. If you want to override any of the configuration params, then it is easiest to just provide your own connection pool object to the builder. The connection pool object uses a builder pattern that helps you explore all the overrides available. See example below

	DynoJedisClient client = new DynoJedisClient.Builder()
									.withApplicationName(APP)
									.withDynomiteClusterName(CLUSTER)
									.withCPConfig(new ArchaiusConnectionPoolConfiguration(APP)
												  .setPort(port)
												  .setMaxTimeoutWhenExhausted(1000)
												  .setMaxConnsPerHost(5)
									.build());

Using Archaius Properties

See the configuration wiki page for property names and examples of how to do this.