Dynamically changing the instanceindex with spring cloud stream kafka

415 Views Asked by At

Similar to: Changing spring-cloud-stream instance index/count at runtime

I do a poc on the launch of batch in a microserver architecture and I am using Spring batch with Spring Cloud Stream Kafka. I am looking for a way to dynamically create multiple instances of consumer (processor) application. I saw that it is possible to define a number of instances with

spring.cloud.stream.instanceCount=n 
spring.cloud.stream.instanceIndex=[0, ..., n-1]

However I haven't found a way to dynamically change the value of instanceIndex. Is it possible to modify this value dynamically with Spring Cloud Stream kafka.

Thanks for any help.

1

There are 1 best solutions below

0
On

I have find a solution how change dynamically the instanceindex automatically. I set the value of minPortNum & maxPortNum and instanceCount in the properties file and I vary instanceIndex depending on the value of port-minPortNum, and then I run as much consumer as I want.


@component
class ServerPortCustomize implements WebServerFactoryCustomizer {
    @value("${port.number.min}")
    private Integer minPortNum;
    @value("${port.number.max}")
    private Integer maxPortNum;
    @value("${spring.cloud.stream.instanceCount}")
    private Integer instanceCount;
    
    @Override
    public void customize(final ConfigurableWebServerFactory factory) {
        final int port = SocketUtils.findAvailableTcpPort(minPortNum, maxPortNum);
        factory.setPort(port);
        System.getProperties().put("server.port", port);
        System.getProperties().put("spring.cloud.stream.instanceIndex", port-minPortNum);
    }
}

for this example I have set spring.cloud.stream.instanceCount=3, so I run three consumer with instance index as value 0, 1 & 2